Close
AlgoliaLogoLight
Close full mode
logo

GitHub Actions fundamentals

Git RepositoryEdit on Github

Reference for Workflow syntax (YAML)

Workflow commands for GitHub Actions

Setting an environment variable

  • We can define environment variables at workflow level, job level and step level.
  • We can set environment variable with a workflow command in jobs.<job_id>.steps[*].run e.g.
- run: |
# literal value
echo "action_state=yellow" >> $GITHUB_ENV
# value from a secret
echo "SLACK_WEBHOOK=${{ secrets.SLACK_WEBHOOK }}" >> $GITHUB_ENV

Context and expression syntax for GitHub Actions

# Expression syntax
${{ <expression> }}
if: ${{ <expression> }}
if: <expression> # You can omit {{ }}
# Read context value
${{ <context> }}
${{ env.VARIABLE_NAME }}

Useful predefined contexts

  • github.sha The commit SHA that triggered the workflow run.
  • github.ref The branch or tag ref that triggered the workflow run.

Environment variables

  • https://docs.github.com/en/actions/reference/environment-variables
  • To use the value of an environment variable in a workflow file, you should use the env context.
  • If you want to use the value of an environment variable inside a runner, you can use the runner operating system's normal method for reading environment variables. e.g. \$VARIABLE_NAME
# Use environment with GitHub `env` context and normal operation system as $ENVIRONMENT_VARIABLE
env:
FIRST_NAME: Mona
DAY_OF_WEEK: Mona
if: env.DAY_OF_WEEK == 'Mon'
run: echo "Hello $FIRST_NAME at ${{ env.DAY_OF_WEEK }}"

Run job on a specific repository only

jobs:
job_id:
name: Job name
if: github.repository == username/repository-name

Conditional set environment variables

Ternary workflow expression

${{ github.ref == env.MAIN_BRANCH && secrets.PRO_PUBLISH_PROFILE || secrets.DEV_PUBLISH_PROFILE }}

Run a job based on a condition with if expression

Find more virtual environments

An action

  • An action is unit of code.
  • You can use an action defined in the same repository as the workflow, a public repository, or in a published Docker container image.
  • Version of the action:
    • Using the commit SHA of a released action version is the safest for stability and security.
    • Using the specific major action version allows you to receive critical fixes and security patches while still maintaining compatibility. It also assures that your workflow should still work.
    • Using the default branch of an action may be convenient, but if someone releases a new major version with a breaking change, your workflow could break.
  • More details https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsuses
steps:
# Reference the major version of a release
- uses: actions/setup-[email protected]

Known issues:

env:
Deploy: 'false'
jobs:
prejob:
runs-on: ubuntu-latest
outputs:
envvalue: ${{ steps.setvar.outputs.envvar }}
steps:
- name: set value
id: setvar
run: |
echo "::set-output name=envvar::$Deploy" # get top level env and set it as output
envcheck:
needs: [prejob]
if: ${{needs.prejob.outputs.envvalue=='false'}} # check env value in job level expression now.
runs-on: [ubuntu-latest]
steps:

You can use a step's name with step's uses

Specific branch name pattern to trigger an action

  • Match on a specific branch
on:
push:
branches:
- '*' # matches every branch that doesn't contain a '/'
- '*/*' # matches every branch containing a single '/'
- '**' # matches every branch
- '!master' # excludes master
  • Match all branches
on: push
on: [push, pull_request]
  • Ignore branches
on:
push:
branches-ignore:
- master

Path, useful for monorepo

Retention period of GitHub Actions

Change a working directory

  • To all steps, at workflow level:
defaults:
run:
working-directory: ./temp
  • At job level:
jobs:
job1:
defaults:
run:
working-directory: ./temp
  • At step level:
- name: Clean temp directory
run: rm -rf *
working-directory: ./temp

Yarn is already installed on Ubuntu runner

Change a default shell

  • You can use default run to set default shell to all steps
  • It can be at workflow level (defaults.run) or job level (jobs.<job_id>.defaults.run).
defaults:
run:
shell: powershell

How to apt-get install in a GitHub Actions workflow?

Loading comments...