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.
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.
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.
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.
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.
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.
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.
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.
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}}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With