Create a Commit Action
In this section, you will learn how to create an Action that commits on GitHub using the STK CLI.
Introduction
- Intermediate level
This is an example of how to create an Action. To create your own Action, see the Actions page.
Prerequisites
- Have a StackSpot account
- Install STK CLI
- Have permission to create content
- Log in to your StackSpot account. Run the following command in your terminal:
stk login
Create an Action
Summary of steps:
- 1. Create an Action using the command
stk create action - 2. Edit the
action.yamlfile with the inputs - 3. Run the Action locally
- 4. Publish the Action in your Studio
- 5. Add the Action to the Stack
- 6. Publish the Stack
Step 1. Create the Action
In your terminal or IDE of choice, run the command:
stk create action
Answer the questions in your terminal:
- ? Name your action: enter a name for your Action.
- ? Do you want to init a git repository (Y/n): option to start a Git repository in the Action folder. For this example, type
nnot to start a Git repository. - ? Add remote? for this example, answer
no. - ? Action description: enter text describing your Action.
- ? Version (0.0.1): enter the desired version number for your Action. It must follow semantic versioning, such as
0.0.1. - ? Select the action type: select the type of Action you want to create. For this example, select
Shell. - ? Do you want to add a Required Connection? (Y/n): option to add a Connection Interface as required in the Action. For this example, type
nnot to add a Connection Interface.
Use this option when the Action must interact with infrastructure or Cloud resources during execution. To learn more about Connection Interfaces, see the documentation.
After answering the questions, you will receive confirmation in the terminal that the Action has been created, as in the example:
? Name your action: commit-push-action
? Do you want to init a git repository (Y/n): no
? Action description: This Action commits and pushes your changes to GitHub.
? Version: (0.0.1)
? Select the action type: Shell
? Do you want to add a Required Connection? (Y/n): no
- Action commit-push-action successfully created.
The Action created is just a minimally functional basic model.
For the next steps, edit all files and options using the presented examples or with the parameters you want your Action to execute.
Step 2. Edit the Action inputs in action.yaml
The Action is created with example inputs already filled in. You need to edit the action.yaml file to edit and add the inputs that will be used by your Action script.
- Access the Action folder you created and edit the inputs in the
action.yamlfile:
The inputs below are only an example and are part of the guide to creating an Action that commits/pushes to GitHub. You can create your own Action by following this guide.
inputs:
- label: Personal Access Token
name: pat
type: password
- label: Organization Name
name: org_name
type: text
- label: Repository Name
name: repo_name
type: text
- label: Commit message
name: message
type: text
For each type of Action, there are options where you must enter the commands or scripts that will be executed by the Action. The example below demonstrates commands that can be executed in a Shell-type Action. You can use them in your Action or type the Shell commands you want.
Learn more about how to fill in the fields for each Action type in Action Types.
requirements-check:
- check-command:
linux: |
git --version
mac: |
git --version
windows: |
git --version
info: "Git not installed"
This section of the Shell Action is used to display the info text in the terminal when an error occurs in this command.
- Add the commands to your
action.yamlfile according to your operating system. The following code snippet is just an example; you can use it or type any commands you want:
script:
linux: |
git add .
git commit -m "{{message}}"
git push https://{{pat}}@github.com/{{org_name}}/{{repo_name}}
mac: |
git add .
git commit -m "{{message}}"
git push https://{{pat}}@github.com/{{org_name}}/{{repo_name}}
windows: |
git add .
git commit -m "{{message}}"
git push https://{{pat}}@github.com/{{org_name}}/{{repo_name}}
This example adds, commits, and pushes your changes to a remote repository.
Check the indentation. If an error occurs, it is probably for this reason.
The script field has two indentation levels.
Step 3. Run the Action locally
To run the Action locally as a test, run the command with the Action folder path:
stk run action <path-of-your-actions-folder>
Example:
stk run action /Users/myteam/workspace/demo-action/commit-push-action
Or go into the Action folder and run the command:
stk run action .
At this point, the Action will show as questions the inputs you added to your action.yaml file. You must answer the questions to continue. Consider the example inputs presented previously and answer them:
? Personal Access Token: add your PAT token
? Organization Name: add your organization name
? Repository Name: add your repository name
? Commit message: commit-test
Step 4. Publish the Action in your Studio
It is not mandatory to add a Git repository to publish your Action.
Run the command in your Action folder and enter the name of your Studio:
stk publish action
You can also enter the name of the Studio directly in the terminal with the --studio option, as in the following example:
stk publish action --studio my-studio-name
Step 5. Add the Action to the Stack
Follow the steps:
- On the StackSpot Portal, access your Studio;
- Click the Stack you created previously. In the menu, click ‘Actions’;
- In the menu, click ‘Add Actions’ and choose your Action.
Step 6. Publish your Stack
Now you need to publish your Stack:
- Access your Studio on the StackSpot Portal;
- In the menu, click ‘Stacks’;
- Click the ‘Publish’ button;
You created an Action and published the Stack described in the First Steps: Creator section.
Your content is available for you or someone else to use.