@wp-operations/wp-ops 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,6 +45,20 @@ wp-ops doctor
45
45
 
46
46
  Running `wp-ops` with no arguments opens a menu.
47
47
 
48
+ ## Deploy
49
+
50
+ `init` (and `update`, for repositories made earlier) adds two workflows. They work the same way as the Elvion `deploy-apps.yml`:
51
+
52
+ - `deploy-apps.yml`: on push to `main`, deploys each changed app, and every app when `packages/` or a manifest changed. Running it from the platform deploys every app. Each app gets `composer install --no-dev`, is copied with rsync to `<app_path>/wp-content/themes/<theme_slug>/` on its server, and is activated with WP-CLI.
53
+ - `deploy-platform.yml`: deploys `wp-operations/` (with the root `platform.json`) to `platform.server` / `platform.app_path` from `.github/platform.json`, and activates it.
54
+
55
+ Repository secrets:
56
+
57
+ | Secret | Value |
58
+ |---|---|
59
+ | `SSH_PRIVATE_KEY` | The deploy key. |
60
+ | `SERVERS` | `label:ip:user label2:ip2:user2`, where each label is a `server` value in the manifests. |
61
+
48
62
  ## Sandbox
49
63
 
50
64
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-operations/wp-ops",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "The wp-operations tool. One install gives the platform and wp-ops, wp-app, wp-ide and wp-code.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -8,6 +8,8 @@ import { UsageError } from '../args.js';
8
8
  import {
9
9
  APP_FUNCTIONS,
10
10
  APP_INDEX,
11
+ DEPLOY_APPS,
12
+ DEPLOY_PLATFORM,
11
13
  GITIGNORE,
12
14
  appStyle,
13
15
  composerWorkflow,
@@ -67,7 +69,7 @@ export async function init({ dir = process.cwd(), platform, version, log = conso
67
69
  writeNew(path.join(root, kind.folder, '.gitkeep'), '', created);
68
70
  }
69
71
  writeNew(path.join(root, 'packages', '.gitkeep'), '', created);
70
- writeNew(path.join(root, '.github', 'workflows', 'composer.yml'), composerWorkflow(version), created);
72
+ writeWorkflows(root, version, created);
71
73
  writeNew(path.join(root, '.gitignore'), GITIGNORE, created);
72
74
 
73
75
  platform = platform || bundledPlatform();
@@ -116,6 +118,13 @@ export function bundledPlatform() {
116
118
 
117
119
  export const PLATFORM_DIR = 'wp-operations';
118
120
 
121
+ function writeWorkflows(root, version, created) {
122
+ const dir = path.join(root, '.github', 'workflows');
123
+ writeNew(path.join(dir, 'composer.yml'), composerWorkflow(version), created);
124
+ writeNew(path.join(dir, 'deploy-apps.yml'), DEPLOY_APPS, created);
125
+ writeNew(path.join(dir, 'deploy-platform.yml'), DEPLOY_PLATFORM, created);
126
+ }
127
+
119
128
  function themeVersion(dir) {
120
129
  const css = path.join(dir, 'style.css');
121
130
  return fs.existsSync(css) ? (fs.readFileSync(css, 'utf8').match(/^Version:\s*(\S+)/m)?.[1] ?? '') : '';
@@ -135,15 +144,18 @@ function copyTheme(source, target) {
135
144
  * Replaces the repository's platform theme (wp-operations/) with the one
136
145
  * shipped in this wp-ops version. Commit the result to deploy it.
137
146
  */
138
- export async function update({ root, log = console.log }) {
147
+ export async function update({ root, version, log = console.log }) {
139
148
  const source = bundledPlatform();
140
149
  if (!source) throw new UsageError('The platform theme is missing from this wp-ops install. Reinstall: npm install -g @wp-operations/wp-ops');
150
+ const created = [];
151
+ writeWorkflows(root, version, created);
152
+ for (const file of created) log(` ${pc.green('+')} ${path.relative(root, file).split(path.sep).join('/')}`);
141
153
  const target = path.join(root, PLATFORM_DIR);
142
154
  const from = themeVersion(target);
143
155
  const to = themeVersion(source);
144
156
  if (from === to) {
145
157
  log(` Platform is up to date (${to}).`);
146
- return { from, to, changed: false };
158
+ return { from, to, changed: created.length > 0 };
147
159
  }
148
160
  fs.rmSync(target, { recursive: true, force: true });
149
161
  copyTheme(source, target);
package/src/main.js CHANGED
@@ -61,7 +61,7 @@ async function run() {
61
61
  return;
62
62
  }
63
63
  case 'update':
64
- await update({ root: await platformRoot() });
64
+ await update({ root: await platformRoot(), version: pkg.version });
65
65
  return;
66
66
  case 'build':
67
67
  buildAll();
package/src/templates.js CHANGED
@@ -74,6 +74,200 @@ jobs:
74
74
  `;
75
75
  }
76
76
 
77
+ const SSH_SETUP = ` - name: Setup SSH
78
+ run: |
79
+ mkdir -p ~/.ssh
80
+ echo "\${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
81
+ chmod 600 ~/.ssh/id_ed25519
82
+ `;
83
+
84
+ /*
85
+ * Secrets: SSH_PRIVATE_KEY, and SERVERS = "label:ip:user label2:ip2:user2"
86
+ * where each label is a "server" value from the manifests.
87
+ */
88
+ const SERVER_LOOKUP = ` SERVER_IP=$(echo "$SERVERS" | tr ' ' '\\n' | grep "^\${SERVER_LABEL}:" | cut -d: -f2)
89
+ SSH_USER=$(echo "$SERVERS" | tr ' ' '\\n' | grep "^\${SERVER_LABEL}:" | cut -d: -f3)
90
+ if [ -z "$SERVER_IP" ] || [ -z "$SSH_USER" ]; then echo "Server \\"$SERVER_LABEL\\" is not in the SERVERS secret."; exit 1; fi
91
+ ssh-keyscan -H "$SERVER_IP" >> ~/.ssh/known_hosts`;
92
+
93
+ export const DEPLOY_APPS = `name: deploy apps
94
+
95
+ on:
96
+ push:
97
+ branches: [main]
98
+ workflow_dispatch:
99
+
100
+ concurrency:
101
+ group: deploy-apps
102
+ cancel-in-progress: false
103
+
104
+ jobs:
105
+ detect:
106
+ runs-on: ubuntu-latest
107
+ outputs:
108
+ matrix: \${{ steps.set.outputs.matrix }}
109
+ steps:
110
+ - uses: actions/checkout@v4
111
+ with:
112
+ fetch-depth: 2
113
+
114
+ - name: Find the apps to deploy
115
+ id: set
116
+ env:
117
+ EVENT: \${{ github.event_name }}
118
+ run: |
119
+ KINDS=$(jq -c '.apps | to_entries | map({kind: .key, folder: (.value.folder // .key), manifest: (.value.manifest // (".github/" + .key + ".json"))})' .github/platform.json)
120
+ CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || true)
121
+ ENTRIES="[]"
122
+ for ROW in $(echo "$KINDS" | jq -c '.[]'); do
123
+ FOLDER=$(echo "$ROW" | jq -r .folder)
124
+ MANIFEST=$(echo "$ROW" | jq -r .manifest)
125
+ [ -f "$MANIFEST" ] || continue
126
+ for APP in $(jq -r 'keys[]' "$MANIFEST"); do
127
+ if [ "$EVENT" = "workflow_dispatch" ] \\
128
+ || echo "$CHANGED" | grep -q "^$FOLDER/$APP/" \\
129
+ || echo "$CHANGED" | grep -q "^packages/" \\
130
+ || echo "$CHANGED" | grep -qx "$MANIFEST"; then
131
+ ENTRIES=$(echo "$ENTRIES" | jq -c --arg f "$FOLDER" --arg m "$MANIFEST" --arg a "$APP" '. + [{folder: $f, manifest: $m, app: $a}]')
132
+ fi
133
+ done
134
+ done
135
+ echo "matrix=$ENTRIES" >> "$GITHUB_OUTPUT"
136
+ echo "Deploying: $ENTRIES"
137
+
138
+ deploy:
139
+ needs: detect
140
+ if: \${{ needs.detect.outputs.matrix != '[]' }}
141
+ runs-on: ubuntu-latest
142
+ strategy:
143
+ fail-fast: false
144
+ max-parallel: 3
145
+ matrix:
146
+ include: \${{ fromJson(needs.detect.outputs.matrix) }}
147
+ steps:
148
+ - uses: actions/checkout@v4
149
+
150
+ - uses: shivammathur/setup-php@v2
151
+ with:
152
+ php-version: '8.2'
153
+ tools: composer:v2
154
+ coverage: none
155
+
156
+ ${SSH_SETUP}
157
+ - name: Load the deploy target
158
+ id: target
159
+ env:
160
+ SERVERS: \${{ secrets.SERVERS }}
161
+ APP: \${{ matrix.app }}
162
+ MANIFEST: \${{ matrix.manifest }}
163
+ run: |
164
+ SERVER_LABEL=$(jq -r --arg a "$APP" '.[$a].server' "$MANIFEST")
165
+ APP_PATH=$(jq -r --arg a "$APP" '.[$a].app_path' "$MANIFEST")
166
+ THEME_SLUG=$(jq -r --arg a "$APP" '.[$a].theme_slug // $a' "$MANIFEST")
167
+ ${SERVER_LOOKUP}
168
+ echo "target=$SSH_USER@$SERVER_IP" >> "$GITHUB_OUTPUT"
169
+ echo "app_path=$APP_PATH" >> "$GITHUB_OUTPUT"
170
+ echo "theme_slug=$THEME_SLUG" >> "$GITHUB_OUTPUT"
171
+
172
+ - name: Install plugins
173
+ working-directory: \${{ matrix.folder }}/\${{ matrix.app }}
174
+ run: |
175
+ if [ -f composer.json ]; then
176
+ composer install --no-dev --no-interaction --no-progress --optimize-autoloader
177
+ fi
178
+
179
+ - name: Deploy via rsync
180
+ uses: nick-fields/retry@v3
181
+ with:
182
+ timeout_minutes: 5
183
+ max_attempts: 3
184
+ command: |
185
+ rsync -rlz --omit-dir-times --delete \\
186
+ --exclude='.git' --exclude='node_modules' --exclude='.env' \\
187
+ -e "ssh -i ~/.ssh/id_ed25519" \\
188
+ \${{ matrix.folder }}/\${{ matrix.app }}/ \\
189
+ \${{ steps.target.outputs.target }}:\${{ steps.target.outputs.app_path }}/wp-content/themes/\${{ steps.target.outputs.theme_slug }}/
190
+
191
+ - name: Activate theme and flush cache
192
+ uses: nick-fields/retry@v3
193
+ with:
194
+ timeout_minutes: 5
195
+ max_attempts: 3
196
+ command: |
197
+ ssh -i ~/.ssh/id_ed25519 \${{ steps.target.outputs.target }} \\
198
+ "cd \${{ steps.target.outputs.app_path }} && \\
199
+ wp theme activate \${{ steps.target.outputs.theme_slug }} --allow-root && \\
200
+ wp rewrite flush --allow-root && \\
201
+ wp cache flush --allow-root && \\
202
+ (curl -X PURGE http://127.0.0.1/ || true)"
203
+ `;
204
+
205
+ export const DEPLOY_PLATFORM = `name: deploy platform
206
+
207
+ on:
208
+ push:
209
+ branches: [main]
210
+ paths:
211
+ - 'wp-operations/**'
212
+ - 'platform.json'
213
+ workflow_dispatch:
214
+
215
+ concurrency:
216
+ group: deploy-platform
217
+ cancel-in-progress: false
218
+
219
+ jobs:
220
+ deploy:
221
+ runs-on: ubuntu-latest
222
+ steps:
223
+ - uses: actions/checkout@v4
224
+
225
+ ${SSH_SETUP}
226
+ - name: Load the deploy target
227
+ id: target
228
+ env:
229
+ SERVERS: \${{ secrets.SERVERS }}
230
+ run: |
231
+ SERVER_LABEL=$(jq -r '.platform.server // ""' .github/platform.json)
232
+ APP_PATH=$(jq -r '.platform.app_path // ""' .github/platform.json)
233
+ THEME_SLUG=$(jq -r '.platform.theme_slug // "wp-operations"' .github/platform.json)
234
+ if [ -z "$SERVER_LABEL" ] || [ -z "$APP_PATH" ]; then echo "::notice::Set platform.server and platform.app_path in .github/platform.json to deploy the platform."; echo "skip=true" >> "$GITHUB_OUTPUT"; exit 0; fi
235
+ ${SERVER_LOOKUP}
236
+ echo "target=$SSH_USER@$SERVER_IP" >> "$GITHUB_OUTPUT"
237
+ echo "app_path=$APP_PATH" >> "$GITHUB_OUTPUT"
238
+ echo "theme_slug=$THEME_SLUG" >> "$GITHUB_OUTPUT"
239
+
240
+ - name: Use this repository's platform.json
241
+ if: steps.target.outputs.skip != 'true'
242
+ run: cp platform.json wp-operations/platform.json
243
+
244
+ - name: Deploy via rsync
245
+ if: steps.target.outputs.skip != 'true'
246
+ uses: nick-fields/retry@v3
247
+ with:
248
+ timeout_minutes: 5
249
+ max_attempts: 3
250
+ command: |
251
+ rsync -rlz --omit-dir-times --delete \\
252
+ --exclude='.git' --exclude='node_modules' \\
253
+ -e "ssh -i ~/.ssh/id_ed25519" \\
254
+ wp-operations/ \\
255
+ \${{ steps.target.outputs.target }}:\${{ steps.target.outputs.app_path }}/wp-content/themes/\${{ steps.target.outputs.theme_slug }}/
256
+
257
+ - name: Activate the platform and flush cache
258
+ if: steps.target.outputs.skip != 'true'
259
+ uses: nick-fields/retry@v3
260
+ with:
261
+ timeout_minutes: 5
262
+ max_attempts: 3
263
+ command: |
264
+ ssh -i ~/.ssh/id_ed25519 \${{ steps.target.outputs.target }} \\
265
+ "cd \${{ steps.target.outputs.app_path }} && \\
266
+ wp theme activate \${{ steps.target.outputs.theme_slug }} --allow-root && \\
267
+ wp cache flush --allow-root && \\
268
+ (curl -X PURGE http://127.0.0.1/ || true)"
269
+ `;
270
+
77
271
  export function appStyle(slug) {
78
272
  return `/*
79
273
  Theme Name: ${slug}