-
Notifications
You must be signed in to change notification settings - Fork 30
129 lines (110 loc) · 4.66 KB
/
playground-preview.yml
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
# For now (!) the name here is important and should not be changed!
#
# The plugin-proxy.php does a check against exact this workflow name.
name: Playground Preview
# Inspired by, but not based on https://github.com/WordPress/gutenberg/blob/b89fb7b6eaf619bde0269e2a7fbf6245822f6cbf/.github/workflows/build-plugin-zip.yml#L153
on:
# What and Why pull_request_target?
# https://github.com/GatherPress/gatherpress/pull/666#issuecomment-2143088443
#
# The GitHub Actions built in environment variables and Context
# of pull_request_target event are different from those of pull_request event.
#
# For example, the following environment variables and context are different.
#
# - event_name, GITHUB_EVENT_NAME
# - ref, GITHUB_REF
# - sha, GITHUB_SHA
# - ref_name, GITHUB_REF_NAME
#
# @source https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#modify-workflows-for-pullrequesttarget
pull_request_target:
types: [opened, synchronize]
paths:
- 'build/**'
- 'includes/**'
- 'src/**'
- '*.php'
# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request_target' && github.head_ref || github.sha }}
cancel-in-progress: true
jobs:
zip:
name: Build GatherPress plugin & upload as zipped artifact
runs-on: ubuntu-latest
steps:
# To checkout the merged commit with actions/checkout on pull_request_target event,
# you need to get the pull request by GitHub API
# and set the merge commit hash to actions/checkout input ref.
#
# @source https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#checkout-merge-commits
- uses: suzuki-shunsuke/[email protected]
id: pr
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ steps.pr.outputs.merge_commit_sha }}
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: latest
coverage: none
tools: wp-cli
- name: Install latest version of dist-archive-command
run: wp package install wp-cli/dist-archive-command:dev-main
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
# Enable built-in functionality for caching and restoring dependencies, which is disabled by default.
# The actions/setup-node uses actions/cache under the hood.
# https://github.com/actions/setup-node#caching-global-packages-data
cache: 'npm'
# Restoring the short lived node_modules cache
# to be used across all workflows running on the last commit.
# https://github.com/actions/cache/blob/main/caching-strategies.md#creating-a-short-lived-cache
- uses: actions/cache/restore@v4
id: node_modules-cache
with:
path: |
./node_modules
key: ${{ runner.os }}-node_modules-${{ steps.pr.outputs.merge_commit_sha }}-${{ hashFiles('package-lock.json') }}
- name: NPM install
if: steps.node_modules-cache.outputs.cache-hit != 'true'
run: npm ci --legacy-peer-deps
# Creating a short lived node_modules cache
- uses: actions/cache/save@v4
if: steps.node_modules-cache.outputs.cache-hit != 'true'
with:
path: |
./node_modules
key: ${{ steps.node_modules-cache.outputs.cache-primary-key }}
- name: Build plugin
# - [Incorrect version number used when creating zip archive · Issue #92 · wp-cli/dist-archive-command](https://github.com/wp-cli/dist-archive-command/issues/92)
run: |
npm run build
wp dist-archive . ./${{ github.event.repository.name }}.zip
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ github.event.repository.name }}-pr
path: ./${{ github.event.repository.name }}.zip
comment:
name: Comment with playground link
needs: zip # Ensure this runs after zip job.
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Add Preview Links comment
id: comment-on-pr
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const createPreviewLinks = require('.github/scripts/create-preview-links');
createPreviewLinks(github, context);