- Workflows:
- Are defined at the repository level
- Define which triggers actually start the workflow (defined by
on
) - Are composed of one or more jobs
- Contain jobs
- Jobs:
- Define in which execution environment they are run (defined by
run-on
) - Are contain one or more steps
- Run in parallel by default
- Contain steps
- Define in which execution environment they are run (defined by
- Steps:
- Are defined at the job level
- Define where it will be executed
- Run sequentially by default
- Contain the code that will be executed
Example of the workflow, jobs, steps
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions
on:
- push
jobs:
job1:
runs-on: ubuntu-latest
steps:
- run: echo "Step 1 in Job 1"
- run: echo "Step 2 in Job 1"
- run: echo "Step 3 in Job 1"
job2:
runs-on: ubuntu-latest
steps:
- run: echo "Step 1 in Job 2"
- run: echo "Step 2 in Job 2"
- run: echo "Step 3 in Job 2"
Practical exercise Create a new repository:
mkdir github-actions-demo
cd githab-actions-demo
git init
mkdir -p .github/workflows
cd .github/workflows
Workflow files must be created within .github/workflows
directory.
Create a new file 001-first-workflow.yaml
name: 001 - First Workflow
on:
- push
jobs:
begin-job:
runs-on: ubuntu-latest
steps:
- name: Begin bash script
run: |
echo "Workflow is starting"
echo "Location:"
pwd
finish-job:
runs-on: ubuntu-latest
steps:
- name: Finish bash script
run: |
echo "Workflow is finishing"
- name: Pre-fail step
run: |
echo "Pre-fail step"
exit 1
- name: Post-fail step
run: |
echo "Post-fail step"
This is the simple workflow with 2 jobs:
- First job has 1 step
- Second Job has 3 steps:
- The first step will be executed successfully
- The second step will be failed
- The third step will not be executed as the previous step was failed
Be aware when the step in the workflow failed - no futher steps and jobs will be executed.
Add the workflow to the repository
cd ../..
git add .github
git commit -m "Added the first workflow: 001-first-workflow.yaml"
git push
All actions you can find in GitHub Actions: https://github.com/ORG-NAME/github-actions-demo/actions
Try to change the exit code from 1
to 0
and check the workflow status
...
echo "Pre-fail step"
exit 0
- name: Post-fail step
And update the workflow in the repository
git add .github
git commit -m "Added updated first workflow: 001-first-workflow.yaml"
git push