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@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
- 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@main
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@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 ${{ 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'
github-script.yml
name: 05-1. GitHub Script - Thank you
on:
issues:
types: [opened, edited, reopened, labeled]
# Limit the permissions of the GITHUB_TOKEN
permissions:
contents: read
issues: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '👋 Thank you! We appreciate your contribution to this project.'
})
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']
})
hello-world-composite-action/action.yml
name: 'Hello World Composite Action'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
using: "composite"
steps:
- run: echo Hello from composite action ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "random-id=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: bash
- run: echo "${{ github.action_path }}" >> $GITHUB_PATH
shell: bash
- name: Hello world
uses: actions/hello-world-javascript-action@main
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
hello-world-composite.yml
name: 05-2. Hello World Composite
on:
pull_request:
branches: [main]
workflow_dispatch:
jobs:
hello_world_job1:
runs-on: ubuntu-latest
name: A job1 to say hello
steps:
- id: hello-world
uses: githubabcs/hello-world-composite-action@main
with:
who-to-greet: 'Hello from GH ABCs'
- run: echo random-number ${{ steps.hello-world.outputs.random-number }}
shell: bash
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 ${{ steps.hello-world.outputs.random-number }}
shell: bash
use-custom-actions.yml
name: 05-3. Use Custom Actions (JS & Doker)
on:
pull_request:
types: [labeled]
workflow_dispatch:
# Limit the permissions of the GITHUB_TOKEN
permissions:
contents: read
issues: write
jobs:
js-custom-actions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "🎉 Running the JS actions"
- name: hello-action
uses: ./.github/actions/hello-world-js
if: ${{ success() }}
- name: ha-ha
uses: ./.github/actions/joke-action
id: jokes
- name: create-issue
uses: ./.github/actions/issue-maker-js
with:
repo-token: ${{secrets.GITHUB_TOKEN}}
joke: ${{steps.jokes.outputs.joke-output}}
issue-title: "A joke for you from custom actions workflow"
docker-custom-actions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "🎉 Running the Docker actions"
- name: hello-action
uses: ./.github/actions/hello-world-docker
- name: meow
uses: ./.github/actions/cat-facts
id: cat
- name: create-issue
uses: ./.github/actions/issue-maker-docker
with:
repoToken: ${{secrets.GITHUB_TOKEN}}
catFact: ${{steps.cat.outputs.fact}}
issueTitle: "A cat fact for you from ${{ github.repository_owner }}"