Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to the pipeline before pushing the image #22

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 42 additions & 28 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,36 +34,29 @@ 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
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
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,10 +67,9 @@ 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
- name: Build and push Docker image
id: build-and-push
# Build and push Docker image with Buildx, using only the digest
- name: Build and push Docker image with digest
id: build-and-push-digest
uses: docker/build-push-action@v5
with:
context: .
Expand All @@ -92,22 +78,50 @@ 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-digest.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

# Build and push Docker image with Buildx,this time using the final tags
- name: Build and push Docker image with final tags (release)
id: build-and-push-release
if: github.event_name != 'pull_request'
uses: docker/build-push-action@v5
with:
context: .
build-args: |
POSTGRESQL_VERSION=${{ matrix.postgres_version }}
PARTMAN_VERSION=${{ matrix.partman_version }}
PARTMAN_CHECKSUM=${{ matrix.partman_checksum }}
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# 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