Skip to content

Latest commit

 

History

History
161 lines (111 loc) · 5.19 KB

02-My-first-action.md

File metadata and controls

161 lines (111 loc) · 5.19 KB

🔨 Hands-on: My first Action

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:

Creating the action

  1. Create a new file hello-world-docker-action/action.yml:

image

  1. Add content to the action.yml file (see the template and help). Have the action run a Dockerfile and pass in the parameter who-to-greet with the default value world. 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 }}
  1. Commit the file ([skip ci] to not run a build, yet).
  2. Inside the hello-world-docker-action folder create the Dockerfile. The container inherits FROM alpine:3.10 and should copy and execute a file entrypoint.sh. Remember to make the script executable with RUN chmod +x entrypoint.sh
Solution
FROM alpine:3.10

COPY entrypoint.sh /entrypoint.sh

RUN chmod +x entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
  1. Commit the file ([skip ci] to skip running a build, for now).
  2. 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
  1. Commit the file ([skip ci] to not run a build, yet).

Testing the action

  1. To test the action we create a new workflow file .github/workflows/hello-world-docker-ci.yml.
  2. 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/[email protected]

      - 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"
  1. Run the workflow and see how the parameters are passed into the action and back to the workflow.

image

Optional: Release and use the action

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.

  1. Create a new public repository hello-world-docker-action and copy all the files from hello-world-docker-action to it.

  2. Create a new release with a tag v1 and the title v1. Click Generate release notes and publish the release.

image

  1. Go to Settings > Actions > General > Access, and ensure Accessible from repositories owned by the user '<your-github-username> is selected.

  2. 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'

Summary

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.