Skip to content

Commit eb1d670

Browse files
[IMP] Push PR image
1 parent cd4ae3f commit eb1d670

1 file changed

Lines changed: 79 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
permissions:
22
contents: read
33
packages: write
4+
pull-requests: write
45

56
name: Build, Test & Deploy
67

@@ -41,6 +42,10 @@ jobs:
4142
DOCKER_IMAGE_NAME: ${{ github.repository }}
4243
# Push only on non-PR events (push to main/tags, workflow_dispatch)
4344
PUSH: ${{ toJSON(github.event_name != 'pull_request') }}
45+
# Push PR image only for PRs from the same repository
46+
PUSH_PR_IMAGE:
47+
${{ toJSON(github.event_name == 'pull_request' &&
48+
github.event.pull_request.head.repo.full_name == github.repository) }}
4449
steps:
4550
# Set up Docker Environment
4651
- uses: actions/checkout@v4
@@ -89,8 +94,10 @@ jobs:
8994
echo "Actor: $GITHUB_ACTOR"
9095
echo "Event: $GITHUB_EVENT_NAME"
9196
echo "Ref: $GITHUB_REF"
97+
echo "PUSH: ${{ env.PUSH }}"
98+
echo "PR: ${{ env.PUSH_PR_IMAGE }}"
9299
- name: Login to GitHub Container Registry
93-
if: ${{ fromJSON(env.PUSH) }}
100+
if: ${{ fromJSON(env.PUSH) || fromJSON(env.PUSH_PR_IMAGE) }}
94101
uses: docker/login-action@v3
95102
with:
96103
registry: ghcr.io
@@ -125,3 +132,74 @@ jobs:
125132
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
126133
labels: ${{ steps.docker_meta_public.outputs.labels }}
127134
tags: ${{ steps.docker_meta_public.outputs.tags }}
135+
136+
- name: Docker meta for PR image
137+
if: ${{ fromJSON(env.PUSH_PR_IMAGE) }}
138+
id: docker_meta_pr
139+
uses: crazy-max/ghaction-docker-meta@v1
140+
with:
141+
images: |
142+
ghcr.io/tecnativa/docker-whitelist-gateway-service
143+
tags: |
144+
type=raw,value=pr-${{ github.event.pull_request.number }}
145+
type=raw,value=pr-${{ github.event.pull_request.number }}-${{ github.sha }}
146+
147+
- name: Build and push PR image to GHCR
148+
if: ${{ fromJSON(env.PUSH_PR_IMAGE) }}
149+
uses: docker/build-push-action@v4
150+
with:
151+
context: .
152+
file: ./Dockerfile
153+
platforms: |
154+
linux/386
155+
linux/amd64
156+
linux/arm64
157+
load: false
158+
push: true
159+
provenance: false
160+
cache-from: type=local,src=/tmp/.buildx-cache
161+
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
162+
labels: ${{ steps.docker_meta_pr.outputs.labels }}
163+
tags: ${{ steps.docker_meta_pr.outputs.tags }}
164+
165+
- name: Comment PR with test image
166+
if: ${{ fromJSON(env.PUSH_PR_IMAGE) }}
167+
uses: actions/github-script@v7
168+
with:
169+
script: |
170+
const pr = context.payload.pull_request.number;
171+
const owner = context.repo.owner;
172+
const repo = context.repo.repo;
173+
const image = `ghcr.io/tecnativa/docker-whitelist-gateway-service:pr-${pr}`;
174+
const marker = "<!-- pr-test-image-comment -->";
175+
const body = `${marker}
176+
Test image published:
177+
\`${image}\``;
178+
179+
const { data: comments } = await github.rest.issues.listComments({
180+
owner,
181+
repo,
182+
issue_number: pr,
183+
per_page: 100,
184+
});
185+
186+
const existing = comments.find(comment =>
187+
comment.user?.type === "Bot" &&
188+
comment.body?.includes(marker)
189+
);
190+
191+
if (existing) {
192+
await github.rest.issues.updateComment({
193+
owner,
194+
repo,
195+
comment_id: existing.id,
196+
body,
197+
});
198+
} else {
199+
await github.rest.issues.createComment({
200+
owner,
201+
repo,
202+
issue_number: pr,
203+
body,
204+
});
205+
}

0 commit comments

Comments
 (0)