Skip to content

Commit 55147b5

Browse files
authored
Merge pull request #122 from rossjrw/integration-tests
Add automated integration testing
2 parents 954d5fe + 6d98ee3 commit 55147b5

27 files changed

+942
-47
lines changed

.github/workflows/test.yml

Lines changed: 322 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,329 @@
1-
name: Run tests
2-
on: push
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
workflow_dispatch:
9+
10+
concurrency:
11+
group: test-${{ github.ref }}
12+
cancel-in-progress: true
13+
314
jobs:
4-
test:
15+
test-unit:
16+
name: Unit tests
517
runs-on: ubuntu-latest
618
steps:
719
- name: Checkout
820
uses: actions/checkout@v4
21+
922
- name: Run tests
23+
run: bash test/run-unit-tests.sh
24+
25+
# Git-level tests verify repository state without waiting for Pages deployment
26+
test-git-lifecycle:
27+
name: "[Git] Lifecycle"
28+
runs-on: ubuntu-latest
29+
env:
30+
TEST_ID: test-git-lifecycle
31+
TEST_UMBRELLA_DIR: pr-preview-run-${{ github.run_id }}
32+
TEST_DEPLOY_REPO: rozzjrw/pr-preview-action-testing
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v4
36+
37+
- name: Modify fixture
38+
run: sed -i.bak 's/<p id="testid">.*<\/p>/<p id="testid">${{ env.TEST_ID }}-INITIALISED<\/p>/' test/fixtures/html/index.html
39+
40+
- name: Deploy
41+
uses: ./
42+
with:
43+
pr-number: ${{ env.TEST_ID }}
44+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
45+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
46+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
47+
preview-branch: ${{ env.TEST_ID }}
48+
source-dir: test/fixtures/html
49+
action: deploy
50+
comment: false
51+
wait-for-pages-deployment: false
52+
git-config-name: GitHub Actions
53+
git-config-email: actions@github.com
54+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
55+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
56+
57+
- name: Verify deployed
58+
env:
59+
GITHUB_TOKEN: ${{ secrets.TEST_DEPLOY_TOKEN }}
60+
DEPLOY_REPO: ${{ env.TEST_DEPLOY_REPO }}
61+
PREVIEW_BRANCH: ${{ env.TEST_ID }}
62+
UMBRELLA_DIR: ${{ env.TEST_UMBRELLA_DIR }}
63+
run: bash test/integration/test-git-lifecycle.sh "deployed" "${{ env.TEST_ID }}-INITIALISED"
64+
65+
- name: Modify fixture
66+
run: sed -i.bak 's/<p id="testid">.*<\/p>/<p id="testid">${{ env.TEST_ID }}-REDEPLOYED<\/p>/' test/fixtures/html/index.html
67+
68+
- name: Redeploy
69+
uses: ./
70+
with:
71+
pr-number: ${{ env.TEST_ID }}
72+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
73+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
74+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
75+
preview-branch: ${{ env.TEST_ID }}
76+
source-dir: test/fixtures/html
77+
action: deploy
78+
comment: false
79+
wait-for-pages-deployment: false
80+
git-config-name: GitHub Actions
81+
git-config-email: actions@github.com
82+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
83+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
84+
85+
- name: Verify redeployed
86+
env:
87+
GITHUB_TOKEN: ${{ secrets.TEST_DEPLOY_TOKEN }}
88+
DEPLOY_REPO: ${{ env.TEST_DEPLOY_REPO }}
89+
PREVIEW_BRANCH: ${{ env.TEST_ID }}
90+
UMBRELLA_DIR: ${{ env.TEST_UMBRELLA_DIR }}
91+
run: bash test/integration/test-git-lifecycle.sh "deployed" "${{ env.TEST_ID }}-REDEPLOYED"
92+
93+
- name: Remove
94+
uses: ./
95+
with:
96+
pr-number: ${{ env.TEST_ID }}
97+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
98+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
99+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
100+
preview-branch: ${{ env.TEST_ID }}
101+
action: remove
102+
comment: false
103+
wait-for-pages-deployment: false
104+
git-config-name: GitHub Actions
105+
git-config-email: actions@github.com
106+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
107+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
108+
109+
- name: Verify removed
110+
env:
111+
GITHUB_TOKEN: ${{ secrets.TEST_DEPLOY_TOKEN }}
112+
DEPLOY_REPO: ${{ env.TEST_DEPLOY_REPO }}
113+
PREVIEW_BRANCH: ${{ env.TEST_ID }}
114+
UMBRELLA_DIR: ${{ env.TEST_UMBRELLA_DIR }}
115+
run: bash test/integration/test-git-lifecycle.sh "removed"
116+
117+
- name: Cleanup
118+
if: failure()
119+
run: |
120+
echo "Action removal failed or was incomplete, performing manual cleanup..."
121+
source test/lib/cleanup-test-deployment.sh
122+
cleanup_deployment "${{ env.TEST_DEPLOY_REPO }}" "${{ env.TEST_ID }}" "${{ secrets.TEST_DEPLOY_TOKEN }}" "${{ env.TEST_UMBRELLA_DIR }}"
123+
124+
test-git-custom-umbrella:
125+
name: "[Git] Custom umbrella"
126+
runs-on: ubuntu-latest
127+
env:
128+
TEST_ID: test-git-custom-umbrella
129+
TEST_UMBRELLA_DIR: pr-preview-run-${{ github.run_id }}
130+
CUSTOM_UMBRELLA_DIR: custom-previews-run-${{ github.run_id }}
131+
TEST_DEPLOY_REPO: rozzjrw/pr-preview-action-testing
132+
steps:
133+
- name: Checkout
134+
uses: actions/checkout@v4
135+
136+
- name: Modify fixture
137+
run: sed -i 's/<p id="testid">.*<\/p>/<p id="testid">${{ env.TEST_ID }}<\/p>/' test/fixtures/html/index.html
138+
139+
- name: Deploy
140+
uses: ./
141+
with:
142+
pr-number: ${{ env.TEST_ID }}
143+
umbrella-dir: ${{ env.CUSTOM_UMBRELLA_DIR }}
144+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
145+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
146+
preview-branch: ${{ env.TEST_ID }}
147+
source-dir: test/fixtures/html
148+
action: deploy
149+
comment: false
150+
wait-for-pages-deployment: false
151+
git-config-name: GitHub Actions
152+
git-config-email: actions@github.com
153+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
154+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
155+
156+
- name: Verify
157+
env:
158+
GITHUB_TOKEN: ${{ secrets.TEST_DEPLOY_TOKEN }}
159+
DEPLOY_REPO: ${{ env.TEST_DEPLOY_REPO }}
160+
PREVIEW_BRANCH: ${{ env.TEST_ID }}
161+
TEST_UMBRELLA_DIR: ${{ env.TEST_UMBRELLA_DIR }}
162+
CUSTOM_UMBRELLA_DIR: ${{ env.CUSTOM_UMBRELLA_DIR }}
163+
run: bash test/integration/test-git-custom-umbrella.sh
164+
165+
- name: Cleanup
166+
if: always()
167+
run: |
168+
source test/lib/cleanup-test-deployment.sh
169+
cleanup_deployment "${{ env.TEST_DEPLOY_REPO }}" "${{ env.TEST_ID }}" "${{ secrets.TEST_DEPLOY_TOKEN }}" "${{ env.TEST_UMBRELLA_DIR }}" "${{ env.CUSTOM_UMBRELLA_DIR }}"
170+
171+
test-git-concurrent:
172+
name: "[Git] Concurrency"
173+
runs-on: ubuntu-latest
174+
env:
175+
TEST_ID: test-git-concurrent
176+
TEST_UMBRELLA_DIR: pr-preview-run-${{ github.run_id }}
177+
TEST_DEPLOY_REPO: rozzjrw/pr-preview-action-testing
178+
steps:
179+
- name: Checkout
180+
uses: actions/checkout@v4
181+
182+
- name: Modify fixture
183+
run: sed -i.bak 's/<p id="testid">.*<\/p>/<p id="testid">${{ env.TEST_ID }}-1<\/p>/' test/fixtures/html/index.html
184+
185+
- name: Deploy PR 1
186+
uses: ./
187+
with:
188+
pr-number: ${{ env.TEST_ID }}-1
189+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
190+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
191+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
192+
preview-branch: ${{ env.TEST_ID }}
193+
source-dir: test/fixtures/html
194+
action: deploy
195+
comment: false
196+
wait-for-pages-deployment: false
197+
git-config-name: GitHub Actions
198+
git-config-email: actions@github.com
199+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
200+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
201+
202+
- name: Modify fixture
203+
run: sed -i.bak 's/<p id="testid">.*<\/p>/<p id="testid">${{ env.TEST_ID }}-2<\/p>/' test/fixtures/html/index.html
204+
205+
- name: Deploy PR 2
206+
uses: ./
207+
with:
208+
pr-number: ${{ env.TEST_ID }}-2
209+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
210+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
211+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
212+
preview-branch: ${{ env.TEST_ID }}
213+
source-dir: test/fixtures/html
214+
action: deploy
215+
comment: false
216+
wait-for-pages-deployment: false
217+
git-config-name: GitHub Actions
218+
git-config-email: actions@github.com
219+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
220+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
221+
222+
- name: Modify fixture
223+
run: sed -i.bak 's/<p id="testid">.*<\/p>/<p id="testid">${{ env.TEST_ID }}-2-redeployed<\/p>/' test/fixtures/html/index.html
224+
225+
- name: Deploy PR 2 (again)
226+
uses: ./
227+
with:
228+
pr-number: ${{ env.TEST_ID }}-2
229+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
230+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
231+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
232+
preview-branch: ${{ env.TEST_ID }}
233+
source-dir: test/fixtures/html
234+
action: deploy
235+
comment: false
236+
wait-for-pages-deployment: false
237+
git-config-name: GitHub Actions
238+
git-config-email: actions@github.com
239+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
240+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
241+
242+
- name: Verify
243+
env:
244+
GITHUB_TOKEN: ${{ secrets.TEST_DEPLOY_TOKEN }}
245+
DEPLOY_REPO: ${{ env.TEST_DEPLOY_REPO }}
246+
PREVIEW_BRANCH: ${{ env.TEST_ID }}
247+
UMBRELLA_DIR: ${{ env.TEST_UMBRELLA_DIR }}
248+
run: bash test/integration/test-git-concurrent.sh
249+
250+
- name: Cleanup
251+
if: always()
252+
run: |
253+
source test/lib/cleanup-test-deployment.sh
254+
cleanup_deployment "${{ env.TEST_DEPLOY_REPO }}" "${{ env.TEST_ID }}" "${{ secrets.TEST_DEPLOY_TOKEN }}" "${{ env.TEST_UMBRELLA_DIR }}"
255+
256+
# Pages deployment test runs after git tests pass
257+
# This is the only test that uses the actual Pages branch and waits for deployment
258+
test-pages-lifecycle:
259+
name: "[Pages] Lifecycle"
260+
needs:
261+
[test-git-custom-umbrella, test-git-lifecycle, test-git-concurrent]
262+
runs-on: ubuntu-latest
263+
env:
264+
TEST_ID: test-pages-lifecycle
265+
TEST_UMBRELLA_DIR: pr-preview-run-${{ github.run_id }}
266+
TEST_DEPLOY_REPO: rozzjrw/pr-preview-action-testing
267+
TEST_DEPLOY_REPO_BRANCH: main
268+
steps:
269+
- name: Checkout
270+
uses: actions/checkout@v4
271+
272+
- name: Modify fixture
273+
run: sed -i.bak 's/<p id="testid">.*<\/p>/<p id="testid">${{ env.TEST_ID }}<\/p>/' test/fixtures/html/index.html
274+
275+
- name: Deploy and wait
276+
uses: ./
277+
with:
278+
pr-number: ${{ env.TEST_ID }}
279+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
280+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
281+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
282+
preview-branch: ${{ env.TEST_DEPLOY_REPO_BRANCH }}
283+
source-dir: test/fixtures/html
284+
action: deploy
285+
comment: false
286+
wait-for-pages-deployment: true
287+
git-config-name: GitHub Actions
288+
git-config-email: actions@github.com
289+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
290+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
291+
292+
- name: Verify
293+
env:
294+
GITHUB_TOKEN: ${{ secrets.TEST_DEPLOY_TOKEN }}
295+
DEPLOY_REPO: ${{ env.TEST_DEPLOY_REPO }}
296+
PREVIEW_BRANCH: ${{ env.TEST_DEPLOY_REPO_BRANCH }}
297+
UMBRELLA_DIR: ${{ env.TEST_UMBRELLA_DIR }}
298+
run: bash test/integration/test-pages-lifecycle.sh "deployed"
299+
300+
- name: Remove
301+
uses: ./
302+
with:
303+
pr-number: ${{ env.TEST_ID }}
304+
umbrella-dir: ${{ env.TEST_UMBRELLA_DIR }}
305+
token: ${{ secrets.TEST_DEPLOY_TOKEN }}
306+
deploy-repository: ${{ env.TEST_DEPLOY_REPO }}
307+
preview-branch: ${{ env.TEST_DEPLOY_REPO_BRANCH }}
308+
action: remove
309+
comment: false
310+
wait-for-pages-deployment: true
311+
git-config-name: GitHub Actions
312+
git-config-email: actions@github.com
313+
deploy-commit-message: deploy ${{ env.TEST_ID }} ${{ github.run_id }}
314+
remove-commit-message: remove ${{ env.TEST_ID }} ${{ github.run_id }}
315+
316+
- name: Verify removed
317+
env:
318+
GITHUB_TOKEN: ${{ secrets.TEST_DEPLOY_TOKEN }}
319+
DEPLOY_REPO: ${{ env.TEST_DEPLOY_REPO }}
320+
PREVIEW_BRANCH: ${{ env.TEST_DEPLOY_REPO_BRANCH }}
321+
UMBRELLA_DIR: ${{ env.TEST_UMBRELLA_DIR }}
322+
run: bash test/integration/test-pages-lifecycle.sh "removed"
323+
324+
- name: Cleanup
325+
if: failure()
10326
run: |
11-
set -e
12-
for testscript in test/test-*.sh; do
13-
bash $testscript
14-
done
327+
echo "Action removal failed or was incomplete, performing manual cleanup..."
328+
source test/lib/cleanup-test-deployment.sh
329+
cleanup_deployment "${{ env.TEST_DEPLOY_REPO }}" "${{ env.TEST_ID }}" "${{ secrets.TEST_DEPLOY_TOKEN }}" "${{ env.TEST_UMBRELLA_DIR }}"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The following input parameters are provided, which can be passed to the `with` p
8383
| `deploy-repository` | The repository to deploy the preview to. <br> **Note:** The `token` parameter must also be set if changing this from the default. <br><br> Default: The pull request's target repository. |
8484
| `preview-branch` | Branch to save previews to. This should be the same branch that your GitHub Pages site is deployed from. <br><br> Default: `gh-pages` |
8585
| `umbrella-dir` | Path to the directory to place previews in. <br> The umbrella directory is used to namespace previews from your main branch's deployment on GitHub Pages. <br><br> Default: `pr-preview` |
86+
| `pr-number` | The PR number to use for the preview path. <br> Useful for testing or when the workflow is not triggered by a pull_request event. <br><br> Default: The PR number (`${{ github.event.number }}`) |
8687
| `pages-base-url` | Base URL to use when providing a link to the preview site. <br><br> Default: The pull request's target repository's default GitHub Pages URL (e.g. `rossjrw.github.io/pr-preview-action/`) |
8788
| `pages-base-path` | Path that GitHub Pages is being served from, as configured in your repository settings, e.g. `docs/`. When generating the preview URL path, this is removed from the beginning of the file path. <br><br> Default: `.` (repository root) |
8889
| `wait-for-pages-deployment` <br> (boolean) | Whether to wait for the GitHub Pages deployment to complete. When enabled, the action will poll the GitHub Deployments API and delay workflow completion until the Pages deployment finishes, e.g. to ensure the preview URL is accessible when the comment is posted. <br><br> Default: `false` (this will be `true` in a future version of this Action) |

