Skip to content

Commit 83fb274

Browse files
authored
ci: add workflow for deploying temporary PR environments (#423)
1 parent 9ceaf6c commit 83fb274

File tree

2 files changed

+150
-2
lines changed

2 files changed

+150
-2
lines changed

.github/workflows/deploy-pr.yml

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
name: Manage PR Temp Envs
2+
'on':
3+
pull_request:
4+
types:
5+
- labeled
6+
- unlabeled
7+
- closed
8+
9+
permissions:
10+
contents: read
11+
pull-requests: write
12+
13+
env:
14+
APP_NAME: gitingest
15+
FLUX_OWNER: '${{ github.repository_owner }}'
16+
FLUX_REPO: '${{ secrets.CR_FLUX_REPO }}'
17+
18+
jobs:
19+
deploy-pr-env:
20+
if: >-
21+
${{ github.event.action == 'labeled' && github.event.label.name ==
22+
'deploy-pr-temp-env' }}
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Create GitHub App token
26+
uses: actions/create-github-app-token@v2
27+
id: app-token
28+
with:
29+
app-id: '${{ secrets.CR_APP_CI_APP_ID }}'
30+
private-key: '${{ secrets.CR_APP_CI_PRIVATE_KEY }}'
31+
owner: '${{ env.FLUX_OWNER }}'
32+
repositories: '${{ env.FLUX_REPO }}'
33+
- name: Checkout Flux repo
34+
uses: actions/checkout@v4
35+
with:
36+
repository: '${{ env.FLUX_OWNER }}/${{ env.FLUX_REPO }}'
37+
token: '${{ steps.app-token.outputs.token }}'
38+
path: flux-repo
39+
persist-credentials: false
40+
- name: Export PR ID
41+
run: 'echo "PR_ID=${{ github.event.pull_request.number }}" >> $GITHUB_ENV'
42+
shell: bash
43+
- name: Ensure template exists
44+
run: >
45+
T="flux-repo/pr-template/${APP_NAME}"
46+
47+
[[ -d "$T" ]] || { echo "Missing $T"; exit 1; }
48+
49+
[[ $(find "$T" -type f | wc -l) -gt 0 ]] || { echo "No files in $T";
50+
exit 1; }
51+
shell: bash
52+
- name: Render & copy template
53+
run: |
54+
SRC="flux-repo/pr-template/${APP_NAME}"
55+
DST="flux-repo/deployments/prs-${APP_NAME}/${PR_ID}"
56+
mkdir -p "$DST"
57+
cp -r "$SRC/." "$DST/"
58+
find "$DST" -type f -print0 \
59+
| xargs -0 -n1 sed -i "s|@PR-ID@|${PR_ID}|g"
60+
shell: bash
61+
- name: Sanity‑check rendered output
62+
run: >
63+
E=$(find "flux-repo/pr-template/${APP_NAME}" -type f | wc -l)
64+
65+
G=$(find "flux-repo/deployments/prs-${APP_NAME}/${PR_ID}" -type f | wc
66+
-l)
67+
68+
(( G == E )) || { echo "Expected $E files, got $G"; exit 1; }
69+
shell: bash
70+
- name: Commit & push creation
71+
run: >
72+
cd flux-repo
73+
74+
git config user.name "${{ steps.app-token.outputs.app-slug }}[bot]"
75+
76+
git config user.email "${{ steps.app-token.outputs.app-slug
77+
}}[bot]@users.noreply.github.com"
78+
79+
git add .
80+
81+
git commit -m "chore(prs-${APP_NAME}): create temp env for PR #${{
82+
env.PR_ID }} [skip ci]" || echo "Nothing to commit"
83+
84+
git remote set-url origin \
85+
https://x-access-token:${{ steps.app-token.outputs.token }}@github.com/${{ env.FLUX_OWNER }}/${{ env.FLUX_REPO }}.git
86+
git push origin HEAD:main
87+
shell: bash
88+
89+
remove-pr-env:
90+
if: >-
91+
(github.event.action == 'unlabeled' && github.event.label.name ==
92+
'deploy-pr-temp-env') || (github.event.action == 'closed' &&
93+
github.event.pull_request.merged == true)
94+
runs-on: ubuntu-latest
95+
steps:
96+
- name: Create GitHub App token
97+
uses: actions/create-github-app-token@v2
98+
id: app-token
99+
with:
100+
app-id: '${{ secrets.CR_APP_CI_APP_ID }}'
101+
private-key: '${{ secrets.CR_APP_CI_PRIVATE_KEY }}'
102+
owner: '${{ env.FLUX_OWNER }}'
103+
repositories: '${{ env.FLUX_REPO }}'
104+
- name: Checkout Flux repo
105+
uses: actions/checkout@v4
106+
with:
107+
repository: '${{ env.FLUX_OWNER }}/${{ env.FLUX_REPO }}'
108+
token: '${{ steps.app-token.outputs.token }}'
109+
path: flux-repo
110+
persist-credentials: false
111+
- name: Export PR ID
112+
run: 'echo "PR_ID=${{ github.event.pull_request.number }}" >> $GITHUB_ENV'
113+
shell: bash
114+
- name: Remove deployed directory
115+
run: |
116+
DST="flux-repo/deployments/prs-${APP_NAME}/${PR_ID}"
117+
if [[ -d "$DST" ]]; then
118+
rm -rf "$DST"
119+
echo "✅ Deleted $DST"
120+
else
121+
echo "⏭️ Nothing to delete at $DST"
122+
fi
123+
shell: bash
124+
- name: Commit & push deletion
125+
run: >
126+
cd flux-repo
127+
128+
git config user.name "${{ steps.app-token.outputs.app-slug }}[bot]"
129+
130+
git config user.email "${{ steps.app-token.outputs.app-slug
131+
}}[bot]@users.noreply.github.com"
132+
133+
git add -A
134+
135+
git commit -m "chore(prs-${APP_NAME}): remove temp env for PR #${{
136+
env.PR_ID }} [skip ci]" || echo "Nothing to commit"
137+
138+
git remote set-url origin \
139+
https://x-access-token:${{ steps.app-token.outputs.token }}@github.com/${{ env.FLUX_OWNER }}/${{ env.FLUX_REPO }}.git
140+
git push origin HEAD:main
141+
shell: bash

.github/workflows/docker_image.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: Build & Push Container
2+
23
on:
34
push:
45
branches:
@@ -16,8 +17,14 @@ concurrency:
1617
env:
1718
REGISTRY: ghcr.io
1819
IMAGE_NAME: ${{ github.repository }}
19-
# Set to 'true' to allow pushing container from pull requests with the label 'push-container'
20-
PUSH_FROM_PR: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'push-container') }}
20+
# Now allow pushing from PRs when either 'push-container' OR 'deploy-pr-temp-env' is present:
21+
PUSH_FROM_PR: >-
22+
${{ github.event_name == 'pull_request' &&
23+
(
24+
contains(github.event.pull_request.labels.*.name, 'push-container') ||
25+
contains(github.event.pull_request.labels.*.name, 'deploy-pr-temp-env')
26+
)
27+
}}
2128
2229
jobs:
2330
docker-build:

0 commit comments

Comments
 (0)