Skip to content

Github actions basic

Posted on:June 4, 2024 at 07:30 AM

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:

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