Skip to content

Commit 0bb4bd9

Browse files
committed
New approach with pushing the images in any case and retagging them once the test is complete.
1 parent 402f2ff commit 0bb4bd9

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

.github/workflows/build-and-publish.yml

+35-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
name: Docker
22

3-
# This workflow uses actions that are not certified by GitHub.
4-
# They are provided by a third-party and are governed by
5-
# separate terms of service, privacy policy, and support
6-
# documentation.
7-
83
on:
94
schedule:
105
- cron: '18 2 * * *'
@@ -17,7 +12,6 @@ env:
1712
REGISTRY: ghcr.io
1813
IMAGE_NAME: dbsystel/postgresql-partman
1914

20-
2115
jobs:
2216
build:
2317
strategy:
@@ -40,26 +34,22 @@ jobs:
4034
permissions:
4135
contents: read
4236
packages: write
43-
# This is used to complete the identity challenge
44-
# with sigstore/fulcio when running outside of PRs.
4537
id-token: write
4638

4739
steps:
4840
- name: Checkout repository
4941
uses: actions/checkout@v4
5042

5143
# Install the cosign tool except on PR
52-
# https://github.com/sigstore/cosign-installer
5344
- name: Install cosign
5445
if: github.event_name != 'pull_request'
5546
uses: sigstore/cosign-installer@v3.4.0
5647

57-
# Workaround: https://github.com/docker/build-push-action/issues/461
48+
# Workaround for Docker buildx issue
5849
- name: Setup Docker buildx
5950
uses: docker/setup-buildx-action@v3
6051

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

7161
# Extract metadata (tags, labels) for Docker
72-
# https://github.com/docker/metadata-action
7362
- name: Extract Docker metadata
7463
id: meta
7564
uses: docker/metadata-action@v5
@@ -80,8 +69,7 @@ jobs:
8069
type=raw,value=${{ matrix.postgres_version }},enable=${{ matrix.default == 'true'}}
8170
${{ matrix.postgres_version }}-${{ matrix.major }}
8271
83-
# Build and push Docker image with Buildx (don't push on PR)
84-
# https://github.com/docker/build-push-action
72+
# Build and push Docker image with Buildx, using only the digest
8573
- name: Build and push Docker image
8674
id: build-and-push
8775
uses: docker/build-push-action@v5
@@ -92,22 +80,44 @@ jobs:
9280
PARTMAN_VERSION=${{ matrix.partman_version }}
9381
PARTMAN_CHECKSUM=${{ matrix.partman_checksum }}
9482
platforms: linux/amd64,linux/arm64
95-
push: ${{ github.event_name != 'pull_request' }}
96-
tags: ${{ steps.meta.outputs.tags }}
83+
outputs: type=docker
84+
push: true
85+
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pipeline
9786
labels: ${{ steps.meta.outputs.labels }}
9887
cache-from: type=gha
9988
cache-to: type=gha,mode=max
10089

90+
# Test the built Docker image using the digest
91+
- name: Test Docker image
92+
env:
93+
POSTGRES_PASSWORD: examplepassword
94+
run: |
95+
DIGEST=${{ steps.build-and-push.outputs.digest }}
96+
docker run -d --name test-db -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@$DIGEST
97+
sleep 30
98+
docker exec test-db pg_isready -U postgres
99+
docker stop test-db
100+
docker rm test-db
101+
102+
# Retag and push Docker image after successful tests
103+
- name: Retag and push Docker image
104+
if: github.event_name != 'pull_request'
105+
run: |
106+
DIGEST=${{ steps.build-and-push.outputs.digest }}
107+
TAGS=$(echo ${{ steps.meta.outputs.tags }} | tr ',' '\n')
108+
for TAG in $TAGS; do
109+
docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@$DIGEST ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG
110+
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG
111+
done
101112
102-
# Sign the resulting Docker image digest except on PRs.
103-
# This will only write to the public Rekor transparency log when the Docker
104-
# repository is public to avoid leaking data. If you would like to publish
105-
# transparency data ev en for private images, pass --force to cosign below.
106-
# https://github.com/sigstore/cosign
113+
# Sign the resulting Docker image digest except on PRs
107114
- name: Sign the published Docker image
108-
if: ${{ github.event_name != 'pull_request' }}
115+
if: github.event_name != 'pull_request'
109116
env:
110117
COSIGN_EXPERIMENTAL: "true"
111-
# This step uses the identity token to provision an ephemeral certificate
112-
# against the sigstore community Fulcio instance.
113-
run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign -y {}@${{ steps.build-and-push.outputs.digest }}
118+
run: |
119+
DIGEST=${{ steps.build-and-push.outputs.digest }}
120+
TAGS=$(echo ${{ steps.meta.outputs.tags }} | tr ',' '\n')
121+
for TAG in $TAGS; do
122+
cosign sign -y ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$TAG
123+
done

0 commit comments

Comments
 (0)