Skip to content

Commit

Permalink
New approach with pushing the images in any case and retagging them o…
Browse files Browse the repository at this point in the history
…nce the test is complete.
  • Loading branch information
Morl99 committed Nov 18, 2024
1 parent 402f2ff commit 39c32e7
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
name: Docker

# 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.

on:
schedule:
- cron: '18 2 * * *'
Expand All @@ -17,7 +12,6 @@ env:
REGISTRY: ghcr.io
IMAGE_NAME: dbsystel/postgresql-partman


jobs:
build:
strategy:
Expand All @@ -40,26 +34,21 @@ jobs:
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/[email protected]

# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
Expand All @@ -69,7 +58,6 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
Expand All @@ -80,8 +68,7 @@ jobs:
type=raw,value=${{ matrix.postgres_version }},enable=${{ matrix.default == 'true'}}
${{ matrix.postgres_version }}-${{ matrix.major }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
# Build and push Docker image with Buildx, using only the digest
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
Expand All @@ -92,22 +79,43 @@ jobs:
PARTMAN_VERSION=${{ matrix.partman_version }}
PARTMAN_CHECKSUM=${{ matrix.partman_checksum }}
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pipeline
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# Test the built Docker image using the digest
- name: Test Docker image
env:
POSTGRES_PASSWORD: examplepassword
run: |
DIGEST=${{ steps.build-and-push.outputs.digest }}
docker run -d --name test-db -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@$DIGEST
sleep 30
docker exec test-db pg_isready -U postgres
docker stop test-db
docker rm test-db
# Retag and push Docker image after successful tests
- name: Retag and push Docker image
if: github.event_name != 'pull_request'
run: |
DIGEST=${{ steps.build-and-push.outputs.digest }}
TAGS=$(echo ${{ steps.meta.outputs.tags }} | tr ',' '\n')
for TAG in $TAGS; do
docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@$DIGEST ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG
done
# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data ev en for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
# Sign the resulting Docker image digest except on PRs
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
if: github.event_name != 'pull_request'
env:
COSIGN_EXPERIMENTAL: "true"
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign -y {}@${{ steps.build-and-push.outputs.digest }}
run: |
DIGEST=${{ steps.build-and-push.outputs.digest }}
TAGS=$(echo ${{ steps.meta.outputs.tags }} | tr ',' '\n')
for TAG in $TAGS; do
cosign sign -y ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG
done

0 comments on commit 39c32e7

Please sign in to comment.