-
Notifications
You must be signed in to change notification settings - Fork 6
243 lines (220 loc) · 8.83 KB
/
build-plugin-archive.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
name: Create plugin archive
on:
workflow_call:
inputs:
NODE_OPTIONS:
description: Space-separated list of command-line Node options.
type: string
default: ''
required: false
NODE_VERSION:
description: Node version with which the static code analysis is to be executed.
default: 18
required: false
type: string
NPM_REGISTRY_DOMAIN:
description: Domain of the private npm registry.
default: https://npm.pkg.github.com/
required: false
type: string
PACKAGE_MANAGER:
description: Package manager with which the dependencies should be installed (`npm` or `yarn`).
default: 'yarn'
required: false
type: string
COMPOSER_ARGS:
description: Set of arguments passed to Composer.
default: '--no-dev --no-scripts --prefer-dist --optimize-autoloader'
required: false
type: string
PHP_VERSION:
description: PHP version to use during packaging.
default: "8.0"
required: false
type: string
ARCHIVE_NAME:
description: The base name of the resulting zip archive. Falls back to the repository name.
default: ''
required: false
type: string
ARCHIVE_FULL_NAME:
description: The full name of the resulting zip archive. Falls back to the ARCHIVE_NAME-PLUGIN_VERSION template.
required: false
default: '',
type: string
PLUGIN_MAIN_FILE:
description: The name of the main plugin file.
required: false
default: 'index.php'
type: string
PLUGIN_DIR_NAME:
description: The name of the plugin directory inside the archive. Falls back to the archive name and repository name.
required: false
default: ''
type: string
PLUGIN_VERSION:
description: The new plugin version.
required: true
type: string
PRE_SCRIPT:
description: Run custom shell code before creating the release archive.
default: ''
required: false
type: string
COMPILE_ASSETS_ARGS:
description: Set of arguments passed to Composer Asset Compiler.
default: '-v --env=root'
required: false
type: string
secrets:
COMPOSER_AUTH_JSON:
description: Authentication for privately hosted packages and repositories as a JSON formatted object.
required: false
NPM_REGISTRY_TOKEN:
description: Authentication for the private npm registry.
required: false
ENV_VARS:
description: Additional environment variables as a JSON formatted object.
required: false
jobs:
create-plugin-archive:
timeout-minutes: 5
runs-on: ubuntu-latest
env:
NODE_OPTIONS: ${{ inputs.NODE_OPTIONS }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }}
NODE_CACHE_MODE: ''
COMPOSER_AUTH: '${{ secrets.COMPOSER_AUTH_JSON }}'
# Disables symlinking of local path repositories.
# During development, symlinking is preferable.
# In resulting builds, you will likely want to ship the actual install location and remove the source directory.
COMPOSER_MIRROR_PATH_REPOS: 1
outputs:
artifact: ${{ steps.set-artifact-name.outputs.artifact }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up plugin base name
env:
ARCHIVE_NAME: ${{ inputs.ARCHIVE_NAME }}
id: plugin-data
run: |
if [ ! -z "$ARCHIVE_NAME" ]; then
echo "archive-name=$ARCHIVE_NAME" >> $GITHUB_OUTPUT
else
echo "archive-name=${{ github.event.repository.name }}" >> $GITHUB_OUTPUT
fi
- name: Set up full archive name
env:
ARCHIVE_FULL_NAME: ${{ inputs.ARCHIVE_FULL_NAME }}
id: archive-full-name
run: |
if [ ! -z "$ARCHIVE_FULL_NAME" ]; then
echo "archive-full-name=$ARCHIVE_FULL_NAME" >> $GITHUB_OUTPUT
fi
- name: Set up plugin dir name
env:
PLUGIN_DIR_NAME: ${{ inputs.PLUGIN_DIR_NAME }}
id: plugin-dir-name
run: |
if [ ! -z "$PLUGIN_DIR_NAME" ]; then
echo "plugin-dir-name=$PLUGIN_DIR_NAME" >> $GITHUB_OUTPUT
elif [! -z "$ARCHIVE_NAME"]; then
echo "plugin-dir-name=$ARCHIVE_NAME"
else
echo "plugin-dir-name=${{ github.event.repository.name }}" >> $GITHUB_OUTPUT
- name: Set up custom environment variables
env:
ENV_VARS: ${{ secrets.ENV_VARS }}
if: ${{ env.ENV_VARS }}
uses: actions/github-script@v7
with:
script: |
JSON
.parse(process.env.ENV_VARS)
.forEach(envVar => core.exportVariable(envVar.name, envVar.value));
- name: Set up node cache mode
run: |
if [ "${{ inputs.PACKAGE_MANAGER }}" == 'npm' ] && { [ -f "${GITHUB_WORKSPACE}/package-lock.json" ] || [ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ]; }; then
echo "NODE_CACHE_MODE=npm" >> $GITHUB_ENV
elif [ "${{ inputs.PACKAGE_MANAGER }}" == 'yarn' ] && [ -f "${GITHUB_WORKSPACE}/yarn.lock" ]; then
echo "NODE_CACHE_MODE=yarn" >> $GITHUB_ENV
else
echo "No lock files found or unknown package manager"
fi
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.PHP_VERSION }}
tools: wp-cli
- name: Install Composer dependencies
# ⬑ The idea is to ensure we have non-production tools like Composer Asset Compiler and WordPress Translation Downloader available
uses: ramsey/composer-install@v2
with:
composer-options: --prefer-dist
- name: Check optional Composer build tools
id: composer-tools
run: |
hasDep(){
composer show -i -N -D --strict "${1}" >/dev/null 2>&1
local EXIT=$?
echo "$EXIT"
}
echo "assets-compiler=$( hasDep inpsyde/composer-assets-compiler )" >> $GITHUB_OUTPUT
echo "translation-downloader=$( hasDep inpsyde/wp-translation-downloader )" >> $GITHUB_OUTPUT
- name: Check for package.json
id: check-for-package
run: |
if [ -f package.json ]; then
echo "has_package=true" >> $GITHUB_OUTPUT
else
echo "has_package=false" >> $GITHUB_OUTPUT
fi
- name: Set up node
if: steps.check-for-package.outputs.has_package == 'true'
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.NODE_VERSION }}
registry-url: ${{ inputs.NPM_REGISTRY_DOMAIN }}
cache: ${{ env.NODE_CACHE_MODE }}
- name: Run Composer Asset Compiler
if: steps.composer-tools.outputs.assets-compiler == '0' && steps.check-for-package.outputs.has_package == 'true'
run: composer compile-assets ${{ inputs.COMPILE_ASSETS_ARGS }}
- name: Run WordPress Translation Downloader
if: steps.composer-tools.outputs.translation-downloader == '0'
run: composer wp-translation-downloader:download
- name: Install Composer dependencies without dev dependencies
# ⬑ Now we want to get rid of dev dependencies and most probably skip composer scripts
uses: ramsey/composer-install@v2
with:
composer-options: ${{ inputs.COMPOSER_ARGS }}
- name: Add commit hash to plugin header
env:
GIT_SHA: ${{ github.sha }}
run: 'sed -Ei "s/SHA: .*/SHA: ${GIT_SHA}/g" ${{ inputs.PLUGIN_MAIN_FILE }}'
- name: Set plugin version header
env:
PLUGIN_VERSION: ${{ inputs.PLUGIN_VERSION }}
run: 'sed -Ei "s/Version: .*/Version: ${PLUGIN_VERSION}/g" ${{ inputs.PLUGIN_MAIN_FILE }}'
- name: Install dist-archive command
run: wp package install wp-cli/dist-archive-command
- name: Execute custom code before archive creation
run: |
${{ inputs.PRE_SCRIPT }}
- name: Run WP-CLI command
run: wp dist-archive . ./archive.zip --plugin-dirname=${{ steps.plugin-data.outputs.archive-name }}
# GitHub Action artifacts would otherwise produce a zip within a zip
- name: Unzip archive to dist/
run: unzip archive.zip -d dist
- name: Set artifact name
id: set-artifact-name
run: |
if [! -z ${{ steps.plugin-data.outputs.archive-full-name }}]; then
echo "artifact=${{ steps.plugin-data.outputs.archive-full-name }}" >> $GITHUB_OUTPUT
else
echo "artifact=${{ steps.plugin-data.outputs.archive-name }}-${{ inputs.PLUGIN_VERSION }}" >> $GITHUB_OUTPUT
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ${{ steps.set-artifact-name.outputs.artifact }}
path: dist/*