1

I am using Github Actions for continuous delivery and building the Docker images for my application.

How can I add version information my Docker images

  • The release tag
  • Commit hash
  • Commit message
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435

1 Answers1

1

You can do this with combination of few steps

  • Read the release version from Github tags
  • Pass this and other information to Docker using build args
  • Write the build args to the files in the Dockerfile

Actions YAML

      # https://stackoverflow.com/a/58178121/315168
      - name: Scrape build info
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          # https://stackoverflow.com/questions/67051284/how-to-set-a-dockerfile-arg-in-github-actions
          # https://stackoverflow.com/a/63619526/315168
          build-args: |
            GIT_VERSION_TAG=${{ env.RELEASE_VERSION }}
            GIT_COMMIT_MESSAGE=${{ github.event.head_commit.message }}
            GIT_VERSION_HASH=${{ github.sha }}

Then in Dockerfile:

# Passed from Github Actions
ARG GIT_VERSION_TAG=unspecified
ARG GIT_COMMIT_MESSAGE=unspecified
ARG GIT_VERSION_HASH=unspecified

WORKDIR /usr/src/myapp

# You can read these files for the information in your application
RUN echo $GIT_VERSION_TAG > GIT_VERSION_TAG.txt
RUN echo $GIT_COMMIT_MESSAGE > GIT_COMMIT_MESSAGE.txt
RUN echo $GIT_VERSION_HASH > GIT_VERSION_HASH.txt
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435