Skip to content

Commit 05ffd44

Browse files
authored
chore(ci): fix for external contributors (baserow#5040)
* fix: support CI for fork PRs via artifact-based image transfer Fork PRs cannot push Docker images to the org's container registry (GITHUB_TOKEN lacks packages:write for forks). This changes the CI to detect fork PRs and transfer images via GHA artifacts instead of the registry. - Build jobs detect forks and use load (local) instead of push (registry) - Images are saved as gzipped tarballs and uploaded as artifacts - Downstream jobs download and load from artifacts for fork PRs - GHA cache-to is skipped for fork PRs (no write access) For non-fork PRs, behavior is completely unchanged. * test: force is_fork=true to test artifact path REVERT THIS COMMIT before merging. * fix: add pytest.skip to flaky test test_async_start_workflow_rate_limited_runs_eventually_disable_workflow * Revert "fix: add pytest.skip to flaky test test_async_start_workflow_rate_limited_runs_eventually_disable_workflow" This reverts commit 2cd4c2e. * Revert "test: force is_fork=true to test artifact path" This reverts commit 5f5316b.
1 parent d7115ae commit 05ffd44

1 file changed

Lines changed: 125 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,26 @@ jobs:
6767
packages: write
6868
outputs:
6969
image: ${{ steps.image.outputs.full }}
70+
is_fork: ${{ steps.fork-check.outputs.is_fork }}
7071
steps:
7172
- name: Checkout code
7273
uses: actions/checkout@v4
7374

75+
- name: Check if PR is from a fork
76+
id: fork-check
77+
run: |
78+
if [[ "${{ github.event_name }}" == "pull_request" && \
79+
"${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]]; then
80+
echo "is_fork=true" >> "$GITHUB_OUTPUT"
81+
else
82+
echo "is_fork=false" >> "$GITHUB_OUTPUT"
83+
fi
84+
7485
- name: Set up Docker Buildx
7586
uses: docker/setup-buildx-action@v3
7687

7788
- name: Log in to GitHub Container Registry
89+
if: steps.fork-check.outputs.is_fork != 'true'
7890
uses: docker/login-action@v3
7991
with:
8092
registry: ${{ env.REGISTRY }}
@@ -95,15 +107,28 @@ jobs:
95107
context: .
96108
file: backend/Dockerfile
97109
target: ci
98-
push: true
110+
push: ${{ steps.fork-check.outputs.is_fork != 'true' }}
111+
load: ${{ steps.fork-check.outputs.is_fork == 'true' }}
99112
tags: ${{ steps.image.outputs.full }}
100113
cache-from: ${{ inputs.clear_cache != true && 'type=gha,scope=backend-ci' || '' }}
101-
cache-to: type=gha,scope=backend-ci,mode=max
114+
cache-to: ${{ steps.fork-check.outputs.is_fork != 'true' && 'type=gha,scope=backend-ci,mode=max' || '' }}
102115
labels: |
103116
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
104117
org.opencontainers.image.revision=${{ env.REAL_GITHUB_SHA }}
105118
org.opencontainers.image.created=${{ github.event.head_commit.timestamp }}
106119
120+
- name: Save Docker image for fork PRs
121+
if: steps.fork-check.outputs.is_fork == 'true'
122+
run: docker save ${{ steps.image.outputs.full }} | gzip > /tmp/backend-ci-image.tar.gz
123+
124+
- name: Upload Docker image artifact
125+
if: steps.fork-check.outputs.is_fork == 'true'
126+
uses: actions/upload-artifact@v4
127+
with:
128+
name: backend-ci-image
129+
path: /tmp/backend-ci-image.tar.gz
130+
retention-days: 1
131+
107132
build-frontend:
108133
name: Build Web-Frontend CI Image
109134
runs-on: ubuntu-latest
@@ -112,14 +137,26 @@ jobs:
112137
packages: write
113138
outputs:
114139
image: ${{ steps.image.outputs.full }}
140+
is_fork: ${{ steps.fork-check.outputs.is_fork }}
115141
steps:
116142
- name: Checkout code
117143
uses: actions/checkout@v4
118144

145+
- name: Check if PR is from a fork
146+
id: fork-check
147+
run: |
148+
if [[ "${{ github.event_name }}" == "pull_request" && \
149+
"${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]]; then
150+
echo "is_fork=true" >> "$GITHUB_OUTPUT"
151+
else
152+
echo "is_fork=false" >> "$GITHUB_OUTPUT"
153+
fi
154+
119155
- name: Set up Docker Buildx
120156
uses: docker/setup-buildx-action@v3
121157

122158
- name: Log in to GitHub Container Registry
159+
if: steps.fork-check.outputs.is_fork != 'true'
123160
uses: docker/login-action@v3
124161
with:
125162
registry: ${{ env.REGISTRY }}
@@ -140,15 +177,28 @@ jobs:
140177
context: .
141178
file: web-frontend/Dockerfile
142179
target: ci
143-
push: true
180+
push: ${{ steps.fork-check.outputs.is_fork != 'true' }}
181+
load: ${{ steps.fork-check.outputs.is_fork == 'true' }}
144182
tags: ${{ steps.image.outputs.full }}
145183
cache-from: ${{ inputs.clear_cache != true && 'type=gha,scope=frontend-ci' || '' }}
146-
cache-to: type=gha,scope=frontend-ci,mode=max
184+
cache-to: ${{ steps.fork-check.outputs.is_fork != 'true' && 'type=gha,scope=frontend-ci,mode=max' || '' }}
147185
labels: |
148186
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
149187
org.opencontainers.image.revision=${{ env.REAL_GITHUB_SHA }}
150188
org.opencontainers.image.created=${{ github.event.head_commit.timestamp }}
151189
190+
- name: Save Docker image for fork PRs
191+
if: steps.fork-check.outputs.is_fork == 'true'
192+
run: docker save ${{ steps.image.outputs.full }} | gzip > /tmp/frontend-ci-image.tar.gz
193+
194+
- name: Upload Docker image artifact
195+
if: steps.fork-check.outputs.is_fork == 'true'
196+
uses: actions/upload-artifact@v4
197+
with:
198+
name: frontend-ci-image
199+
path: /tmp/frontend-ci-image.tar.gz
200+
retention-days: 1
201+
152202
# ==========================================================================
153203
# LINT STAGE - Run linting on backend, frontend, and Dockerfiles
154204
# ==========================================================================
@@ -228,12 +278,24 @@ jobs:
228278
packages: read
229279
steps:
230280
- name: Log in to GitHub Container Registry
281+
if: needs.build-frontend.outputs.is_fork != 'true'
231282
uses: docker/login-action@v3
232283
with:
233284
registry: ${{ env.REGISTRY }}
234285
username: ${{ github.actor }}
235286
password: ${{ secrets.GITHUB_TOKEN }}
236287

288+
- name: Download frontend image artifact
289+
if: needs.build-frontend.outputs.is_fork == 'true'
290+
uses: actions/download-artifact@v4
291+
with:
292+
name: frontend-ci-image
293+
path: /tmp
294+
295+
- name: Load frontend image from artifact
296+
if: needs.build-frontend.outputs.is_fork == 'true'
297+
run: gunzip -c /tmp/frontend-ci-image.tar.gz | docker load
298+
237299
- name: Run frontend lint
238300
run: |
239301
docker run --rm \
@@ -340,12 +402,24 @@ jobs:
340402
--shm-size=512m
341403
steps:
342404
- name: Log in to GitHub Container Registry
405+
if: needs.build-backend.outputs.is_fork != 'true'
343406
uses: docker/login-action@v3
344407
with:
345408
registry: ${{ env.REGISTRY }}
346409
username: ${{ github.actor }}
347410
password: ${{ secrets.GITHUB_TOKEN }}
348411

412+
- name: Download backend image artifact
413+
if: needs.build-backend.outputs.is_fork == 'true'
414+
uses: actions/download-artifact@v4
415+
with:
416+
name: backend-ci-image
417+
path: /tmp
418+
419+
- name: Load backend image from artifact
420+
if: needs.build-backend.outputs.is_fork == 'true'
421+
run: gunzip -c /tmp/backend-ci-image.tar.gz | docker load
422+
349423
- name: Check backend startup
350424
run: |
351425
docker run --rm --network="${{ job.services.db.network }}" \
@@ -405,12 +479,24 @@ jobs:
405479
--shm-size=512m
406480
steps:
407481
- name: Log in to GitHub Container Registry
482+
if: needs.build-backend.outputs.is_fork != 'true'
408483
uses: docker/login-action@v3
409484
with:
410485
registry: ${{ env.REGISTRY }}
411486
username: ${{ github.actor }}
412487
password: ${{ secrets.GITHUB_TOKEN }}
413488

489+
- name: Download backend image artifact
490+
if: needs.build-backend.outputs.is_fork == 'true'
491+
uses: actions/download-artifact@v4
492+
with:
493+
name: backend-ci-image
494+
path: /tmp
495+
496+
- name: Load backend image from artifact
497+
if: needs.build-backend.outputs.is_fork == 'true'
498+
run: gunzip -c /tmp/backend-ci-image.tar.gz | docker load
499+
414500
- name: Run backend tests for group ${{ matrix.group }}
415501
run: |
416502
mkdir -p reports
@@ -464,12 +550,24 @@ jobs:
464550
shard: [1, 2, 3]
465551
steps:
466552
- name: Log in to GitHub Container Registry
553+
if: needs.build-frontend.outputs.is_fork != 'true'
467554
uses: docker/login-action@v3
468555
with:
469556
registry: ${{ env.REGISTRY }}
470557
username: ${{ github.actor }}
471558
password: ${{ secrets.GITHUB_TOKEN }}
472559

560+
- name: Download frontend image artifact
561+
if: needs.build-frontend.outputs.is_fork == 'true'
562+
uses: actions/download-artifact@v4
563+
with:
564+
name: frontend-ci-image
565+
path: /tmp
566+
567+
- name: Load frontend image from artifact
568+
if: needs.build-frontend.outputs.is_fork == 'true'
569+
run: gunzip -c /tmp/frontend-ci-image.tar.gz | docker load
570+
473571
- name: Run web-frontend tests for shard ${{ matrix.shard }}
474572
env:
475573
CI: "true"
@@ -621,12 +719,35 @@ jobs:
621719
cache-dependency-path: "e2e-tests/yarn.lock"
622720

623721
- name: Log in to GitHub Container Registry
722+
if: needs.build-backend.outputs.is_fork != 'true'
624723
uses: docker/login-action@v3
625724
with:
626725
registry: ${{ env.REGISTRY }}
627726
username: ${{ github.actor }}
628727
password: ${{ secrets.GITHUB_TOKEN }}
629728

729+
- name: Download backend image artifact
730+
if: needs.build-backend.outputs.is_fork == 'true'
731+
uses: actions/download-artifact@v4
732+
with:
733+
name: backend-ci-image
734+
path: /tmp
735+
736+
- name: Load backend image from artifact
737+
if: needs.build-backend.outputs.is_fork == 'true'
738+
run: gunzip -c /tmp/backend-ci-image.tar.gz | docker load
739+
740+
- name: Download frontend image artifact
741+
if: needs.build-frontend.outputs.is_fork == 'true'
742+
uses: actions/download-artifact@v4
743+
with:
744+
name: frontend-ci-image
745+
path: /tmp
746+
747+
- name: Load frontend image from artifact
748+
if: needs.build-frontend.outputs.is_fork == 'true'
749+
run: gunzip -c /tmp/frontend-ci-image.tar.gz | docker load
750+
630751
- name: Restore database from dump
631752
run: |
632753
echo "Restoring database from dump to container ${{ job.services.db.id }}..."

0 commit comments

Comments
 (0)