Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.
In this lab, you will explore the basics of YAML syntax and learn how it is used to define workflows in GitHub Actions. By analyzing and modifying a workflow file, you will understand key YAML structures such as lists, mappings, anchors, and flow control elements.
Estimated Duration: 20-30 minutes
Copy the content of the starter file:
name: Intro - YAML Syntax
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/intro-yaml-syntax.yml'
.github/workflows
and name it intro-yaml-syntax.yml
.main
branch.intro-yaml-syntax.yml
file.Replace the file’s content with the following YAML configuration:
name: YAML Syntax
on:
workflow_dispatch:
push:
branches:
- main
jobs:
initial:
runs-on: ubuntu-latest
steps:
- run: echo "This job will be run first."
fanout1:
runs-on: ubuntu-latest
needs: initial
steps:
- run: echo "This job will run after the initial job, in parallel with fanout2."
fanout2:
runs-on: ubuntu-latest
needs: initial
steps:
- run: echo "This job will run after the initial job, in parallel with fanout1."
fanout3:
runs-on: ubuntu-latest
needs: fanout1
steps:
- run: echo "This job will run after the initial job, in parallel with fanout2."
fanin:
runs-on: ubuntu-latest
needs: [fanout1, fanout2]
steps:
- run: echo "This job will run after fanout1 and fanout2 have finished."
build:
runs-on: ubuntu-latest
strategy:
matrix:
configuration: [debug, release]
steps:
- run: echo "This job builds the configuration $."
test:
runs-on: ubuntu-latest
needs: build
steps:
- run: echo "This job will be run after the build job."
ring01:
runs-on: ubuntu-latest
needs: test
steps:
- run: echo "This job will be run after the test job."
ring02:
runs-on: macos-latest
needs: test
steps:
- run: echo "This job will be run after the test job."
ring03:
runs-on: ubuntu-latest
needs: test
steps:
- run: echo "This job will be run after the test job."
ring04:
runs-on: ubuntu-latest
needs: [ring01, ring02, ring03]
steps:
- run: echo "This job will be run after the ring01, ring02, and ring03 jobs."
prod:
runs-on: ubuntu-latest
needs: [ring04]
steps:
- run: echo "This job will be run after the ring04 job."
main
branch.Key-Value Pairs:
For example:
name: YAML Syntax
name
is the key, and YAML Syntax
is its value.Lists:
-
to indicate items.Example:
branches:
- main
Mappings:
Use nested keys for defining structure:
jobs:
initial:
runs-on: ubuntu-latest
Anchor and Alias:
Reduce duplication by referencing values:
common-steps: &default-steps
- run: echo "Step reused!"
Multi-line Strings:
Use |
for multi-line values:
run: |
echo "Line 1"
echo "Line 2"
Triggers (on
):
workflow_dispatch
: Manual trigger.push
: Trigger on changes pushed to the repository.Jobs and Dependencies:
needs
specifies the dependency chain.fanin
depends on fanout1
and fanout2
.Matrix Strategy:
Allows running jobs with multiple configurations:
matrix:
configuration: [debug, release]
needs
) are executed sequentially, while independent jobs run in parallel.Add a new job to the workflow. For example:
experiment:
runs-on: ubuntu-latest
needs: fanout3
steps:
- run: echo "This job will run after fanout3."
Modify the matrix in the build
job to include another configuration:
configuration: [debug, release, staging]
Commit the changes and observe the results in the Actions tab.
In this lab, you:
This lab provided a comprehensive introduction to YAML and its usage in defining GitHub workflows.