I am trying to use an env variable in both the job and step level if condition. Whatever I do, it errors or it does not show my expected behavior. Here is my code
name: CI/CD
on:
push:
branches: [ other, stable ]
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
STABLE_BRANCH_NAME: stable
jobs:
job1:
name: test
runs-on: [ self-hosted, linux, enterprise, .... ]
container:
image: some-image
steps:
- name: echo
run: echo $BRANCH_NAME
if I am on a branch named stable
it prints stable
Then I want to add if condition. I change the job to
jobs:
job1:
name: test
runs-on: [ self-hosted, linux, enterprise, .... ]
container:
image: some-image
if: env.BRANCH_NAME == env.STABLE_BRANCH_NAME
steps:
- name: echo
run: echo $BRANCH_NAME
it fails. wrong syntax
Then I change the job if condition to if: ${{ env.BRANCH_NAME == env.STABLE_BRANCH_NAME }}
again does not work
I need two if conditions. one on the job and one on the step level like this
name: CI/CD
on:
push:
branches: [ other, stable ]
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
STABLE_BRANCH_NAME: stable
jobs:
job1:
name: test
runs-on: [ self-hosted, linux, enterprise, .... ]
if: #### need help here: check the branch name is stable - This one des not work
container:
image: some-image
steps:
- name: echo
run: echo $BRANCH_NAME
if: ${{ env.STABLE_BRANCH_NAME != env.BRANCH_NAME }}. # This one works
On the step level I can do if: ${{ env.STABLE_BRANCH_NAME != env.BRANCH_NAME }}
and it works but the same does not work on the job level
How should I do this?
Found these two
- https://github.com/actions/toolkit/issues/299
- GitHub Actions - How to use environment variable at job level?
But isn't there an easier method?