01 December 2024
GitHub Actions are a fantastic time saver. I’ve been using them for a while and have recently had the chance to start expanding functionality. Building and pushing Docker containers is a great place to start because I do that all the time.
If you are familiar with GitHub Actions most of the file is straightforward. If you aren’t familiar with GitHub Actions, start here.
In lines 22-40 the Maven artifactId and version are stored as variables for the build:
- name: Get artifactId from pom.xml
id: get_artifactId
run: echo "::set-output name=artifactId::$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout)"
- name: Expose artifactId as an output
run: echo "POM_ARTIFACT=$" >> $GITHUB_ENV
- name: Print artifactId
run: echo $POM_ARTIFACT
- name: Get version from pom.xml
id: get_version
run: echo "::set-output name=version::$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)"
- name: Expose version as an output
run: echo "POM_VERSION=$" >> $GITHUB_ENV
- name: Print version
run: echo $POM_VERSION
The “Get artifact/version from pom.xml” steps pull the values out of the pom.xml and set them into the environment. The “Print artifactId/version” is just logging.
The Action uses your Docker username to tag the image:
- name: Build Docker image
run: docker build --file src/main/docker/Dockerfile.jvm -t $/${POM_ARTIFACT}:${POM_VERSION} -t $/${POM_ARTIFACT}:latest .
The push to Dockerub requires your Docker username and a token.
- name: Push Docker image
run: |
echo $ | docker login -u $ --password-stdin
docker push $/${POM_ARTIFACT}:${POM_VERSION}
Your Dockerhub username and token needs to be set in “Settings > Secrets and variables > Actions.” There is a screenshot in the repo’s README.md of where to find these settings.