GitHub Actions πŸ™

Hero image for GitHub Actions πŸ™

Formatting

  • Add a line break between each root-level block.

    name: Build
    on: push
    env:
      DOCKER_IMAGE: $
    
    name: Build
    
    on: push
    
    env:
      DOCKER_IMAGE: $
    
  • Add a line break between each job step.

    steps:
      - uses: actions/[email protected]
      - name: Login to DockerHub
        uses: docker/[email protected]
    
    steps:
      - uses: actions/[email protected]
    
      - name: Login to DockerHub
        uses: docker/[email protected]
    

Naming

  • Name workflow files following the pattern verb_environment.yml. environment can be omitted if the workflow does not vary per environment.

    β”œβ”€β”€ ci.yml
    β”œβ”€β”€ run_all_tests.yml
    β”œβ”€β”€ prod_deploy.yml
    β”œβ”€β”€ docker_test.yml
    
    β”œβ”€β”€ test.yml
    β”œβ”€β”€ deploy.yml
    β”œβ”€β”€ deploy_production.yml
    β”œβ”€β”€ deploy_staging.yml
    β”œβ”€β”€ test_docker_build.yml
    
  • The name attribute for job must be provided as this is the value which will be used in the web UI.

    jobs:
      publish:
        runs-on: ubuntu-latest
    
    jobs:
      publish:
        name: Publish Docker Image
        runs-on: ubuntu-latest
    
  • The name attribute jobs set must be provided and follow the pattern β€œVerb description”. Start with a verb so there is a clear expectation of what the step is for.

    - run: echo "SEGMENT_JS_KEY=$SEGMENT_JS_KEY" >> .env
    
    - uses: docker/[email protected]
    
    - name: Add environments variables to .env file
      run: echo "SEGMENT_JS_KEY=$SEGMENT_JS_KEY" >> .env
    
    - name: Login to DockerHub
      uses: docker/[email protected]
    

Syntax

  • In the step block with run, prefer using a command with single line format.

    - name: All using commands
      run: |
        echo "command no.1" >> .env
    
    - name: All using commands
      run: echo "command no.1" >> .env
    

    And for multi-line commands, prefer using the literal block scalar | which preserves new lines and trailing spaces.

    - name: All using commands
      run: |
        echo "command no.1" >> .env
        echo "command no.2" >> .env
        echo "command no.3" >> .env
    

Timeout

  • Prefer to set a timeout for each job.

    jobs:
      build:
        name: Publish Docker Image
    
        runs-on: ubuntu-latest
    
        ...
    
      publish:
        name: Publish Docker Image
    
        runs-on: ubuntu-latest
    
        ...
    
    jobs:
      build:
        name: Publish Docker Image
    
        runs-on: ubuntu-latest
    
        timeout-minutes: 10
    
        ...
    
      publish:
        name: Publish Docker Image
    
        runs-on: ubuntu-latest
    
        timeout-minutes: 5
    
        ...
    

The team maintains a repository of reusable GitHub Actions workflows. Check it out before creating a new workflow.