Skip to content

Build, test and push a Docker image #2058

Build, test and push a Docker image

Build, test and push a Docker image #2058

Workflow file for this run

name: Build, test and push a Docker image
on:
push:
branches:
- main
tags:
- v20[0-9][0-9].[01][0-9].[0-3][0-9] # e.g. v2023.12.04
- v20[0-9][0-9].[01][0-9].[0-3][0-9]-[0-9] # e.g. v2023.12.04-2
pull_request:
branches:
- main
merge_group:
jobs:
build:
permissions:
contents: read
id-token: write
runs-on: ubuntu-24.04
env:
# Disable docker compose volume mounts in docker-compose.override.yml
COMPOSE_FILE: docker-compose.yml
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
persist-credentials: false
- name: Get info
run: |
uname -v
docker info
- name: Create version.json
run: |
# create a version.json per
# https://github.com/mozilla-services/Dockerflow/blob/master/docs/version_object.md
printf '{"commit":"%s","version":"%s","source":"%s","build":"%s"}\n' \
"$GITHUB_SHA" \
"$GITHUB_REF_NAME" \
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > version.json
- name: Output version.json
run: cat version.json
- name: Install just
run: sudo apt-get update && sudo apt-get install -y just
- name: Build Docker images
run: |
just build
docker compose images
- name: Run lint check
run: |
just lint
- name: Run tests
run: |
just test
- name: Set Docker image tag to the shortened commit sha for updates of the main branch
id: image-tag-main
if: github.ref == 'refs/heads/main'
run: |
echo IMAGE_TAG=$(git rev-parse --short=10 "$GITHUB_SHA") >> "$GITHUB_OUTPUT"
- name: Set Docker image tag to the git tag for tagged builds
id: image-tag-release
if: startsWith(github.ref, 'refs/tags/')
run: |
echo IMAGE_TAG="$GITHUB_REF_NAME" >> "$GITHUB_OUTPUT"
- name: Set Docker image target name
id: target-image
if: steps.image-tag-main.outputs.IMAGE_TAG || steps.image-tag-release.outputs.IMAGE_TAG
env:
IMAGE_TAG: ${{ steps.image-tag-main.outputs.IMAGE_TAG || steps.image-tag-release.outputs.IMAGE_TAG }}
run: |
echo TARGET_IMAGE=us-docker.pkg.dev/${{ secrets.DOCKER_IMAGE_PATH }}:"${IMAGE_TAG}" >> "$GITHUB_OUTPUT"
- name: Tag Docker image
if: steps.target-image.outputs.TARGET_IMAGE
env:
TARGET_IMAGE: ${{ steps.target-image.outputs.TARGET_IMAGE }}
run: |
docker tag local/socorro_app:latest "$TARGET_IMAGE"
- name: Push the Docker image to GAR
if: steps.target-image.outputs.TARGET_IMAGE
uses: mozilla-it/deploy-actions/docker-push@f7f886627a7914358ff56dda226419e1df90ca40 # v6.5.4
with:
image_tags: ${{ steps.target-image.outputs.TARGET_IMAGE }}
workload_identity_pool_project_number: ${{ secrets.WORKLOAD_IDENTITY_POOL_PROJECT_NUMBER }}
project_id: ${{ secrets.GCP_PROJECT_ID }}