-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
256 lines (224 loc) · 9.74 KB
/
action.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
244
245
246
247
248
249
250
251
252
253
254
255
256
name: 'Build and Release WordPress Plugin'
description: 'Create build releases of your create-wordpress-plugin WordPress projects.'
branding:
icon: 'package'
color: 'blue'
inputs:
build-command:
description: 'Specify the command to run for build.'
required: false
default: 'npm run build'
draft:
description: 'Specify whether to create a draft release.'
required: false
default: 'false'
github-token:
description: 'Specify the GitHub token to use.'
required: false
default: ${{ github.token }}
node-version:
description: 'Specify the version of Node.js to use.'
required: false
default: 'lts/*'
php-version:
description: 'Specify the version of PHP to use.'
required: false
default: '8.1'
skip-build:
description: 'Specify whether to skip the build step.'
required: false
default: 'false'
skip-composer-install:
description: 'Specify whether to skip the composer install step.'
required: false
default: 'false'
skip-composer-require-removal:
description: 'Skip the removal of the require/require-dev sections in the composer.json file'
default: 'false'
required: false
skip-npm-install:
description: 'Specify whether to skip the npm install step.'
required: false
default: 'false'
runs:
using: 'composite'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ inputs.github-token || github.token }}
# Extract data about the project.
# The format is:
# id: extract-data-name
# Outputted to value="..."
# Referenced as steps.extract-data-name.outputs.value.
- name: Extract plugin version
shell: bash
id: extract-version
run: |
PACKAGE_VERSION=""
PLUGIN_NAME=$(basename $(pwd))
echo "Using Plugin Name: $PLUGIN_NAME"
extract-version() {
grep -E "Version:\s*[0-9]+\.[0-9]+\.[0-9]+" $1 | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+" | head -1
}
if [ -f "$PLUGIN_NAME.php" ]; then
PACKAGE_VERSION=$(extract-version "$PLUGIN_NAME.php")
elif [ -f "plugin.php" ]; then
PACKAGE_VERSION=$(extract-version "plugin.php")
fi
if [ -f composer.json ] && [ -z "$PACKAGE_VERSION" ]; then
PACKAGE_VERSION=$(cat composer.json | jq -r '.version')
if [ "$PACKAGE_VERSION" = "null" ]; then
PACKAGE_VERSION=""
fi
fi
if [ -f package.json ] && [ -z "$PACKAGE_VERSION" ]; then
PACKAGE_VERSION=$(cat package.json | jq -r '.version')
if [ "$PACKAGE_VERSION" = "null" ]; then
PACKAGE_VERSION=""
fi
fi
# Validate that we have a semver version number
if [ -n "$PACKAGE_VERSION" ]; then
if ! [[ "$PACKAGE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
echo "Invalid version number: $PACKAGE_VERSION"
exit 1
fi
fi
# Ignore the version if it's 0.0.0
if [ "$PACKAGE_VERSION" = "0.0.0" ]; then
PACKAGE_VERSION=""
fi
echo "Package Version: $PACKAGE_VERSION"
if [ -z $PACKAGE_VERSION ]; then
echo "value=false" >> $GITHUB_OUTPUT
else
echo "value=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
fi
- name: Extract branch name
shell: bash
id: extract-branch
run: echo "value=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
- name: Check if this is a built branch
id: extract-is-built-branch
shell: bash
run: |
if [[ ${{ steps.extract-branch.outputs.value }} =~ -built$ ]]; then
echo "value=true" >> $GITHUB_OUTPUT
else
echo "value=false" >> $GITHUB_OUTPUT
fi
- name: Compile the tag name from the version
id: extract-tag-name
shell: bash
if: steps.extract-version.outputs.value != 'false'
run: echo "value=v${{ steps.extract-version.outputs.value }}" >> $GITHUB_OUTPUT
- name: Check if the version already exists as a tag
id: extract-tag-exists
shell: bash
if: steps.extract-version.outputs.value != 'false' && steps.extract-tag-name.outputs.value
run: |
if [ -z "$(git tag -l ${{ steps.extract-tag-name.outputs.value }})" ]; then
echo "value=false" >> $GITHUB_OUTPUT
else
echo "value=true" >> $GITHUB_OUTPUT
fi
- name: Extract previous tag version
id: extract-previous-tag
shell: bash
if: steps.extract-tag-exists.outputs.value == 'false' && steps.extract-version.outputs.value != 'false'
run: |
# Get the release tag for comparison
PREVIOUS_TAG=$(git describe --tags $(git rev-list --tags --max-count=1) 2>/dev/null || echo "")
# If there is no previous tag, then we can't compare
if [ -z "$PREVIOUS_TAG" ]; then
echo "value=false" >> $GITHUB_OUTPUT
else
echo "value=$PREVIOUS_TAG" >> $GITHUB_OUTPUT
fi
- name: Check if the project has front-end assets
shell: bash
id: extract-has-node-assets
run: |
[[ -f package.json ]] && echo "value=true" >> $GITHUB_OUTPUT || echo "value=false" >> $GITHUB_OUTPUT
- name: Set environment variables
shell: bash
run: |
echo "GITHUB_TOKEN=$([ ! -z "${{ inputs.github-token }}"] && echo "${{ inputs.github-token }}" || echo "${{ github.token }}")" >> $GITHUB_ENV
echo "SKIP_BUILD=$([ "${{ inputs.skip-build }}" = "true" ] && echo "true" || echo "false")" >> $GITHUB_ENV
echo "SKIP_COMPOSER_INSTALL=$([ "${{ inputs.skip-composer-install }}" = "true" ] && echo "true" || echo "false")" >> $GITHUB_ENV
echo "SKIP_NPM_INSTALL=$([ "${{ inputs.skip-npm-install }}" = "true" ] && echo "true" || echo "false")" >> $GITHUB_ENV
echo "BUILT_BRANCH=${{ steps.extract-branch.outputs.value }}-built" >> $GITHUB_ENV
echo "VERSION_TAG=$([ ! -z "${{ steps.extract-tag-name.outputs.value }}" ] && echo "${{ steps.extract-tag-name.outputs.value }}" || echo "false")" >> $GITHUB_ENV
- name: Composer Install
if: inputs.skip-composer-install != 'true'
uses: alleyinteractive/action-test-php@develop
with:
install-command: 'composer install --prefer-dist --no-interaction --no-progress --no-dev --optimize-autoloader'
php-version: '${{ inputs.php-version }}'
skip-audit: 'true'
skip-services: 'true'
skip-test: 'true'
skip-wordpress-install: 'true'
- name: Clear composer require/require-dev
if: inputs.skip-composer-install != 'true' && inputs.skip-composer-require-removal != 'true'
shell: bash
run: |
jq 'del(.require, .["require-dev"])' composer.json > composer.json.tmp
mv composer.json.tmp composer.json
rm -rf composer.lock composer.json.bak || true
- name: NPM Install and Build
uses: alleyinteractive/action-test-node@develop
if: steps.extract-has-node-assets.outputs.value == 'true' && inputs.skip-npm-install != 'true'
with:
build-command: '${{ inputs.build-command }}'
node-version: '${{ inputs.node-version }}'
skip-audit: 'true'
skip-test: 'true'
- name: Push to ${{ steps.extract-branch.outputs.value }}-built branch
if: steps.extract-is-built-branch.outputs.value == 'false' && steps.extract-branch.outputs.value != 'false' && github.repository != 'alleyinteractive/create-wordpress-plugin'
shell: bash
env:
CURRENT_BRANCH: '${{ steps.extract-branch.outputs.value }}'
CREATING_RELEASE: ${{ steps.extract-version.outputs.value != 'false' && steps.extract-tag-exists.outputs.value == 'false' && 'true' }}
run: |
git config --global user.email "[email protected]"
git config --global user.name "$GITHUB_ACTOR"
rm -rf docker_tag output.log
# Clear out the .gitignore file if .deployignore exists
if [[ -e "$GITHUB_WORKSPACE/.deployignore" ]]; then
echo "Replacing .gitignore with .deployignore"
mv .deployignore .gitignore
elif [[ -e "$GITHUB_WORKSPACE/.distignore" ]]; then
echo "Replacing .gitignore with .distignore"
mv .distignore .gitignore
fi
git ls-files -i -c --exclude-standard . | xargs git rm --cached
git checkout -b $BUILT_BRANCH
git add -A
if [[ $CREATING_RELEASE == "true" ]]; then
git commit -m "Built changes for $VERSION_TAG from $CURRENT_BRANCH $GITHUB_SHA"
else
git commit -m "Built changes from $CURRENT_BRANCH $GITHUB_SHA"
fi
git push --force -u origin "${BUILT_BRANCH}"
- name: Create release for built branch
if: steps.extract-is-built-branch.outputs.value == 'false' && steps.extract-branch.outputs.value != 'false' && github.repository != 'alleyinteractive/create-wordpress-plugin' && steps.extract-version.outputs.value != 'false' && steps.extract-tag-exists.outputs.value == 'false'
shell: bash
env:
PREVIOUS_TAG_VERSION: '${{ steps.extract-previous-tag.outputs.value }}'
run: |
if [ "$PREVIOUS_TAG_VERSION" == "false" ]; then
if [[ $DRAFT_RELEASE == "true" ]]; then
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes -d --latest
else
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes --latest
fi
else
if [[ $DRAFT_RELEASE == "true" ]]; then
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes -d --latest --notes-start-tag "$PREVIOUS_TAG_VERSION"
else
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes --latest --notes-start-tag "$PREVIOUS_TAG_VERSION"
fi
fi