In this hands-on lab you will learn how to create a docker action, pass in parameters and return values to your workflow. And you will learn how to test the action locally with a CI build.
This hands on lab consists of the following steps:
- Create a new file
hello-world-docker-action/action.yml
:
- Add content to the
action.yml
file (see the template and help). Have the action run aDockerfile
and pass in the parameterwho-to-greet
with the default valueworld
. Also give back an output that returns the current time.
Solution
name: 'Hello World Docker Action'
description: 'Say hello to a user or the world.'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
default: 'world'
outputs:
time:
description: 'The time we said hello.'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.who-to-greet }}
- Commit the file (
[skip ci]
to not run a build, yet). - Inside the
hello-world-docker-action
folder create theDockerfile
. The container inheritsFROM alpine:3.10
and should copy and execute a fileentrypoint.sh
. Remember to make the script executable withRUN chmod +x entrypoint.sh
Solution
FROM alpine:3.10
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
- Commit the file (
[skip ci]
to skip running a build, for now). - Create the file
entrypoint.sh
in the folder. It is a simple bash script that writes a message to the console and sets the output parameter.
Solution
#!/bin/sh -l
echo "hello $1"
echo "time=$(date)" >> $GITHUB_OUTPUT
- Commit the file (
[skip ci]
to not run a build, yet).
- To test the action we create a new workflow file
.github/workflows/hello-world-docker-ci.yml
. - The workflow should run on every push if the action has changed. Also add a manual trigger to start the build manually. Checkout the repository to reference the action locally and without a git reference.
Solution
name: CI Build for Docker Action
on:
push:
branches: [ main ]
paths: [ hello-world-docker-action/** ]
workflow_dispatch:
jobs:
test-action:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3.3.0
- name: Run my own container action
id: hello-action
uses: ./hello-world-docker-action
with:
who-to-greet: '@wulfland'
- name: Output time set in the container
run: echo "The time was ${{ steps.hello-action.outputs.time }} when the action said hello"
- Run the workflow and see how the parameters are passed into the action and back to the workflow.
If time permits you can create a release and then use the action in a workflow in another repository.
Note You can only publish a GitHub Action that exists in the root of a repository.
-
Create a new public repository
hello-world-docker-action
and copy all the files from hello-world-docker-action to it. -
Create a new release with a tag
v1
and the titlev1
. ClickGenerate release notes
and publish the release.
- Create a new repository or use another existing one and create a simple workflow that calls the action your created in version
v1
.
Solution
name: Test
on: [workflow_dispatch]
jobs:
test-action:
runs-on: ubuntu-latest
steps:
- name: Say hello
uses: <your-github-username>/hello-world-docker-action@v1
with:
who-to-greet: '@octocat'
In this hands-on lab you've learned how to create a docker action, pass in parameters, return values to your workflow, and to test the action locally with a CI build.
You can continue now with Staged deployments.