Home > Mobile >  How to pass a variable from GitHub action to Docker container?
How to pass a variable from GitHub action to Docker container?

Time:01-27

When I push a commit with a tag, a docker container is automatically built by the docker/build-push-action@v1 GitHub Actions. The tag indicates the version number, which I want to show in the application.

Here is my workflow:

- name: Push to Docker Hub - develop
  uses: docker/build-push-action@v1
  with:
    repository: my_repo/my_image
    path: frontend/   
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}
    tags: develop 
    args: SOFTWARE_VERSION=${{ steps.vars.outputs.tag }}

At that moment the variable SOFTWARE_VERSION should be available in the Dockerfile, is that right?

In my Dockerfile I set:

ARG SOFTWARE_VERSION
ENV SOFTWARE_VERSION ${SOFTWARE_VERSION} 

Inside the container, the variable has an empty value. How to do it properly?

I don't know if it matters, but I run the container using docker-compose.

CodePudding user response:

Go to your Settings->Secrets and Variables

Add two Secrets

DOCKERHUB_USERNAME your username on Dockerhub DOCKERHUB_TOKEN this is the token,you can get it from DockerHub

You must add this to precede Build and Push

 — name: Login to DockerHub
   if: GitHub.event_name != 'pull_request'
   uses: docker/login-action@v1
   with:
     username: ${{ secrets.DOCKERHUB_USERNAME }}
     password: ${{ secrets.DOCKERHUB_TOKEN }}

CodePudding user response:

You are using args to pass the arguments.

However, the @v1 of https://github.com/docker/build-push-action offers build_args and @v3 build-args.

You should choose accordingly for the version that you need to use. IMO, you should go for the latest one.

  • Related