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
- Open the workflow file github-script.yml
- Edit the file and copy the following YAML content at the end of the file:
apply-label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
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
- Go to
Actions
and see the details of your running workflow - After the workflow completes, a new label should be applied to your issue
- 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:
- name: Hello world
uses: actions/hello-world-javascript-action@v1
with:
who-to-greet: "${{ inputs.who-to-greet }}"
id: hello
- name: Echo the greeting's time
run: echo 'The time was ${{ steps.hello.outputs.time }}.'
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:
hello_world_job2:
runs-on: ubuntu-latest
name: A job2 to say hello
steps:
- uses: actions/checkout@v2
- 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 ${{ steps.hello-world.outputs.random-number }}
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
- 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
- Follow the guide to create a JavaScript action
- Use your action in a workflow
- name: Hello world action step
id: hello
uses: <YOUR-USER-ACCOUNT>/[email protected]
with:
who-to-greet: 'Mona the Octocat'