Skip to content

Create and publish a Docker image #314

Create and publish a Docker image

Create and publish a Docker image #314

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Create and publish a Docker image
on:
push:
branches: ['master']
pull_request:
branches: ['master']
schedule:
# postgis/postgis update schedule is '15 5 * * 1'
# https://github.com/postgis/docker-postgis/blob/master/.github/workflows/main.yml
- cron: '15 7 * * 1'
env:
REGISTRY: ghcr.io
TEST_TAG: ${{ github.repository }}:test
COMPOSE_FILE: ./docker-compose.test.yml
LATEST_TAG: 16-3.5
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
pull-requests: read
strategy:
fail-fast: false
matrix:
postgres: [16, 15, 14, 13, 12]
postgis: ['3.5']
env:
BASE_IMAGE_TAG: ${{ matrix.postgres }}-${{ matrix.postgis }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}
tags: ${{ env.BASE_IMAGE_TAG }}
- name: Build and export to Docker
uses: docker/build-push-action@v6
with:
context: .
load: true
no-cache: true
pull: true
tags: ${{ env.TEST_TAG }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BASE_IMAGE_TAG=${{ env.BASE_IMAGE_TAG }}
- name: Build test stack
env:
IMAGE_NAME: ${{ env.TEST_TAG }}
run: docker compose up --build --no-start
- name: Run tests
env:
IMAGE_NAME: ${{ env.TEST_TAG }}
run: docker compose start sut
- name: Log in to container registry
if: ${{ github.event_name != 'pull_request' }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push to registry
if: ${{ github.event_name != 'pull_request' }}
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BASE_IMAGE_TAG=${{ env.BASE_IMAGE_TAG }}
- name: Login to Docker Hub
if: ${{ github.event_name != 'pull_request' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Push to Docker Hub
if: ${{ github.event_name != 'pull_request' }}
uses: akhilerm/[email protected]
with:
src: ${{ steps.meta.outputs.tags }}
dst: |
docker.io/${{ github.repository }}:${{ env.BASE_IMAGE_TAG }}
- name: Push latest tag to both registries
if: ${{ (github.event_name != 'pull_request') && (env.BASE_IMAGE_TAG == env.LATEST_TAG) }}
uses: akhilerm/[email protected]
with:
src: ${{ steps.meta.outputs.tags }}
dst: |
${{ env.REGISTRY }}/${{ github.repository }}:latest
docker.io/${{ github.repository }}:latest
- name: Update Docker Hub description
if: ${{ github.event_name != 'pull_request' }}
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
repository: ${{ github.repository }}