Skip to content

Commit 1ddb039

Browse files
committed
Switched to using GHA artifacts for PRs from forks
1 parent aba302a commit 1ddb039

File tree

1 file changed

+66
-14
lines changed

1 file changed

+66
-14
lines changed

.github/workflows/docker-build.yml

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ jobs:
5757
org.opencontainers.image.vendor=TryGhost
5858
maintainer=TryGhost
5959
60+
- name: Determine build strategy
61+
id: strategy
62+
run: |
63+
IS_FORK_PR="false"
64+
if [ "${{ github.event_name }}" = "pull_request" ] && [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
65+
IS_FORK_PR="true"
66+
fi
67+
echo "is-fork-pr=$IS_FORK_PR" >> $GITHUB_OUTPUT
68+
echo "should-push=${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}" >> $GITHUB_OUTPUT
69+
echo "should-load=$IS_FORK_PR" >> $GITHUB_OUTPUT
70+
71+
echo "Build strategy determined:"
72+
echo "- Is fork PR: $IS_FORK_PR"
73+
echo "- Should push: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}"
74+
echo "- Should load: $IS_FORK_PR"
75+
6076
- name: Display Docker metadata
6177
run: |
6278
echo "Generated tags:"
@@ -72,9 +88,9 @@ jobs:
7288
context: .
7389
file: .docker/Dockerfile
7490
# Only push on main branch or PRs from the main repo (not forks)
75-
push: ${{ github.event_name != 'pull_request' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository) }}
91+
push: ${{ steps.strategy.outputs.should-push }}
7692
# Only load image on PRs from forks (to allow testing without pushing)
77-
load: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}
93+
load: ${{ steps.strategy.outputs.should-load }}
7894
tags: ${{ steps.meta.outputs.tags }}
7995
labels: ${{ steps.meta.outputs.labels }}
8096
# On PRs: use both main cache and PR-specific cache
@@ -83,12 +99,29 @@ jobs:
8399
type=registry,ref=${{ steps.cache.outputs.image }}:cache-main
84100
${{ github.event_name == 'pull_request' && format('type=registry,ref={0}:cache-pr-{1}', steps.cache.outputs.image, github.event.pull_request.number) || '' }}
85101
# Only export cache if we can push (not on fork PRs)
86-
cache-to: ${{ (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) && format('type=registry,ref={0}:cache-{1},mode=max', steps.cache.outputs.image, github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || 'main') || '' }}
102+
cache-to: ${{ steps.strategy.outputs.should-push == 'true' && format('type=registry,ref={0}:cache-{1},mode=max', steps.cache.outputs.image, github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || 'main') || '' }}
103+
104+
- name: Save image as artifact (fork PR)
105+
if: steps.strategy.outputs.is-fork-pr == 'true'
106+
run: |
107+
IMAGE_TAG=$(echo "${{ steps.meta.outputs.tags }}" | head -n1)
108+
echo "Saving image: $IMAGE_TAG"
109+
docker save "$IMAGE_TAG" | gzip > docker-image.tar.gz
110+
echo "Image saved as docker-image.tar.gz"
111+
ls -lh docker-image.tar.gz
112+
113+
- name: Upload image artifact (fork PR)
114+
if: steps.strategy.outputs.is-fork-pr == 'true'
115+
uses: actions/upload-artifact@v4
116+
with:
117+
name: docker-image
118+
path: docker-image.tar.gz
119+
retention-days: 1
87120

88121
outputs:
89122
image-tags: ${{ steps.meta.outputs.tags }}
90123
image-digest: ${{ steps.build.outputs.digest }}
91-
is-fork: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}
124+
is-fork: ${{ steps.strategy.outputs.is-fork-pr }}
92125
test:
93126
needs: build
94127
runs-on: ubuntu-latest-16-cores
@@ -97,6 +130,20 @@ jobs:
97130
- name: Set up Docker Buildx
98131
uses: docker/setup-buildx-action@v3
99132

133+
- name: Download image artifact (fork PR)
134+
if: needs.build.outputs.is-fork == 'true'
135+
uses: actions/download-artifact@v4
136+
with:
137+
name: docker-image
138+
139+
- name: Load image from artifact (fork PR)
140+
if: needs.build.outputs.is-fork == 'true'
141+
run: |
142+
echo "Loading Docker image from artifact..."
143+
gunzip -c docker-image.tar.gz | docker load
144+
echo "Available images after load:"
145+
docker images
146+
100147
- name: Log in to GitHub Container Registry
101148
if: needs.build.outputs.is-fork == 'false'
102149
uses: docker/login-action@v3
@@ -119,21 +166,26 @@ jobs:
119166
echo "Pulling image from registry..."
120167
docker pull ${{ steps.image.outputs.tag }}
121168
122-
- name: List available images (fork PR)
123-
if: needs.build.outputs.is-fork == 'true'
124-
run: |
125-
echo "Images loaded locally:"
126-
docker images
127-
echo "Looking for image: ${{ steps.image.outputs.tag }}"
128-
129169
- name: Verify image is available
130170
run: |
131-
if docker inspect ${{ steps.image.outputs.tag }} > /dev/null 2>&1; then
132-
echo "✅ Image ${{ steps.image.outputs.tag }} is available"
171+
IMAGE_TAG="${{ steps.image.outputs.tag }}"
172+
echo "Checking for image: $IMAGE_TAG"
173+
174+
if docker inspect "$IMAGE_TAG" > /dev/null 2>&1; then
175+
echo "✅ Image $IMAGE_TAG is available"
176+
echo "Image details:"
177+
docker inspect "$IMAGE_TAG" --format='{{.Id}} {{.Created}}'
133178
else
134-
echo "❌ Image ${{ steps.image.outputs.tag }} is not available"
179+
echo "❌ Image $IMAGE_TAG is not available"
135180
echo "Available images:"
136181
docker images
182+
183+
# For fork PRs, the image might have been loaded with a different tag
184+
if [ "${{ needs.build.outputs.is-fork }}" = "true" ]; then
185+
echo ""
186+
echo "Searching for any loaded Ghost images..."
187+
docker images --format "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}" | grep -E "(ghost|Ghost)" || echo "No Ghost images found"
188+
fi
137189
exit 1
138190
fi
139191

0 commit comments

Comments
 (0)