If the variable ENVIRONMENT is either dev
or qa
then I wish to have a new environment variable FINALENV
set to nonprod
else to prod
Below is my workflow file from a develop
[non-master] branch
I face 2 problems with my workflow.
Run Workflow
button to trigger the workflow manually.FINALENV
to print as non-prod
as the if
condition was met however it prints blank
.I'm new to github workflows. Kindly suggest.
---
name: CICD-RECYCLE
on:
push:
branches:
- "*"
pull_request:
branches:
- "*"
workflow_dispatch:
inputs:
ENVIRONMENT:
type: choice
options:
- dev
- qa
- perf
- uat
- prod
jobs:
SETVARS:
runs-on: self-hosted
steps:
- name: Print environment variable
run: |
echo "ENV is ${{ env.ENVIRONMENT }} only."
- name: Set environment variable for nonprod ENV
run: |
echo "FINALENV=nonprod" >> $GITHUB_ENV
if: ${{env.ENVIRONMENT == 'dev' || env.ENVIRONMENT == 'qa'}}
- name: Set environment variable for prod ENV
run: |
echo "FINALENV=prod" >> $GITHUB_ENV
if: ${{env.ENVIRONMENT != 'dev' || env.ENVIRONMENT != 'qa'}}
- name: Print environment variable for ENV
run: |
echo "FINALENV is ${{ env.FINALENV }} only."
With Windows runner, you're using Bash syntax throughout in your run
sections but the default shell on Windows runner is pwsh
. It should be shell: bash
if that is intended.
See jobs.<job_id>.steps[*].shell
for more details.
Wherever you use env.ENVIRONMENT
, it should be inputs.ENVIRONMENT
.
Also, from the job name i.e. SETVARS
, it seems like you're using it to set env vars only and most probably want to use these in your next jobs. That won't work as each job runs in a different runner. So, you need to change your strategy here.
Your current workflow may be simplified to this:
name: CICD-RECYCLE
on:
push:
pull_request:
workflow_dispatch:
inputs:
ENVIRONMENT:
type: choice
options:
- dev
- qa
- perf
- uat
- prod
jobs:
SETVARS:
runs-on: '1561'
defaults:
run:
shell: bash
steps:
- name: Set FINALENV
env:
FINALENV: ${{ (inputs.ENVIRONMENT == 'dev' || inputs.ENVIRONMENT == 'qa') && 'nonprod' || 'prod' }}
run: echo "FINALENV=$FINALENV" >> "$GITHUB_ENV"
- name: Print FINALENV
run: echo "FINALENV is $FINALENV."
Depending on your use case, you might need to perform above steps only if it's a workflow_dispatch
event:
if: ${{ github.event_name == 'workflow_dispatch' }}