Following is my simple github workflow. It is intended to print an environment variable.
name: verify
on:
workflow_dispatch:
jobs:
read_env_variables:
environment: build
runs-on: [ self-hosted, onprem_dae, docker ]
steps:
- name: cat on branch file
run: |
echo ${{ env.SOME_VARIABLE }}
I have created an environment named "build". In this environment, I have an environment variable named SOME_VARIABLE set to xyz.
When the workflow is triggered, I expected to echo value xyz but actual value is "". Is there something missing?
Your issue here is related to the syntax.
To use the ${{ env.SOME_VARIABLE }} syntax, you need to set an env variable at the workflow, job or step level.
Here is an example:
name: Environment Workflow
on:
workflow_dispatch:
env:
WORKFLOW_VARIABLE: WORKFLOW
jobs:
job1:
runs-on: ubuntu-latest
env:
JOB_VARIABLE: JOB
steps:
- name: Run Commands with various variables
if: ${{ env.WORKFLOW_VARIABLE == 'WORKFLOW' }}
env:
STEP_VARIABLE: STEP
run: |
echo "Hello World"
echo "This is the $WORKFLOW_VARIABLE environment variable"
echo "This is the $JOB_VARIABLE environment variable"
echo "This is the $STEP_VARIABLE environment variable"
Now, if you want to use the environment secrets for deployment, as explained here on the Github Documentation, the syntax would be different using the job_id.environment as you are already using following this doc.
Here is an example:
job4:
runs-on: ubuntu-latest
environment: build
steps:
- name: Show repo env secret
run: |
echo ${{ secrets.REPO_ENV_SECRET }}
Note that this variable is a secret, therefore you won't be able to see it through an echo command on the step (it will show ***)
Here is the workflow I used to validate all this implementation if you want to take a look:
workflow yaml file
workflow run
Related
I am using the getsentry/action-release#v1 GitHub Action to inform Sentry about new releases in my GitHub application. However, since I am using tags as the version number, I would like to inform sentry about the latest tag available on the release page. I'm having issues doing that while using environment variables.
Here is my job:
inform_sentry_about_release:
runs-on: ubuntu-latest
env:
ACCESS_TOKEN: ${{ secrets.GH_PAT }}
steps:
- uses: actions/checkout#v2
- name: Set GITHUB_VERSION variable
run: |
echo 'GITHUB_LATEST_RELEASE=$(curl -H "Authorization: token ${ACCESS_TOKEN}" "https://api.github.com/repos/myusername/myreponame/releases" -s | jq -r ".[0].tag_name")' >> $GITHUB_ENV
- name: Create Sentry release
uses: getsentry/action-release#v1
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
with:
environment: production
version: ${{ GITHUB_LATEST_RELEASE }}
My Github workflow is failing with the following error:
The workflow is not valid. .github/workflows/production-label.yml (Line: 131, Col: 20): Unrecognized named-value: 'GITHUB_LATEST_RELEASE'. Located at position 1 within expression: GITHUB_LATEST_RELEASE
According to How do I dynamically set an environment variable in a github composite action step?, it appears that I should be able to just echo a key=val to the GITHUB_ENV and then call it in the next step, but no luck.
Line 131 specifically refers to this:
version: ${{ GITHUB_LATEST_RELEASE }}
Is there a different way I'm supposed to access this environment variable from this line? I've tried $GITHUB_LATEST_RELEASE and still no luck with that either.
The environment variable should be accessed in the following way:
version: ${{ env.GITHUB_LATEST_RELEASE }}
Please note, that the names of environment variables are case-sensitive, and you can include punctuation.
Example:
steps:
- name: Set the value
id: step_one
run: |
echo "action_state=yellow" >> $GITHUB_ENV
- name: Use the value
id: step_two
run: |
echo "${{ env.action_state }}" # This will output 'yellow'
For more information check the Setting an environment variable.
I have a GitHub workflow which kickoff when PR is created/synchronize/reopened
name: CI Deployment
on:
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
stageA:
runs-on: ubuntu-latest
steps:
- name: Commit SHA
run: |
echo "pass"
stageB:
runs-on: ubuntu-latest
steps:
- name: Commit SHA
run: |
echo "fail"
exit 1
For the first run suppose stageB is failed. How can I get the result of stageA which is passed so I can skip that build and run only stageB.
P.S I know GitHub provided an option to re-run failed job from UI but above use case is just an example my real use case is little complicated for which I need result of previous run result so my bash script runs accordingly.
So, I'm working with Github Actions on end-to-end testing. The setup I'm looking at is having one job retrieve a list of urls to be tested, and my second job creates a matrix with that list and tests them all. My problem here is that when I actually run my testing script, it has to be done from the command line, because I'm using Playwright. Therefore I can't use my matrix object directly; I have to output it to a JSON file. The problem is that toJSON creates invalid pretty-printed JSON when I output it to my file, which breaks my script. Here's my code:
name: <name>
on:
push:
workflow_dispatch:
jobs:
fetch_strategic_urls:
runs-on: ubuntu-latest
outputs:
urls: ${{ steps.req-urls.outputs.urls }}
steps:
- name: Request Urls
id: req-urls
run: |
export RESPONSE=$(
curl -X GET -H "Accept: application/json" <api-endpoint>)
echo "::set-output name=urls::$RESPONSE"
run_tests:
runs-on: ubuntu-latest
strategy:
matrix:
url: ${{needs.fetch_strategic_urls.outputs.urls}}
needs: fetch_strategic_urls
steps:
- ...
- ...
- run: |
ls
echo '${{ toJSON(matrix.url) }}' >> props.json
cat props.json
npm test
working-directory: E2E.Tests
No matter which configuration of echo ${{matrix.url}} >> props.json I've tried (cat <<'EOF' > props.json ${{matrix.url}}, adding and removing quotes), it always produced JSON files that have no quotes, i.e.: { url: string } instead of {"url": "string"}, which is invalid. This is obviously pretty breaking behavior. I've seen a lot of people online recommending jq, but I don't see how I would use it in this case, since I doubt jq can parse a GitHub-type JSON object, which is necessary for me to use when sharding my jobs. Any help is greatly appreciated!
It's not easy to put a JSON doc directly in the command line. You can use env vars.
- shell: bash
env:
JSON_DOC: ${{ toJSON(some.var) }}
run: |
printf '%s\n' "$JSON_DOC" > foo.json
cat foo.json
...
Github actions documenation has a solution here:
name: Context testing
on: push
jobs:
dump_contexts_to_log:
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
id: github_context_step
run: echo '${{ toJSON(github) }}'
From https://docs.github.com/en/actions/learn-github-actions/contexts#example-printing-context-information-to-the-log
I have a worklow thati save in one of the jobs some outputs. in the next job i checkout the branch and try to run the a python script that accepts command line inputs.
when i try to pass the output, or rather a few outputs as command line variables, they don't register and i get and error saying sys.argv[2] out of bounds.
job1:
runs-on: self-hosted
outputs:
name: ${{steps.step1.outputs.name}}
age: ${{steps.step1.outputs.name}}
steps:
- id: step1
run: |
export NAME="name"
export AGE="1"
echo "::set-output name=name::$NAME"
echo "::set-output name=age::$AGE"
job2:
runs-on: self-hosted
steps:
-name: test
run: python app.py ${{needs.job1.outputs.name}} ${{needs.job1.outputs.age}}
You need to declare that job2 needs job1:
job2:
needs: job1
From documentation:
Job outputs are available to all downstream jobs that depend on this job.
In GitHub Actions, I'd like to evaluate a bash expression and then assign it to an environment variable:
- name: Tag image
env:
GITHUB_SHA_SHORT: ${{ $(echo $GITHUB_SHA | cut -c 1-6) }}
..do other things...
However, this naive attempt has failed. According to the docs this doesn't seem to be supported; a somewhat clean workaround would be fine.
The original answer to this question used the Actions runner function set-env. Due to a security vulnerability set-env is being deprecated and should no longer be used.
This is the new way to set environment variables.
name: my workflow
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set env
run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
- name: Test
run: echo $GITHUB_SHA_SHORT
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. Environment variables are case-sensitive and you can include punctuation.
(From https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable)
Example using the output to $GITHUB_ENV method:
echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
This is an alternative way to reference the environment variable in workflows.
- name: Test
run: echo ${{ env.GITHUB_SHA_SHORT }}
The documentation https://docs.github.com/en/free-pro-team#latest/actions/reference/environment-variables#about-environment-variables describes 2 ways of defining environment-variables.
To set custom environment variables, you need to specify the variables
in the workflow file. You can define environment variables for a step,
job, or entire workflow using the jobs.<job_id>.steps[*].env,
jobs.<job_id>.env, and env keywords.
steps:
- name: Hello world
run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
env:
FIRST_NAME: Mona
middle_name: The
Last_Name: Octocat
You can also use the GITHUB_ENV environment file to set an environment
variable that the following steps in a workflow can use. The
environment file can be used directly by an action or as a shell
command in a workflow file using the run keyword.