forked from ubiquity-os/action-deploy-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
229 lines (201 loc) · 8.43 KB
/
action.yml
File metadata and controls
229 lines (201 loc) · 8.43 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
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
name: "Update Manifest and Commit Changes"
description: "Checks out the repository, sets up Node, installs dependencies, updates manifest.json, formats, and commits/pushes changes signing the commit."
inputs:
manifestPath:
description: "The path to the manifest.json file."
required: false
default: "${{ github.workspace }}/manifest.json"
schemaPath:
description: "The path to the plugin settings schema."
required: false
default: "${{ github.workspace }}/src/types/plugin-input.ts"
pluginEntry:
description: "The path to the plugin entry file."
required: false
default: "${{ github.workspace }}/src/index.ts"
commitMessage:
description: "The commit message."
required: false
default: "chore: [skip ci] updated manifest.json and dist build"
nodeVersion:
description: "The version of Node.js to use."
default: "20.10.0"
treatAsEsm:
description: "If the package is set to be treated as ESM, it will replace __dirname occurrences."
default: "false"
sourcemap:
description: "Generates the sourcemap for the compiled files"
outputs: {}
runs:
using: "composite"
steps:
- name: Create GitHub App token
id: app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ env.APP_ID }}
private-key: ${{ env.APP_PRIVATE_KEY }}
- name: Validate GitHub App token
shell: bash
run: |
if [[ -z "${{ steps.app-token.outputs.token }}" ]]; then
echo "Failed to generate GitHub App token."
exit 1
fi
- name: Get GitHub App User ID
id: get-user-id
shell: bash
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Check out the repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.nodeVersion }}
- uses: oven-sh/setup-bun@v2
- name: Install dependencies
shell: bash
run: |
bun install --frozen-lockfile
- name: Build project
shell: bash
run: |
bun add -DE @vercel/ncc
echo "Deleting previous dist..."
rm -rf "${{ github.workspace }}/dist"
echo "Compiling plugin..."
bun ncc build ${{ inputs.pluginEntry }} --external "./tests" -m ${{ inputs.sourcemap == 'true' && '-s' || '' }} -o dist/plugin
echo "Compiling plugin types..."
bun ncc build ${{ inputs.schemaPath }} --external "./tests" -m -o plugin
- name: Replace __dirname with import.meta.dirname
if: ${{ inputs.treatAsEsm }}
shell: bash
run: |
if [ "${{ inputs.treatAsEsm }}" = "true" ]; then
sed -i 's/__dirname/import.meta.dirname/g' "${{ github.workspace }}/dist/plugin/index.js"
fi
- name: Update manifest configuration JSON
uses: actions/github-script@v7
with:
script: |
const fs = require('fs').promises;
const path = require('path');
async function updateManifest() {
const manifestPath = '${{ inputs.manifestPath }}';
const pluginPath = path.resolve('${{ github.workspace }}', 'plugin', 'index.js');
let pluginSettingsSchema;
try {
// First, try to load as ESM
try {
const pluginModule = await import(`file://${pluginPath}`);
pluginSettingsSchema = pluginModule.pluginSettingsSchema;
if (!pluginSettingsSchema) {
throw new Error('pluginSettingsSchema not found in the ESM module');
}
} catch (esmError) {
// If ESM import fails, try loading as CJS
try {
const pluginModule = require(pluginPath);
pluginSettingsSchema = pluginModule.pluginSettingsSchema;
if (!pluginSettingsSchema) {
throw new Error('pluginSettingsSchema not found in the CJS module');
}
} catch (cjsError) {
console.error('Error loading module as ESM and CJS:', esmError, cjsError);
process.exit(1);
}
}
} catch (error) {
console.error('Error loading module:', error);
process.exit(1);
}
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
manifest["configuration"] = pluginSettingsSchema;
function customReviver(key, value) {
if (typeof value === "object" && value !== null) {
if ("properties" in value && "required" in value) {
const requiredFields = new Set(value.required);
for (const [propKey, propValue] of Object.entries(value.properties)) {
if (typeof propValue === 'object' && 'default' in propValue) {
requiredFields.delete(propKey);
}
}
value.required = Array.from(requiredFields);
if (value.required.length === 0) {
delete value.required;
}
}
// Recursively apply to nested objects and arrays
if (Array.isArray(value)) {
return value.map(item => JSON.parse(JSON.stringify(item), customReviver));
} else {
return Object.fromEntries(
Object.entries(value).map(([k, v]) => [k, JSON.parse(JSON.stringify(v), customReviver)])
);
}
}
return value;
}
const updatedManifest = JSON.stringify(manifest, customReviver, 2);
await fs.writeFile(manifestPath, updatedManifest, 'utf8');
}
updateManifest();
- name: Format manifest using Prettier
shell: bash
run: |
npx prettier --write "${{ inputs.manifestPath }}"
- name: Inject reassembly code into dist/index.js
shell: bash
env:
TREAT_AS_ESM: ${{ inputs.treatAsEsm }}
run: |
if [[ "${TREAT_AS_ESM}" == "true" ]]; then
cp "${{ github.action_path }}/.github/scripts/reassembly-esm.js" dist/index.js
cp dist/plugin/package.json dist/package.json
else
cp "${{ github.action_path }}/.github/scripts/reassembly-cjs.js" dist/index.js
fi
- name: Validate GitHub App identity
shell: bash
run: |
if [[ -z "${{ steps.app-token.outputs.app-slug }}" || -z "${{ steps.get-user-id.outputs.user-id }}" ]]; then
echo "Incomplete GitHub App identity information."
exit 1
fi
- name: Update manifest.json and dist folder
shell: bash
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
COMMIT_MESSAGE: ${{ inputs.commitMessage }}
MANIFEST_PATH: ${{ inputs.manifestPath }}
GITHUB_WORKSPACE: ${{ github.workspace }}
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
APP_USER_ID: ${{ steps.get-user-id.outputs.user-id }}
run: |
set -euo pipefail
if [[ -z "${APP_SLUG}" || -z "${APP_USER_ID}" ]]; then
echo "Missing GitHub App identity; aborting commit."
exit 1
fi
git config --global user.name "${APP_SLUG}[bot]"
git config --global user.email "${APP_USER_ID}+${APP_SLUG}[bot]@users.noreply.github.com"
git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git add "${MANIFEST_PATH}"
if [[ -d "${GITHUB_WORKSPACE}/dist" ]]; then
find "${GITHUB_WORKSPACE}/dist" \( -name "*.js" -o -name "*.cjs" -o -name "*.map" -o -name "*.json" \) -print0 | xargs -0 git add -f
fi
if [[ -d "${GITHUB_WORKSPACE}/dist/plugin" ]]; then
find "${GITHUB_WORKSPACE}/dist/plugin" \( -name "*.js" -o -name "*.cjs" -o -name "*.map" -o -name "*.json" \) -print0 | xargs -0 git add -f
fi
echo "Checking for staged changes..."
# Check if git add actually staged anything
if ! git diff --cached --quiet; then
echo "Changes detected. Committing and pushing..."
git commit -m "${COMMIT_MESSAGE}"
git push
echo "Changes committed and pushed successfully."
else
echo "No changes detected in manifest or dist folder."
fi