13

In my Docker project's repo, I have a VERSION file that contains nothing more than the version number.

1.2.3 

In Travis, I'm able to cat the file to an environment variable, and use that to tag my build before pushing to Docker Hub.

--- env: global: - USER=username - REPO=my_great_project - VERSION=$(cat VERSION) 

What is the equivalent of that in GitHub Actions? I tried this, but it's not working.

name: Test on: ... ... env: USER: username REPO: my_great_project jobs: build_ubuntu: name: Build Ubuntu runs-on: ubuntu-latest env: BASE: ubuntu steps: - name: Check out the codebase uses: actions/checkout@v2 - name: Build the image run: | VERSION=$(cat VERSION) docker build --file ${BASE}/Dockerfile --tag ${USER}/${REPO}:${VERSION} . build_alpine: name: Build Alpine runs-on: ubuntu-latest env: BASE: alpine ... ... ... 

I've also tried this, which doesn't work.

- name: Build the image run: | echo "VERSION=$(cat ./VERSION)" >> $GITHUB_ENV docker build --file ${BASE}/Dockerfile --tag ${USER}/${REPO}:${VERSION} . 
5
  • Do you need VERSION in the environment, or do you just need it as the value of the VERSION variable in the docker build command? Commented Nov 6, 2020 at 21:33
  • I just need it to use docker build, docker tag, and docker push Commented Nov 6, 2020 at 21:36
  • If you don't need in in your environment, you could use VERSION=$(< VERSION) and then just use $VERSION in the same run step. What you did in your first attempt, basically. How is that not working? Commented Nov 7, 2020 at 0:07
  • Ugh, I'm an idiot. It was a different step that was failing when I was running what I had. Commented Nov 7, 2020 at 1:33
  • 1
    It seems I do need VERSION in the environment. I'm trying to use it in multiple steps, and don't want to set it every time. Is there a way to add it for all steps? Commented Nov 7, 2020 at 2:03

2 Answers 2

22

I went down the road that Benjamin W. was talking about with having VERSION in my environment vs just in that specific step.

This worked for me to set the variable in one step, then use it in separate steps.

- name: Set variables run: | VER=$(cat VERSION) echo "VERSION=$VER" >> $GITHUB_ENV - name: Build Docker Image uses: docker/build-push-action@v2 with: context: . file: ${{ env.BASE_DIR }}/Dockerfile load: true tags: | ${{ env.USER }}/${{ env.REPO }}:${{ env.VERSION }} ${{ env.USER }}/${{ env.REPO }}:latest 
Sign up to request clarification or add additional context in comments.

Comments

1

As I want to re-use ENV_VAR between jobs, this is how I do it. I wish I could find a way to minimize this code.

In this example, I use VARs from my Dockerfile. But it will work from any file.

 pre_build: runs-on: ubuntu-20.04 steps: ... - name: Save variables to disk run: | cat $(echo ${{ env.DOCKERFILE }}) | grep DOCKERHUB_USER= | head -n 1 | grep -o '".*"' | sed 's/"//g' > ~/varz/DOCKERHUB_USER cat $(echo ${{ env.DOCKERFILE }}) | grep GITHUB_ORG= | head -n 1 | grep -o '".*"' | sed 's/"//g' > ~/varz/GITHUB_ORG cat $(echo ${{ env.DOCKERFILE }}) | grep GITHUB_REGISTRY= | head -n 1 | grep -o '".*"' | sed 's/"//g' > ~/varz/GITHUB_REGISTRY echo "$(cat ~/varz/DOCKERHUB_USER)/$(cat ~/varz/APP_NAME)" > ~/varz/DKR_PREFIX - name: Set ALL variables for this job | à la sauce GitHub Actions run: | echo "VERSION_HASH_DATE=$(cat ~/varz/VERSION_HASH_DATE)" >> $GITHUB_ENV echo "VERSION_HASH_ONLY=$(cat ~/varz/VERSION_HASH_ONLY)" >> $GITHUB_ENV echo "VERSION_CI=$(cat ~/varz/VERSION_CI)" >> $GITHUB_ENV echo "VERSION_BRANCH=$(cat ~/varz/VERSION_BRANCH)" >> $GITHUB_ENV - name: Show variables run: | echo "${{ env.VERSION_HASH_DATE }} < VERSION_HASH_DATE" echo "${{ env.VERSION_HASH_ONLY }} < VERSION_HASH_ONLY" echo "${{ env.VERSION_CI }} < VERSION_CI" echo "${{ env.VERSION_BRANCH }} < VERSION_BRANCH" - name: Upload variables as artifact uses: actions/upload-artifact@master with: name: variables_on_disk path: ~/varz test_build: needs: [pre_build] runs-on: ubuntu-20.04 steps: ... - name: Job preparation | Download variables from artifact uses: actions/download-artifact@master with: name: variables_on_disk path: ~/varz - name: Job preparation | Set variables for this job | à la sauce GitHub Actions run: | echo "VERSION_HASH_DATE=$(cat ~/varz/VERSION_HASH_DATE)" >> $GITHUB_ENV echo "VERSION_HASH_ONLY=$(cat ~/varz/VERSION_HASH_ONLY)" >> $GITHUB_ENV echo "VERSION_BRANCH=$(cat ~/varz/VERSION_BRANCH)" >> $GITHUB_ENV echo "BRANCH_NAME=$(cat ~/varz/BRANCH_NAME)" >> $GITHUB_ENV 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.