action.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ inputs:
3535
description: Directory containing files to deploy.
3636
required: false
3737
default: .
38+
pr-number:
39+
description: >
40+
The PR number to use for the preview path. Defaults to the PR number from the GitHub event context. Override this for testing or when not triggered by a pull_request event.
41+
42+
required: false
43+
default: ""
3844
deploy-repository:
3945
description: >
4046
The GitHub repository to deploy the preview to. This should be formatted like `<org name>/<repo name>`, e.g. `rossjrw/pr-preview-action`. Defaults to the current repository.
@@ -126,9 +132,9 @@ runs:
126132
umbrella_path: ${{ inputs.umbrella-dir }}
127133
pages_base_url: ${{ inputs.pages-base-url }}
128134
pages_base_path: ${{ inputs.pages-base-path }}
129-
pr_number: ${{ github.event.number }}
130-
github_action_ref: ${{ github.action_ref }}
131-
github_action_repository: ${{ github.action_repository }}
135+
pr_number: ${{ inputs.pr-number || github.event.number }}
136+
github_action_ref: ${{ github.action_ref || github.ref }}
137+
github_action_repository: ${{ github.action_repository || github.repository }}
132138
deployment_repository: ${{ inputs.deploy-repository }}
133139
token: ${{ inputs.token }}
134140
deprecated_custom_url: ${{ inputs.custom-url }}

lib/determine-auto-action.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env bash
22

33
# Determine deployment action based on GitHub event
4-
# Usage: determine_auto_action "$GITHUB_EVENT_NAME" "$GITHUB_EVENT_PATH"
54
# Returns: "deploy", "remove", or "none"
65
determine_auto_action() {
7-
local event_name="${1:-$GITHUB_EVENT_NAME}"
8-
local event_path="${2:-$GITHUB_EVENT_PATH}"
6+
local event_name="${1:?missing event_name}"
7+
local event_path="${2:?missing event_path}"
98

109
case $event_name in
1110
"pull_request" | "pull_request_target")

lib/main.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fi
2727

2828
if [ "$deployment_action" = "auto" ]; then
2929
echo >&2 "Determining auto action"
30-
deployment_action=$(determine_auto_action)
30+
deployment_action=$(determine_auto_action "$GITHUB_EVENT_NAME" "$GITHUB_EVENT_PATH")
3131
echo >&2 "Auto action is $deployment_action"
3232
fi
3333

0 commit comments

Comments
 (0)