PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 21, 2022

[FIXED] How to set workflow env variables depending on branch

 August 21, 2022     environment-variables, github, github-actions     No comments   

Issue

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.


Solution

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/cancel-workflow-action@0.4.0
        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}}"
       


Answered By - Thijs Koerselman
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing