-
Notifications
You must be signed in to change notification settings - Fork 0
167 lines (133 loc) · 5.96 KB
/
reusable-e2e.yml
File metadata and controls
167 lines (133 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
name: Run E2E Tests
on:
workflow_call:
inputs:
php-version:
description: 'PHP version to use'
required: true
type: string
permissions:
contents: read
jobs:
e2e:
name: Playwright E2E Tests
runs-on: [public-repo]
container:
image: ghcr.io/actions/actions-runner@sha256:1ad983536759ceec39ed75a2c8f007ca8c37b66eee35ed86f13623b29a4db97d # v2.333.0
# We use --add-host to get the host IP, and we will use socat to forward
# localhost:8889 to the host so Playwright and WordPress agree on "localhost".
options: --add-host=host.docker.internal:host-gateway
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /tmp/wp-env:/tmp/wp-env
- /tmp/playwright-cache:/tmp/playwright-cache
timeout-minutes: 30
env:
WP_ENV_PORT: 8889
WP_ENV_HOME: /tmp/wp-env
PLAYWRIGHT_BROWSERS_PATH: /tmp/playwright-cache
steps:
- name: Magic thing needed becuase of github as it bind mounts __temp
run: |
sudo apt update && sudo apt install -y acl socat
sudo setfacl -R -m u:runner:rwX /__w /github/home /github/workflow /tmp/wp-env /tmp/playwright-cache
sudo setfacl -R -d -m u:runner:rwX /__w /github/home /github/workflow /tmp/wp-env /tmp/playwright-cache
- name: Configure Docker group permissions
run: |
SOCK_GID=$(stat -c '%g' /var/run/docker.sock)
sudo groupmod -g $SOCK_GID -o docker
sudo usermod -aG $SOCK_GID runner
- name: Set up Docker Compose
uses: docker/setup-compose-action@8cccb8c14b6500aaffebff1aa49c502c34d2e5e6 # v2.1.0
with:
version: latest
cache-binary: true
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
- name: Configure Git for Safe Directory
run: git config --global --add safe.directory "${GITHUB_WORKSPACE}"
- name: Set up PHP
uses: shivammathur/setup-php@accd6127cb78bee3e8082180cb391013d204ef9f # v2.37.0
with:
php-version: ${{ inputs.php-version }}
coverage: none
- name: Install Composer dependencies
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # v4.0.0
- name: Setup Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version-file: '.nvmrc'
- name: Install npm dependencies
run: npm ci
- name: Build development assets
run: npm run build:dev
- name: Fix wp-env host mapping
run: |
# Get the host path for the workspace by inspecting the container's own mounts
HOST_WORKSPACE_PATH=$(docker inspect $(hostname) --format '{{ range .Mounts }}{{ if eq .Destination "/__w" }}{{ .Source }}{{ end }}{{ end }}')
# Construct the full host path for the current plugin directory
RELATIVE_PATH=$(pwd | sed 's|^/__w||')
PLUGIN_HOST_PATH="${HOST_WORKSPACE_PATH}${RELATIVE_PATH}"
echo "Host Workspace: $HOST_WORKSPACE_PATH"
echo "Plugin Host Path: $PLUGIN_HOST_PATH"
# Update .wp-env.test.json
jq --arg path "$PLUGIN_HOST_PATH" \
'del(.plugins) | .mappings["wp-content/plugins/plugin-skeleton-d"] = $path' \
.wp-env.test.json > .wp-env.test.json.tmp && mv .wp-env.test.json.tmp .wp-env.test.json
cat .wp-env.test.json
- name: Start the Docker testing environment
uses: nick-fields/retry@ad984534de44a9489a53aefd81eb77f87c70dc60 # v4.0.0
with:
timeout_minutes: 15
max_attempts: 3
command: npm run wp-env:test start -- --update
- name: Install Playwright dependencies
run: |
npx playwright install-deps
npx playwright install
- name: Run E2E tests
run: |
# Forward localhost traffic from the CI container to the Host machine
# where the WordPress Docker container is actually listening.
# This ensures Playwright and WordPress both agree the domain is "localhost:8889".
if ss -tlnp | grep -q ":${WP_ENV_PORT} "; then
echo "ERROR: Port ${WP_ENV_PORT} already in use"
exit 1
fi
socat TCP4-LISTEN:${WP_ENV_PORT},fork,bind=127.0.0.1 TCP4:host.docker.internal:${WP_ENV_PORT} &
SOCAT_PID=$!
sleep 2
if ! kill -0 "$SOCAT_PID" 2>/dev/null; then
echo "ERROR: socat failed to start"
exit 1
fi
echo "socat running (PID: $SOCAT_PID) on port ${WP_ENV_PORT}"
npm run test:e2e
- name: Upload E2E test results as artifact
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
if: always()
with:
name: playwright-report
path: tests/_output
retention-days: 2
if-no-files-found: ignore
- name: Stop the Docker testing environment
if: always()
run: npm run wp-env:test destroy -- --force
- name: Cleanup Workspace & Home to avoid pollution
if: always()
run: |
# Wipes the actual code, build artifacts, and hidden files
sudo rm -rf ./* .[!.]* 2>/dev/null || true
# Deletes everything inside _temp EXCEPT the locked mount points
sudo find /__w/_temp/ -mindepth 1 -maxdepth 1 ! -name '_github_home' ! -name '_github_workflow' -exec rm -rf {} +
# Empties the contents of the locked directories without deleting the folders themselves
sudo rm -rf /github/home/* || true
sudo rm -rf /github/home/.* 2>/dev/null || true
sudo rm -rf /github/workflow/* || true
sudo rm -rf /github/workflow/.* 2>/dev/null || true
# Empty the wp-env directory
sudo rm -rf /tmp/wp-env/* || true
sudo rm -rf /tmp/playwright-cache/* || true