Skip to content

Commit 213912e

Browse files
committed
ci: 👷 preview app test
1 parent 2f19fcc commit 213912e

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
name: Deploy preview environment
2+
3+
concurrency:
4+
group: preview-app-deploy-${{ github.event_name }}-${{ github.ref_name || github.event.ref || github.event.pull_request.head.ref }}
5+
cancel-in-progress: true
6+
7+
on:
8+
push:
9+
branches-ignore:
10+
- main
11+
- next
12+
pull_request:
13+
types: [closed]
14+
delete:
15+
16+
jobs:
17+
deploy-ui:
18+
runs-on: ubuntu-latest
19+
if: github.event_name == 'push'
20+
permissions:
21+
contents: read
22+
issues: write
23+
pull-requests: read
24+
25+
env:
26+
DOCKER_BUILDKIT: 1
27+
KAMAL_REGISTRY_LOGIN_SERVER: ${{ secrets.KAMAL_REGISTRY_LOGIN_SERVER }}
28+
KAMAL_REGISTRY_USERNAME: ${{ secrets.KAMAL_REGISTRY_USERNAME }}
29+
KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }}
30+
KAMAL_SERVER_IP: ${{ secrets.KAMAL_SERVER_IP }}
31+
KAMAL_APP_NAME: ${{ secrets.KAMAL_APP_NAME }}
32+
KAMAL_APP_DOMAIN: ${{ secrets.KAMAL_APP_DOMAIN }}
33+
34+
steps:
35+
- uses: actions/checkout@v4
36+
37+
- name: Set preview app name and domain
38+
run: |
39+
BRANCH_SLUG=$(echo "${{ github.ref_name }}" \
40+
| tr '[:upper:]' '[:lower:]' \
41+
| sed 's/[^a-z0-9-]/-/g' \
42+
| sed 's/-\+/-/g' \
43+
| sed 's/^-//;s/-$//')
44+
45+
APP_NAME="${{ secrets.KAMAL_APP_NAME }}-${BRANCH_SLUG}"
46+
APP_DOMAIN="${APP_NAME}.${{ secrets.KAMAL_APP_DOMAIN }}"
47+
48+
echo "KAMAL_APP_NAME=${APP_NAME}" >> "$GITHUB_ENV"
49+
echo "KAMAL_APP_DOMAIN=${APP_DOMAIN}" >> "$GITHUB_ENV"
50+
51+
- uses: ruby/setup-ruby@v1
52+
with:
53+
ruby-version: 3.3.1
54+
bundler-cache: true
55+
56+
- run: gem install kamal
57+
58+
- uses: webfactory/ssh-agent@v0.9.0
59+
with:
60+
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
61+
62+
- name: Show resolved Kamal config
63+
run: kamal config -d preview
64+
65+
- run: kamal deploy -d preview
66+
67+
- name: Comment preview URL on PR
68+
if: success()
69+
uses: actions/github-script@v7
70+
with:
71+
github-token: ${{ secrets.GITHUB_TOKEN }}
72+
script: |
73+
const branch = '${{ github.ref_name }}';
74+
const previewUrl = 'https://${{ env.KAMAL_APP_DOMAIN }}';
75+
76+
const { data: prs } = await github.rest.pulls.list({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
state: 'open',
80+
head: `${context.repo.owner}:${branch}`
81+
});
82+
83+
const body = `## Preview deployment ready\n\n**Preview URL:** [${previewUrl}](${previewUrl})`;
84+
85+
for (const pr of prs) {
86+
const { data: comments } = await github.rest.issues.listComments({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
issue_number: pr.number
90+
});
91+
92+
const previewComment = comments.find(
93+
c => c.user.type === 'Bot' && c.body.startsWith('## Preview deployment ready')
94+
);
95+
96+
if (previewComment) {
97+
await github.rest.issues.updateComment({
98+
owner: context.repo.owner,
99+
repo: context.repo.repo,
100+
comment_id: previewComment.id,
101+
body
102+
});
103+
} else {
104+
await github.rest.issues.createComment({
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
issue_number: pr.number,
108+
body
109+
});
110+
}
111+
}
112+
113+
remove-preview:
114+
runs-on: ubuntu-latest
115+
if: |
116+
(github.event_name == 'pull_request' &&
117+
github.event.action == 'closed' &&
118+
!contains(fromJson('["main","next"]'), github.event.pull_request.head.ref)) ||
119+
(github.event_name == 'delete' &&
120+
github.event.ref_type == 'branch' &&
121+
!contains(fromJson('["main","next"]'), github.event.ref))
122+
permissions:
123+
contents: read
124+
issues: write
125+
pull-requests: read
126+
127+
env:
128+
DOCKER_BUILDKIT: 1
129+
KAMAL_REGISTRY_LOGIN_SERVER: ${{ secrets.KAMAL_REGISTRY_LOGIN_SERVER }}
130+
KAMAL_REGISTRY_USERNAME: ${{ secrets.KAMAL_REGISTRY_USERNAME }}
131+
KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }}
132+
KAMAL_SERVER_IP: ${{ secrets.KAMAL_SERVER_IP }}
133+
KAMAL_APP_NAME: ${{ secrets.KAMAL_APP_NAME }}
134+
KAMAL_APP_DOMAIN: ${{ secrets.KAMAL_APP_DOMAIN }}
135+
136+
steps:
137+
- uses: actions/checkout@v4
138+
139+
- name: Set branch name for remove
140+
id: branch
141+
run: |
142+
if [ "${{ github.event_name }}" = "pull_request" ]; then
143+
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
144+
else
145+
BRANCH_NAME="${{ github.event.ref }}"
146+
fi
147+
echo "branch=${BRANCH_NAME}" >> "$GITHUB_OUTPUT"
148+
149+
- name: Set preview app name and domain
150+
run: |
151+
BRANCH_SLUG=$(echo "${{ steps.branch.outputs.branch }}" \
152+
| tr '[:upper:]' '[:lower:]' \
153+
| sed 's/[^a-z0-9-]/-/g' \
154+
| sed 's/-\+/-/g' \
155+
| sed 's/^-//;s/-$//')
156+
157+
APP_NAME="${{ secrets.KAMAL_APP_NAME }}-${BRANCH_SLUG}"
158+
APP_DOMAIN="${APP_NAME}.${{ secrets.KAMAL_APP_DOMAIN }}"
159+
160+
echo "KAMAL_APP_NAME=${APP_NAME}" >> "$GITHUB_ENV"
161+
echo "KAMAL_APP_DOMAIN=${APP_DOMAIN}" >> "$GITHUB_ENV"
162+
163+
- uses: ruby/setup-ruby@v1
164+
with:
165+
ruby-version: 3.3.1
166+
bundler-cache: true
167+
168+
- run: gem install kamal
169+
170+
- uses: webfactory/ssh-agent@v0.9.0
171+
with:
172+
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
173+
174+
- run: kamal remove -y -d preview
175+
176+
- name: Comment deployment removed on PR
177+
if: github.event_name == 'pull_request' && success()
178+
uses: actions/github-script@v7
179+
with:
180+
github-token: ${{ secrets.GITHUB_TOKEN }}
181+
script: |
182+
await github.rest.issues.createComment({
183+
owner: context.repo.owner,
184+
repo: context.repo.repo,
185+
issue_number: context.payload.pull_request.number,
186+
body: '## Preview deployment removed\n\nThe preview environment for this branch has been removed.'
187+
});

0 commit comments

Comments
 (0)