I want to use environment variables at the job level. Is there a way to do it?
env:
stageEnv: UAT
jobs:
name: Upload Build
if: ${{ env.stageEnv == 'UAT' }}
steps:
....
I get unrecognized named-value: 'env' error. Tried $stageEnv and ${{ env.stageEnv }}
Note: It works when I access within 'steps', but would like this to be accessible at 'jobs' level.
I'm afraid not, but you can do like this:
env:
stageEnv: UAT
jobs:
build:
name: Build
runs-on: ubuntu-latest
outputs:
stageEnv: ${{ steps.init.outputs.stageEnv }}
steps:
- name: Make environment variables global
id: init
run: |
echo "stageEnv=${{ env.stageEnv }}" >> $GITHUB_OUTPUT
And use it in another job:
upload:
name: Upload build
needs: build
if: ${{ needs.build.outputs.stageEnv == 'UAT' }}
Note this is just an example, and I personally prefer environment variables uppercase and output variables lowercase
Related
We have two runners, one for running production jobs and another for running non production jobs, but I am unable to do that using a workflow level environment variable.
Below is what I have:
name: Workflow file
on:
workflow-dispatch
env:
RUNNER_NAME: ${{ contains(github.ref, 'main') && 'Prod Runner' || 'non-Prod Runner' }}
jobs:
job-run:
runs-on: [${{ env.RUNNER_NAME }}]
needs: ...
steps:
..........
I get the following error message:
Invalid workflow file
You have an error in your yaml syntax on line ###
How do I do this? I don't want to have separate workflow files for prod and non-prod workflows.
For what you can check on this Github Actions ISSUE, it seems it's not possible to use natively env variable on the runs-on job field (yet?).
However, there is a workaround if you configure a variable as output in a previous job, so you would be able to use it afterwards.
Example: runs-on: ${{ needs.setup.outputs.runner }}
In your case, the workflow would look like this:
on:
workflow_dispatch:
jobs:
setup:
runs-on: ubuntu-latest
outputs:
runner: ${{ steps.step1.outputs.runner }}
steps:
- name: Check branch
id: step1
run: |
if [ ${{ github.ref }} == 'refs/heads/main' ]; then
echo "::set-output name=runner::ubuntu-latest"
else
echo "::set-output name=runner::macos-latest"
fi
job1:
needs: [setup]
runs-on: ${{ needs.setup.outputs.runner }}
steps:
- run: echo "My runner is ${{ needs.setup.outputs.runner }}" #ubuntu-latest if main branch
I've made a test here if you want to have a look:
workflow file
workflow run
You can set env vars available across the entire workflow e.g. like in this post.
(From solution on linked post)
name: Git Pull Request Workflow
on:
workflow_dispatch:
pull_request:
branches:
- master
env:
one: 1
two: zwei
three: tres
jobs:
first-job:
runs-on: ubuntu-latest
steps:
- run: |
echo "${{ env.one }}"
echo "${{ env.two }}"
echo "${{ env.three }}"
I have a workflow that uses a matrix strategy and I have to update it in each job if I ever change it. I tried to make it a global variable like above:
name: Model Multipliers
on:
push:
branches:
- main
env:
FRUIT: ["Apple", "Pear", "Banana", "Orange"]
jobs:
ssql-get:
runs-on: ubuntu-latest
strategy:
matrix:
FRUIT: ${{ env.FRUIT }}
name: Get data
steps:
- name: Checkout cum-rev repo
But this gives error:
The workflow is not valid. .github/workflows/main.yml (Line: 12, Col:
9): A sequence was not expected .github/workflows/main.yml (Line: 19,
Col: 15): Unrecognized named-value: 'env'. Located at position 1
within expression: env.FRUIT
Is what I'm trying to do possible by any other means?
If you're using bash, you can create a regular array (like in bash) to use in your steps or inside github expressions -
Example:
env:
MAIN_BRANCHES: ("develop" "main")
Now you can use the array in any step. In the run property, as an environment variable with "${MAIN_BRANCHES}", or inside if conditions with Github expression syntax.
...
- name: Tag build
if: ${{ github.event_name == 'push' && contains(env.MAIN_BRANCHES, steps.calculate_changed_services.outputs.diff_dest) }}
run: echo "my main branches ${MAIN_BRANCHES}"
...
You can find the full Workflow File here - gitversion.yml
if you remove the double quotes in the values it should work
This is my action that returns $TOXENV that looks like this py3.6-django2.2 I'd like to $TOXENV to look like this instead py36-django22 is there any substitute/replace function that I could use to replace . char?
name: CI
on:
workflow_dispatch:
branches: [ master, actions ]
jobs:
demo:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.6, 3.7, 3.8, 3.9]
django: ['2.2', '3.0']
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v1
name: Set up Python ${{ matrix.python }} ${{ matrix.django }}
with:
python-version: ${{ matrix.python }}
- name: python version
env:
TOXENV: "py${{ matrix.python }}-django${{ matrix.django }}"
run:
echo $TOXENV
Another way by using BASH native variable substitution:
- name: python version
env:
TOXENV: "py${{ matrix.python }}-django${{ matrix.django }}"
run: |
TOXENV=${{ env.TOXENV }}
TOXENV=${TOXENV//.} # replace all dots
echo TOXENV=${TOXENV} >> $GITHUB_ENV # update GitHub ENV vars
- name: print env
run: echo ${{ env.TOXENV }}
The idea is to read the GitHub actions expression variable into a BASH variable and do the string manipulation then export or set-output to update in GitHub actions runtime.
I don't think there's an easy way to do this in the env directive of your step when defining the value of TOXENV. The env directive accepts expressions, but the functions that can be used in expressions are limited, with nothing that can replace arbitrary characters. The closest I could find is format(), but that unfortunately requires numbered braces in the target string, which won't work for your situation.
Instead, perhaps you could set the value of TOXENV in the run directive using sed, then add it to the environment:
- name: python version
run:
RAW_TOXENV="py${{ matrix.python }}-django${{ matrix.django }}"
TOXENV=$(echo $RAW_TOXENV | sed 's/\.//')
echo "TOXENV=$TOXENV" >> $GITHUB_ENV
I'm trying to make a GitHub Actions workflow where one job has a dynamic value to its environment setting. https://docs.github.com/en/actions/reference/environments I have two jobs in this workflow. The first job determines which environment the second job will run in, and this is in turn based on which git branch the Actions job is run from.
This is my naive attempt to make it work, but I get an error which says
(Line: 29, Col: 18): Unrecognized named-value: 'needs'. Located at position 1 within expression: needs.get-environment.outputs.environment_name
name: Environments
on:
push:
workflow_dispatch:
jobs:
get-environment:
runs-on: ubuntu-latest
outputs:
environment_name: ${{ steps.get_environment.outputs.environment_name }}
steps:
- id: get_environment
run: |
if [ "$GITHUB_REF" = "refs/heads/test" ]
then
echo "::set-output name=environment_name::test"
elif [ "$GITHUB_REF" = "refs/heads/qa" ]
then
echo "::set-output name=environment_name::qa"
elif [ "$GITHUB_REF" = "refs/heads/master" ]
then
echo "::set-output name=environment_name::production"
fi
use-environment:
runs-on: ubuntu-latest
needs: [get-environment]
environment: ${{ needs.get-environment.outputs.environment_name }}
steps:
- uses: actions/checkout#v2
- name: Run a one-line script
run: echo ${{ secrets.ENV_DEPENDENT_SECRETÂ }}
Is it possible to achieve what I'm trying to do? My end goal is to have a single workflow file for three different app environments (test, QA and prod), where each app environment uses a separate Actions environment. (terminology gets confusing, I know)
I actually had to solve this issue for the place I work. You can use a matrix with the list of your env names and dynamically set the environment name. Take a look at the link for the workflow file.
Have you tried
> use-environment:
> runs-on: ubuntu-latest
> needs: [get-environment]
> environment:
> name: ${{ needs.get-environment.outputs.environment_name }}
I execute a main-action with an entrypoint.sh bash script that returns an environment variable named MATRIX_LIST that looks like this [value1,value2,value3] and then I transfer it to the next job (second_job) as output.
The problem is I want the strategy.matrix within the second_job to run as the number of ${{ needs.first_job.outputs.MATRIX_LIST }} but it does not work and return me this error
Error when evaluating 'strategy' for job 'second_job'.
(Line: 44, Col: 19): Unexpected value '[value1,value2,value3]'
jobs:
first_job:
runs-on: ubuntu-latest
name: Return the MATRIX_LIST environment variable
outputs:
MATRIX_LIST: ${{ env.MATRIX_LIST }}
steps:
- name: Checkout Local Repository
uses: actions/checkout#v1
- name: Main Action Step (Set MATRIX_LIST into GITHUB_ENV)
uses: ./.github/actions/main-action
with:
first-key: ${{ secrets.KEY }}
second_job:
runs-on: ubuntu-latest
name: Trigger the matrix job N times depending to the MATRIX_LIST output variable
needs: [first_job]
strategy:
matrix:
services: ${{ needs.first_job.outputs.MATRIX_LIST }}
steps:
- name: Print the value of service name ${{ matrix.service }}
run: |
echo "Hello: ${{ matrix.service }}"
Is it possible to set an outputs variable within GitHub's index/list expression syntax?
Updated (entrypoint.sh Bash Script):
function Convert_Array_To_Matrix_Index_List()
{
services_array=(value1,value2,value3)
for SERVICE in ${services_array[#]}; do
second_array+=("${SERVICE}")
done
MATRIX_LIST=`(IFS=,; echo "[${second_array[*]}]")`
echo "MATRIX_LIST=${MATRIX_LIST}" >> $GITHUB_ENV
}