ci: deploy front via docker.sock helper container (act_runner ignores custom volumes)
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env node
|
||||
// Deploy front/dist/prod to host /data/www/userbase/front using the Docker daemon
|
||||
// that act_runner already mounts into the job container (/var/run/docker.sock).
|
||||
//
|
||||
// Why this script exists:
|
||||
// act_runner (v0.6.1) ignores both `container.options` extra --volume and
|
||||
// workflow `container.volumes` in this setup, so the host static dir can't be
|
||||
// bind-mounted directly into the job container. But the host docker.sock IS
|
||||
// mounted by default, so we launch a throwaway helper container that binds
|
||||
// BOTH the build workspace (a docker volume on the host) and /data/www/userbase,
|
||||
// and copies the built assets across.
|
||||
//
|
||||
// No docker CLI and no network egress required — we speak the Docker HTTP API
|
||||
// over the unix socket directly with Node's http module.
|
||||
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
|
||||
const SOCKET = '/var/run/docker.sock';
|
||||
const HOST_STATIC = '/data/www/userbase';
|
||||
const HELPER_IMAGE = 'node:20'; // already cached on host (used by the job)
|
||||
|
||||
function dockerReq(method, path, bodyObj) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const data = bodyObj ? JSON.stringify(bodyObj) : null;
|
||||
const headers = {};
|
||||
if (data) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
headers['Content-Length'] = Buffer.byteLength(data);
|
||||
}
|
||||
const req = http.request(
|
||||
{ socketPath: SOCKET, path, method, headers },
|
||||
(res) => {
|
||||
let buf = '';
|
||||
res.on('data', (c) => (buf += c));
|
||||
res.on('end', () => resolve({ status: res.statusCode, body: buf }));
|
||||
}
|
||||
);
|
||||
req.on('error', reject);
|
||||
if (data) req.write(data);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
// Locate the host path backing the workspace mountpoint (a docker volume _data dir).
|
||||
function findWorkspaceSource(mountpoint) {
|
||||
const txt = fs.readFileSync('/proc/self/mountinfo', 'utf8');
|
||||
for (const line of txt.split('\n')) {
|
||||
const parts = line.trim().split(/\s+/);
|
||||
if (parts.length < 5) continue;
|
||||
const mp = parts[4];
|
||||
if (mp === mountpoint || mp.endsWith(mountpoint)) {
|
||||
const dash = parts.indexOf('-');
|
||||
if (dash !== -1 && parts.length > dash + 2) {
|
||||
return parts[dash + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const wsMount = '/workspace/admin/userbase';
|
||||
const wsSource = findWorkspaceSource(wsMount);
|
||||
if (!wsSource) {
|
||||
throw new Error('Could not find workspace source in /proc/self/mountinfo');
|
||||
}
|
||||
console.log('workspace source (host path):', wsSource);
|
||||
|
||||
const srcDist = wsSource + '/front/dist/prod';
|
||||
|
||||
const createBody = {
|
||||
Image: HELPER_IMAGE,
|
||||
Tty: true,
|
||||
HostConfig: {
|
||||
NetworkMode: 'none',
|
||||
Binds: [`${wsSource}:/src:ro`, `${HOST_STATIC}:${HOST_STATIC}`],
|
||||
},
|
||||
Cmd: [
|
||||
'sh',
|
||||
'-c',
|
||||
`set -e; ` +
|
||||
`rm -rf ${HOST_STATIC}/front && ` +
|
||||
`mkdir -p ${HOST_STATIC}/front && ` +
|
||||
`cp -r ${srcDist}/. ${HOST_STATIC}/front/ && ` +
|
||||
`echo "copied"; ls -la ${HOST_STATIC}/front | head`,
|
||||
],
|
||||
};
|
||||
|
||||
const created = await dockerReq('POST', '/containers/create', createBody);
|
||||
if (created.status >= 400) {
|
||||
throw new Error('create failed: ' + created.status + ' ' + created.body);
|
||||
}
|
||||
const id = JSON.parse(created.body).Id;
|
||||
console.log('helper container:', id);
|
||||
|
||||
const started = await dockerReq('POST', `/containers/${id}/start`);
|
||||
if (started.status >= 400) {
|
||||
throw new Error('start failed: ' + started.status + ' ' + started.body);
|
||||
}
|
||||
|
||||
// Block until the helper exits.
|
||||
const waited = await dockerReq('POST', `/containers/${id}/wait`);
|
||||
const waitJson = JSON.parse(waited.body || '{}');
|
||||
console.log('helper exit code:', waitJson.StatusCode);
|
||||
|
||||
// Grab logs (Tty:true => raw, no multiplexing).
|
||||
const logs = await dockerReq('GET', `/containers/${id}/logs?stdout=1&stderr=1`);
|
||||
console.log('--- helper logs ---');
|
||||
console.log(logs.body);
|
||||
|
||||
// Cleanup.
|
||||
await dockerReq('DELETE', `/containers/${id}?force=true`);
|
||||
|
||||
if (waitJson.StatusCode !== 0) {
|
||||
throw new Error('helper exited with code ' + waitJson.StatusCode);
|
||||
}
|
||||
console.log('deploy OK ->', HOST_STATIC + '/front');
|
||||
}
|
||||
|
||||
main().then(
|
||||
() => process.exit(0),
|
||||
(err) => {
|
||||
console.error('DEPLOY FAILED:', err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user