diff --git a/.github/workflows/delete-workflows.yml b/.github/workflows/delete-workflows.yml new file mode 100644 index 0000000..a07401b --- /dev/null +++ b/.github/workflows/delete-workflows.yml @@ -0,0 +1,14 @@ +name: Delete Workflows + +on: [workflow_dispatch] + +jobs: + do: + runs-on: ubuntu-latest + steps: + - name: Delete workflow runs + uses: Mattraks/delete-workflow-runs@v2.0.3 + with: + retain_days: 0 + keep_minimum_runs: 0 + diff --git a/.github/workflows/init.yml b/.github/workflows/init.yml new file mode 100644 index 0000000..e0dc0f9 --- /dev/null +++ b/.github/workflows/init.yml @@ -0,0 +1,26 @@ +name: Init + +on: [create, workflow_dispatch] + +permissions: + issues: write + contents: read + +jobs: + initialize_repo: + runs-on: ubuntu-latest + if: ${{ github.ref == 'refs/heads/main' }} + steps: + - name: Checkout + uses: actions/checkout@v3.3.0 + - name: Create Issue + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh issue create -t "โ“ What is your experience with GitHub?" -b "What is your experience with GitHub? Whats is your experience with other CI/CD tools? Comment below ๐Ÿ‘‡" + gh issue create -t "โ“ What do you want to learn?" -b "What is it you want to learn? What specific scenarios are you interrested in? Comment below ๐Ÿ‘‡" + gh issue create -t "๐Ÿ” Retro Day one: What was good?" -b "What did you like on day one of this course? What was good? Comment below ๐Ÿ‘‡" + gh issue create -t "๐Ÿ” Retro Day one: What can be improved?" -b "What can be improved? What was missing? Any additional comments? Comment below ๐Ÿ‘‡" + gh issue create -t "โค๏ธ What was the best feature you learned yesterday?" -b "What was the GitHub Actions feature you liked best that you have learned about yesterday the first time? If you want add your hobby to share it with the group. Comment below ๐Ÿ‘‡" + gh issue create -t "๐Ÿ” Retro Day two: What was good?" -b "What did you like on day two of this course? What was good? Comment below ๐Ÿ‘‡" + gh issue create -t "๐Ÿ” Retro Day two: What can be improved?" -b "What can be improved? What was missing? Any additional comments? Comment below ๐Ÿ‘‡" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/CheatSheet.md b/CheatSheet.md new file mode 100644 index 0000000..dea2bb7 --- /dev/null +++ b/CheatSheet.md @@ -0,0 +1,39 @@ +# Cheat Sheet + +This document contains an overview of fundamental concepts and terminology, you can use this as a reference book if you get lost in the terminology along the way. + +## Github actions high level components + +image + +## Yaml functions + +| Function | Description | +|---------------------------------|-------------------------------------------------------------------------------------------------------| +| success() | Returns true if none of the previous steps have failed or been cancelled. | +| always() | Returns true even if a previous step was cancelled and causes the step to always get executed anyway. | +| cancelled() | Returns only true if the workflow was canceled. | +| failure() | Returns true if a previous step of the job had failed. | +| contains(search, item) | Returns true if search contains item. | +| startsWith(search, item) | Returns true if search starts with item. | +| endsWith(search, item) | Return true if search ends with item. | +| format('{0} {1}', item1, item2) | Replaces placeholders in a string. | +| join(array, separator) | All values in array are concatenated into a string using provided separator | +| toJSON(value) | Returns a pretty-print JSON representation of value. | +| fromJSON(value) | Returns a JSON object or JSON data type for value. | + +## Workflow commands + +See also: +https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions +Actions can communicate with the runner machine to set environment variables, output values used by other actions, add debug messages to the output logs, and other tasks. +Most workflow commands use the echo command in a specific format, while others are invoked by writing to a file. + +Example echo workflow command: +```bash +echo "::workflow-command parameter1={data},parameter2={data}::{command value}" +``` +Example write to file workflow command: +```bash +echo "SELECTED_COLOR=green" >> $GITHUB_OUTPUT +``` diff --git a/GettingReady.md b/GettingReady.md new file mode 100644 index 0000000..2259800 --- /dev/null +++ b/GettingReady.md @@ -0,0 +1,18 @@ +# Getting ready + +## Step 1: Set up your GitHub.com account + +For this class, we will use a public account on GitHub.com. We do this for a few reasons: + +We don't want you to "practice" in repositories that contain real code. +We are going to break some things, so we can teach you how to fix them (therefore, refer to the line above). + +You can set up your free account by following these steps: + +* Access [GitHub.com](https://github.com) and click Sign up +* Choose the free account +* You will receive a verification email at the address provided +* Click the link to complete the verification process +* If you already have an account, verify that you can visit github.com within your organization's network. + +GitHub is designed to run on the current versions of all major browsers. Take a look at our list of supported browsers. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f57ae43 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Michael Kaufmann + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f49c2f6 --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# GitHub Actions Fundamentals + +Repo for the `GitHub Actions Fundamentals` training. + +## ๐Ÿ‘‰ Objectives + +- Understand the __basic components__ and vocabulary of GitHub Actions +- Understand the __YAML__ syntax (scalar types, quotes, literal blocks, maps, and sequences) +- Understand the __workflow syntax__ and how to write workflows using intellisense +- Understand events that can __trigger__ workflows +- Learn the __context and expression syntax__ as well as workflow commands +- Know the different types of __actions__ and how to create/publish them +- Understand the different hosting options for __runners__ +- Use __Secrets__ and __Environments__ for staged deployments +- Workflow templates and __reusable workflows__ + +## Getting ready + +Please follow [these instructions](GettingReady.md) and make sure you have set up everything correctly following the [prerequisites](#-prerequisites). + +## ๐Ÿ“† Agenda + +### Day 1: ๐Ÿš€ Getting started + +- [ ] Introduction and Icebreaker +- [ ] GitHub Actions Fundamentals +- [ ] The YAML syntax +- [ ] Basic workflow syntax +- [ ] Events that trigger workflow +- [ ] Jobs and steps +- [ ] :mag: Demo: Creating a workflow +- [ ] ๐Ÿ”จ Hands-on: [My first Action workflow](hol/01-My-first-workflow.md) +- [ ] Advanced [workflow syntax](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions) +- [ ] :coffee: Break +- [ ] GitHub Actions +- [ ] Types of Actions +- [ ] ๐Ÿ”จ Hands-on: [My first container Action](hol/02-My-first-action.md) +- [ ] Typescript and composite actions +- [ ] Sharing and releasing actions +- [ ] Using the GitHub API and Octokit +- [ ] Job descriptions + +### Day 2 +- [ ] Introduction +- [ ] Actions for CI/CD +- [ ] Permissions for GitHub Token +- [ ] Environments and Secrets +- [ ] Staged deployments with environments, approvals, and deployment gates +- [ ] ๐Ÿ”จ Hands-on: [Staged deployments](hol/03-Staged-deployments.md) +- [ ] :coffee: Break +- [ ] Action policies +- [ ] Running your workflows +- [ ] Sharing workflows +- [ ] ๐Ÿ”จ Hands-on: [Reusable workflows](hol/04-Reusable-workflows.md) +- [ ] Best practices and security + + +## โšก Prerequisites + +The workshop is designed for developers that have used other platforms like Azure DevOps, GitLab, or Bitbucket and now want to switch to GitHub. But it is also suitable for people that are new to topics like git, CI/CD, and DevOps. + +For this workshop you need the following: + +- A laptop (Windows, Mac, or Linux) +- A free account for https://github.com +- It is recommended to have a second screen for the hand-on labs + +## Cheat Sheet +Find common terminology for reference [here](./CheatSheet.md) + diff --git a/hol/01-My-first-workflow.md b/hol/01-My-first-workflow.md new file mode 100644 index 0000000..f1d8016 --- /dev/null +++ b/hol/01-My-first-workflow.md @@ -0,0 +1,122 @@ +# ๐Ÿ”จ Hands-on: My first workflow + +In this hands-on lab your will create your first GitHub Actions Workflow and learn how you can use Actions to automate tasks in your software development lifecycle. If you like more background information, please refer to the [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions) pages on GitHub Docs. Good luck! ๐Ÿ‘ + +This hands on lab consists of the following steps: +- [Creating a repository](#creating-a-repository) +- [Creating the workflow](#creating-the-workflow) +- [Viewing your workflow results](#viewing-your-workflow-results) + +## Creating a repository + +Go to [Code](/../../) tab of this repository and click `Use this template`: + +2022-09-18_11-24-58 + +Select your GitHub user as the owner and name the repository. Leave the repo public to have unlimited action minutes: + +2022-09-18_11-25-57 + +Continue now in the new repository. + +## Creating the workflow + +Go to **Actions** | [New Workflow](/../../actions/new) and click on [set up a workflow yourself](/../../new/main?filename=.github%2Fworkflows%2Fmain.yml&workflow_template=blank). + +1. Rename the file `main.yml` in the `.github/workflows` directory to `github-actions-demo.yml`. +image + +2. Remove the template content - we want to create the workflow from scratch. +3. Click Ctrl+Space and select name as the first element: + +image + +4. Set the workflow name to `GitHub Actions Demo`: + +```YAML +name: GitHub Actions Demo +``` + +5. Add the triggers to the worklow with the help of Ctrl+Space and the documentation. We want the workflow to trigger: + - on every push to the `main` branch, but not when there are changes to files in the `.github` folder. + - on every pull request with `main` as the base branch + - Every sunday at 6:15 UTC + - Manually + +
+ Solution + +```YAML +on: + push: + branches: [ main ] + paths-ignore: [.github/**] + pull_request: + branches: [ main ] + schedule: + - cron: '15 6 * * 0' + workflow_dispatch: +``` + +
+ +6. Create a job `Build` that runs on the latest Ubuntu image on GitHub hosted runners. Check the documentation of the [virtual environments](https://github.com/actions/virtual-environments/) what label to use and what version it is. The job should do the following things: + - Output the name of the event that triggered the workflow + - Output the name of the branch that the repository is currently referencing + - List all files in the repository + +
+ Solution + +```YAML +jobs: + Build: + runs-on: ubuntu-latest + steps: + - run: | + echo "๐ŸŽ‰ The job was triggered by event: ${{ github.event_name }}" + echo "๐Ÿ”Ž The name of your branch is ${{ github.ref }} and your repository is ." + + - uses: actions/checkout@v3.3.0 + + - name: List files in the repository + run: | + echo "The repository ${{ github.repository }} contains the following files:" + tree +``` + +
+ +7. Commit the workflow file - and trigger the workflow manually. It should not run automatically if your path filter works. Got to [Action](/../../Actions), select [GitHub Actions Demo](/../../actions/workflows/github-actions-demo.yml) and `Run workflow`: + +image + + +## Viewing your workflow results + +1. Click on your workflow run: + +image + +2. Click on the job 'Build': + +image + +3. Expand `Set up job` and note the log with the line number (line numbers are links). Check the information about the virtual environment and included software: + +image + +4. Expand your jobs and check that the output was correct. + +image + +5. Verify you other triggers by modifying the [README.md](/README.md) file: + - Modify and commit: triggers build (`push`) + - Modify and add `[skip ci]` (not triggering the workflow): + image + + - Modify and create pull request (trigger: `pull_request`) + +## Summary + +In this hands-on you've learned to create you first workflow with triggers, jobs, steps, and expressions. Next you will write your [first GitHub Action](02-My-first-action.md). diff --git a/hol/02-My-first-action.md b/hol/02-My-first-action.md new file mode 100644 index 0000000..7a8303e --- /dev/null +++ b/hol/02-My-first-action.md @@ -0,0 +1,161 @@ +# ๐Ÿ”จ 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](#creating-the-action) +- [Testing the action](#testing-the-action) +- Optional: [Release and use the action](#optional-release-and-use-the-action) + + +## Creating the action + +1. Create a new file [`hello-world-docker-action/action.yml`](/../../new/main?filename=hello-world-docker-action%2Faction.yml): +image + +2. Add content to the `action.yml` file (see the [template](https://github.com/actions/hello-world-docker-action) and + [help](https://github.com/actions/hello-world-docker-action)). 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 + +```YAML +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 }} +``` + +
+ +3. Commit the file (`[skip ci]` to not run a build, yet). +4. Inside the `hello-world-docker-action` folder create the [`Dockerfile`](/../../new/main?filename=hello-world-docker-action%2FDockerfile). 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 + +```dockerfile +FROM alpine:3.10 + +COPY entrypoint.sh /entrypoint.sh + +RUN chmod +x entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] +``` + +
+ +5. Commit the file (`[skip ci]` to skip running a build, for now). +6. Create the file [`entrypoint.sh`](/../../new/main?filename=hello-world-docker-action%2Fentrypoint.sh) in the folder. It is a simple bash script that writes a message to the console and sets the output parameter. + +
+ Solution + +```bash +#!/bin/sh -l + +echo "hello $1" + +echo "time=$(date)" >> $GITHUB_OUTPUT +``` + +
+ +7. 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`](/../../new/main?filename=.github%2Fworkflows%2Fhello-world-docker-ci.yml&workflow_template=blank). +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 + +```YAML +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" + +``` + +
+ +3. 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](../hello-world-docker-action) to it. + +2. Create a [new release](/../..releases/new) with a tag `v1` and the title `v1`. Click `Generate release notes` and publish the release. + +image + +3. Go to Settings > Actions > General > Access, and ensure `Accessible from repositories owned by the user '` is selected. + +4. Create a new repository or use another existing one and create a simple workflow that calls the action your created in version `v1`. + +
+ Solution + +```YAML +name: Test +on: [workflow_dispatch] + +jobs: + test-action: + runs-on: ubuntu-latest + steps: + - name: Say hello + uses: /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](03-Staged-deployments.md). diff --git a/hol/03-Staged-deployments.md b/hol/03-Staged-deployments.md new file mode 100644 index 0000000..686ec1f --- /dev/null +++ b/hol/03-Staged-deployments.md @@ -0,0 +1,128 @@ +# ๐Ÿ”จ Hands-on: Staged deployments + +In this hands-on lab your will create environments and a staged deployment workflow with approvals. +> Note: you can only do this on public repos for free accounts. Adding environments for `private` repos is only available for Organizations with a Team account (or higher) and users with GitHub Pro accounts. + +This hands on lab is based on [My first workflow](01-My-first-workflow.md) and adds the following steps: +- [Creating and protecting environments](#creating-and-protecting-environments) +- [Adding a input for picking environments to manual workflow trigger](#adding-a-input-for-picking-environments-to-manual-workflow-trigger) +- [Chaining workflow steps and conditional execution](#chaining-workflow-steps-and-conditional-execution) +- [Reviewing and approving deployments](#reviewing-and-approving-deployments) + +## Creating and protecting environments + +1. Go to [Settings](/../../settings) | [Environments](/../../settings/environments) and click [New environment](/../../settings/environments/new) +2. Enter the name `Production` and click `Configure environment` +3. Add yourself as the `Required reviewer` for this environment: + +image + +4. Only allow the `main`branch to be deployed to this environment: + +image + +5. Create two more environments. `Test` and `Load-Test` without any restrictions. + +## Adding a input for picking environments to manual workflow trigger + +Modify the workflow yml file you created in hands on lab 1 ('My first workflow'). +Add an input of the type environment to the `workflow_dispatch` trigger that you created previously. + +
+ Solution + +```YAML + workflow_dispatch: + inputs: + environment: + description: 'Environment to deploy to' + type: environment + required: true +``` + +
+ +## Chaining workflow steps and conditional execution + +1. Now add 3 jobs to the workflow file: + - Test: runs on `ubuntu-latest` after `Build`. Only runs when the workflow was triggered manually. Runs on the environment `Test`. The job writes `Testing...` to the workflow log. + - Load-Test: runs on `ubuntu-latest` after `Build`. Only runs when the workflow was triggered manually. Runs on the environment `Load-Test`. The job writes `Testing...` to the workflow log and sleeps for 15 seconds. + - Production: runs on `ubuntu-latest` after `Test` and `Load-Test`. Deploys to the environment `Production` onyl if this was selected as the input parameter. The environment has the URL `https://writeabout.net`. To simulate deployment, the job will execute 5 steps. Each step with writes `Step x deploying...` to the workflow log and sleeps for 10 seconds. + +
+ Solution + +```YAML + Test: + runs-on: ubuntu-latest + if: github.event_name == 'workflow_dispatch' + needs: Build + environment: Test + steps: + - run: echo "๐Ÿงช Testing..." + + Load-Test: + runs-on: ubuntu-latest + if: github.event_name == 'workflow_dispatch' + needs: Build + environment: Load-Test + steps: + - run: | + echo "๐Ÿงช Testing..." + sleep 15 + + Production: + runs-on: ubuntu-latest + needs: [Test, Load-Test] + environment: + name: Production + url: https://writeabout.net + if: github.event.inputs.environment == 'Production' + steps: + - run: | + echo "๐Ÿš€ Step 1..." + sleep 10 + - run: | + echo "๐Ÿš€ Step 2..." + sleep 10 + - run: | + echo "๐Ÿš€ Step 3..." + sleep 10 + - run: | + echo "๐Ÿš€ Step 4..." + sleep 10 + - run: | + echo "๐Ÿš€ Step 5..." + sleep 10 +``` + +
+ +2. Trigger the workflow and select `Production` as the environment: + +image + +## Reviewing and approving deployments + +1. Open the workflow run and start the review. + +image + +2. And approve it with a comment: + +image + +3. Note the progress bar while deploying... + +image + +4. The result looks like this and contains the approval and the URL for the production environment: + +image + +## Summary + +In this lab you have learned to create and protect environments in GitHub and use them in a workflow. You have also learned to conditionally +execute jobs or steps and to chain jobs using the `needs` keyword. + +You can continue with the [Reusable workflows](04-Reusable-workflows.md). diff --git a/hol/04-Reusable-workflows.md b/hol/04-Reusable-workflows.md new file mode 100644 index 0000000..419377c --- /dev/null +++ b/hol/04-Reusable-workflows.md @@ -0,0 +1,187 @@ +# ๐Ÿ”จ Hands-on: Reusable workflows + +In this hands-on lab you will create a [reusable workflow](https://docs.github.com/en/actions/using-workflows/reusing-workflows#creating-a-reusable-workflow) and a workflow that consumes it. You will learn to pass in parameters to the reusable workflow and use output parameters in the consuming workflow. + +This hands on lab consists of the following steps: +- [Creating a reusable workflow](#creating-a-reusable-workflow) +- [Adding an output parameter](#adding-an-output-parameter) +- [Consuming the reusable workflow](#consuming-the-reusable-workflow) + +## Creating a reusable workflow + +1. Create a [new file](/../../new/main) `.github/workflows/reusable.yml` (paste the file name with the path in the box). +2. Set the name to `Reusable workflow`. + +
+ Solution + +```YAML +name: Reusable workflow +``` + +
+ +3. Add a `workflow_call` trigger with an [input parameter](https://docs.github.com/en/enterprise-cloud@latest/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_call) `who-to-greet` of the type `string` that is required. Set the default value to `World`. + +
+ Solution + +```YAML +on: + workflow_call: + inputs: + who-to-greet: + description: 'The person to greet' + type: string + required: true + default: World +``` + +
+ +4. Add a job named `reusable-job` that runs on `ubuntu-latest` that echos "Hello " to the console. + +
+ Solution + +```YAML +jobs: + reusable-job: + runs-on: ubuntu-latest + steps: + - name: Greet someone + run: echo "Hello ${{ inputs.who-to-greet }}" +``` + +
+ +## Adding an output parameter + +1. Add an additional step with the id `time` that uses a workflow command to set an output parameter +named `current-time` to the current date and time (use `$(date)` for that). + +
+ Solution + +```YAML + - name: Set time + id: time + run: echo "time=$(date)" >> $GITHUB_OUTPUT +``` + +
+ +2. Add an output called `current-time` to the `reusable-job`. + +
+ Solution + +```YAML + outputs: + current-time: ${{ steps.time.outputs.time }} +``` + +
+ +3. Add an output parameter called `current-time` to `workflow_call` and set it to the outputs of the workflow command. + +
+ Solution + +```YAML + outputs: + current-time: + description: 'The time when greeting.' + value: ${{ jobs.reusable-job.outputs.current-time }} +``` + +
+ + +
+ Complete Solution + +```YAML +name: Reusable workflow + +on: + workflow_call: + inputs: + who-to-greet: + description: 'The person to greet' + type: string + required: true + default: World + outputs: + current-time: + description: 'The time when greeting.' + value: ${{ jobs.reusable-job.outputs.current-time }} + +jobs: + reusable-job: + runs-on: ubuntu-latest + outputs: + current-time: ${{ steps.time.outputs.time }} + steps: + - name: Greet someone + run: echo "Hello ${{ inputs.who-to-greet }}" + - name: Set time + id: time + run: echo "time=$(date)" >> $GITHUB_OUTPUT +``` + +
+ +## Consuming the reusable workflow + +1. Create a [new file](/../../new/main) `.github/workflows/reuse.yml` (paste the file name with the path in the box). +2. Set the name to `Reuse other workflow` and add a manual trigger. + +
+ Solution + +```YAML +name: Reuse other workflow + +on: [workflow_dispatch] +``` + +
+ +3. Add a job `call-workflow` that uses the reusable workflow and passes in your user name as an input parameter. + +
+ Solution + +```YAML +jobs: + call-workflow: + uses: ./.github/workflows/reusable.yml + with: + who-to-greet: '@octocat' +``` + +
+ +4. Add another job `use-output` that writes the output parameter `current-time` to the console. (Hint: use the needs context to access the output) + +
+ Solution + +```YAML + use-output: + runs-on: ubuntu-latest + needs: [call-workflow] + steps: + - run: echo "Time was ${{ needs.call-workflow.outputs.current-time }}" +``` + +
+ +5. Run the workflow and observe the output. + +## Summary + +In this lab you have learned to create a reusable workflow and a workflow that consumes it. You also have learned to pass in parameters to the reusable workflow and to use output parameters in the consuming workflow. + +You can continue with the [README](../README.md).