Solvedstarter workflows How to define env variable?

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

name: Docker build CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@master

      - name: Build Docker image
        run: docker build --file Dockerfile --tag devmtl/rclone:2.29.1-2019-08-24-32c812f7 .

      - name: Test docker official
        run: |
          git clone https://github.com/docker-library/official-images.git official-images
          official-images/test/run.sh devmtl/rclone:2.29.1-2019-08-24-32c812f7

what I want to do

Of course, the tag devmtl/rclone:2.29.1-2019-08-24-32c812f7 should be dynamic. I need to:

  • generate the date
  • find the hash from the actual commit

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!

38 Answers

βœ”οΈ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 😎.

3/ Take a look at my advanced YAML

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.

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'

Ref: Example setup with the above YAMLs

Related Issues:

107
starter workflows How to define env variable?
solution 1/ Here is a great hack to use on your @github actions The pain is that when you set variab...
13
starter workflows Use GITHUB_TOKEN to auth in docker.pkg.github.com
I can confirm that this works for me: Try to publish docker container to docker.pkg.github.com I got...
14
super linter Cannot find rules files - FATAL
You can use actions from docker image to lock to a specific version: Describe the bug A clear and co...
3
super linter FILTER_REGEX_INCLUDE seems not to work
I confirm that with latest version it works fine for me πŸ‘ Describe the bug I added the FILTER_REGEX...