Solvedstarter workflows How to define env variable?
βοΈAccepted Answer
solution
1/ Here is a great hack to use on your @github actions.
The pain is that when you set variables in a step in your workflow, further steps can NOT see the VAR
Officially you have to re-set VAR at each step. This sucks.
Hold my beer
2/ Well, that sucked for about a day. Then I had
I found a way to hack this limitation. Write your VAR on disk (the CI system disk), then cat $my_var
to use your VAR in every step you need
Other Answers:
I found en even easier way buried somewhere in the docs is this syntax:
::set-env name={name}::{value}
This is how you use it:
echo "::set-env name=APP_NAME::$(cat Dockerfile | grep APP_NAME= | head -n 1 | grep -o '".*"' | sed 's/"//g')"
I know it's weird with the echo
, but this actually works because it allows you to access the variable in subsequent steps.
EDIT:
The above actually works not just in bash with the echo
command. I found that it works with python too; so:
print("::set-env name=APP_NAME::{}".format("foo"))
Just coming here to post this current answer because there is a lot of outdated methods here.
To set an env
dynamically (i.e. inside of a job
), you need to use this pattern:
run: echo "name=value" >> $GITHUB_ENV
Latest docs can be found here.
Using set-env
is deprecated.
@deitch it is not missing anymore, it is now alive https://github.blog/changelog/2019-10-01-github-actions-new-workflow-syntax-features/#env-at-the-workflow-and-job-level
I understand this is an old issue, but as I couldn't quickly find reference to the official documentation on Environment Files, this may help clarifying the latest on this.
With the new setup, rather than using ::set-env
which is deprecated, it would look like below.
jobs:
date:
runs-on: ubuntu-latest
steps:
- name: Get date
run: |
echo "current_date=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
- name: Echo env date
run: |
echo "${{ env.current_date }}"
Also, as mentioned in the doc, you can have more complex multiline setup.
jobs:
curl:
runs-on: ubuntu-latest
steps:
- name: Get teapot
run: |
echo 'teapot<<EOF' >> $GITHUB_ENV
curl https://httpbin.org/status/418 >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Echo env teapot
run: |
echo << 'EOF'
${{ env.teapot }}
echo 'EOF'
Hi,
I worked for few hours around the CI aspect of Github actions and the big piece that is missing is about how to define variables.
my yml
what I want to do
Of course, the tag
devmtl/rclone:2.29.1-2019-08-24-32c812f7
should be dynamic. I need to:From there, I should be able to generate all the tags required.
See how I do this using Travis:
https://github.com/firepress-org/ghostfire/blob/master/.travis.yml#L45
Cheers!