5 - Custom actions
In this lab you will create and use custom actions.
Duration: 15-20 minutes
References:
- Creating actions
- Creating a composite action
- Creating a JavaScript action
- GitHub Actions Toolkit
- actions/github-script
5.1 Use the github-script action to apply a label to an issue
- Open the workflow file github-script.yml
- Edit the file and copy the following YAML content at the end of the file:
```YAML
apply-label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7 with: script: | github.rest.issues.addLabels({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, labels: [‘Training’] }) ```
- Commit the changes into the
main
branch - Open a new issue or edit an exiting one to trigger the workflow. If the
Issues
tab is not visible, open your repository settings and enable it. - Go to
Actions
and see the details of your running workflow - After the workflow completes, a new label should be applied to your issue
5.2 Use a composite action
- Open the composite action file /.github/actions/hello-world-composite-action/action.yml
- Edit the file and copy the following YAML content at the end of the file:
```YAML
- name: Hello world uses: actions/hello-world-javascript-action@main with: who-to-greet: “$” id: hello
- name: Echo the greeting’s time run: echo ‘The time was $.’ shell: bash ```
- Commit the changes into a new
feature/lab05
branch - Open the workflow file hello-world-composite.yml
- Edit the file and copy the following YAML content at the end of the file:
```YAML
hello_world_job2:
runs-on: ubuntu-latest
name: A job2 to say hello
steps:
- uses: actions/checkout@v4
- id: hello-world uses: ./.github/actions/hello-world-composite-action with: who-to-greet: ‘Mona the Octocat from composite action’
- run: echo random-number from composite action $ shell: bash ```
- Update the workflow to run on pull_request events
on: pull_request: branches: [main] workflow_dispatch:
- Commit the changes into the same
feature/lab05
branch - Open a new pull request
- Go to
Actions
and see the details of your running workflow - Complete the pull request and delete the source branch
5.3 Custom JS and Docker actions
- Study the implementation of the custom action from the folder: /.github/actions/
- Open the workflow file use-custom-actions.yml
- Edit the file and copy the following YAML content to update the issue title:
issue-title: "A joke for you from custom actions workflow"
- Commit the changes into the
main
branch - Go to
Actions
and manually trigger the workflow by clicking onRun Workflow
button - See the details of your running workflow
5.4 (Optional) Create a JavaScript action
- Follow the guide to create a JavaScript action
- Use your action in a workflow
```YAML
- name: Hello world action step
id: hello
uses:
/hello-world-javascript-action@v1.1 with: who-to-greet: 'Mona the Octocat' ```
- name: Hello world action step
id: hello
uses: