Customization Techniques

Customization Techniques

GitHub Actions allow you to customize your workflows to meet the unique needs of your application and team.

Using Variables in Workflow

GitHub Actions include default environment variables for each workflow run. If you need to use custom environment variables, you can set these in your YAML workflow file.

Example:

jobs:
  example-job:
      steps:
        - name: Connect to PostgreSQL
          run: node client.js
          env:
            POSTGRES_HOST: postgres
            POSTGRES_PORT: 5432

In this example, we create custom variables named POSTGRES_HOST and POSTGRES_PORT. These variables are then available to the node client.js script.

Add Scripts to Workflow

You can use actions to run scripts and shell commands, which are then executed on the assigned runner.

Example: use the run keyword to execute npm install -g bats on the runner.

jobs:
  example-job:
    steps:
      - run: npm install -g bats

To run a script as an action, you can store the script in your repository and supply the path and shell type. Example:

jobs:
  example-job:
    steps:
      - name: Run build script
        run: ./.github/scripts/build.sh
        shell: bash

Sharing Data between Jobs

If your job generates files that you want to share with another job in the same workflow, or if you want to save the files for later reference, you can store them in GitHub as artifacts.

Artifacts are the files created when you build and test your code.

  • Might include binary or package files, test results, screenshots, or log files.
  • Are associated with the workflow run where they were created and can be used by another job.

All actions and workflows called within a run have write access to that run’s artifacts.

Example: create a file and then upload it as an artifact

jobs:
  example-job:
    name: Save output
    steps:
      - shell: bash
        run: | expr 1 + 1 > output.log
      - name: Upload output file
        uses: actions/upload-artifact@v3
        with:
          name: output-log-file
          path: output.log

To download an artifact from a separate workflow run, you can use the actions/download-artifact action. To download an artifact from the same workflow run, your download job should specify needs: upload-job-name so it doesn’t start until the upload job finishes.

Example: download the artifact named output-log-file.

jobs:
  example-job:
    steps:
      - name: Download a single artifact
        uses: actions/download-artifact@v3
        with:
          name: output-log-file

Reference