Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set workflow env variables depending on branch

I currently have two GitHub actions workflow files that are pretty much identical, only one is configured to react to push/pull_requests on branch master the other on production.

The staging workflow starts like this:

env:
  GCLOUD_PROJECT: my-project-stg
  VERCEL_TARGET: staging

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

The production workflow starts like:

env:
  GCLOUD_PROJECT: my-project-prd
  VERCEL_TARGET: production

on:
  push:
    branches: [ production ]
  pull_request:
    branches: [ production ]

The rest of the workflow files is the same, so this is clearly not very DRY.

I would like to have 1 single workflow file and somehow switch between two sets of variables based on the branch name. Is there a way to achieve this or am I maybe approach this from the wrong angle?

If it were possible to extend both workflow files on a shared base definition that would also solve it I guess.

like image 879
Thijs Koerselman Avatar asked Jul 27 '20 14:07

Thijs Koerselman


People also ask

How do you specify environment variables as a workflow job?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

What is GITHUB_ENV?

Setting an environment variable echo "{name}={value}" >> $GITHUB_ENV. Creates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access.

Can I use variables in .ENV file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.

Is it possible to set workflow-level environment variables from a job?

It is not possible to set workflow-level environment variables from a job. Each job runs in its own machine and setting an env variable there only affects all of the later steps in that job. There are currently two ways to share data between jobs; either you create an artifact and use that, or you declare and set job outputs.

How do I set environment variables in GitHub workflow?

GitHub sets default environment variables that are available to every step in a workflow run. Environment variables are case-sensitive. Commands run in actions or steps can create, read, and modify environment variables. To set custom environment variables, you need to specify the variables in the workflow file.

How do I create a new workflow branch?

Or, learn how to create a new workflow. Click the + plus icon to add a new action. In the right panel, select If/then branch or Value equals branch. After selecting your branch type, you'll then configure the branch. In the right panel, in the Branch name field, enter a name for the branch.

How do I assign a variable to a workflow?

Open the drop-down and click +CREATE NEW VARIABLE. Select the Show on start form if you want it to be a start variable. Specify the value to assign to the selected variable. This is the value the variable uses throughout the workflow. Note: Value options depend on the variable type of the selected workflow variable.


1 Answers

It is not possible to set workflow-level environment variables from a job. Each job runs in its own machine and setting an env variable there only affects all of the later steps in that job.

There are currently two ways to share data between jobs; either you create an artifact and use that, or you declare and set job outputs. The latter works for string values.

Here's an example:

name: "Main"

on:
  push:
    branches:
      - master
      - production
  pull_request:
    branches:
      - master
      - production

jobs:
  init:
    runs-on: ubuntu-latest
    outputs:
      gcloud_project: ${{ steps.setvars.outputs.gcloud_project }}
      phase: ${{ steps.setvars.outputs.phase }}

    steps:
      - name: Cancel previous workflow
        uses: styfle/[email protected]
        with:
          access_token: ${{ github.token }}

      - name: Set variables
        id: setvars
        run: |
          if [[ "${{github.base_ref}}" == "master" || "${{github.ref}}" == "refs/heads/master" ]]; then
            echo "::set-output name=gcloud_project::my-project-dev"
            echo "::set-output name=phase::staging"
          fi

          if [[ "${{github.base_ref}}" == "production" || "${{github.ref}}" == "refs/heads/production" ]]; then
            echo "::set-output name=gcloud_project::my-project-prd"
            echo "::set-output name=phase::production"
          fi

  print:
    runs-on: ubuntu-latest
    needs: init
    steps:
      - name: Print
        run: echo "gcloud_project=${{needs.init.outputs.gcloud_project}}"
       
like image 65
Thijs Koerselman Avatar answered Sep 18 '22 13:09

Thijs Koerselman