From dc0bc91665d1241957aa2acbb57921b8aac24748 Mon Sep 17 00:00:00 2001 From: Giulio Micheloni Date: Mon, 8 May 2023 20:15:54 +0200 Subject: [PATCH] main: no-op if builder is already set --- dist/main/index.js | 40 +++++++++++++++++++++++++++++++++++++++- main.ts | 19 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/dist/main/index.js b/dist/main/index.js index 4f984fa..50074f5 100644 --- a/dist/main/index.js +++ b/dist/main/index.js @@ -4288,6 +4288,23 @@ module.exports = require("util"); /******/ } /******/ /************************************************************************/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __nccwpck_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports @@ -4311,6 +4328,11 @@ var __webpack_exports__ = {}; // ESM COMPAT FLAG __nccwpck_require__.r(__webpack_exports__); +// EXPORTS +__nccwpck_require__.d(__webpack_exports__, { + "nscRemoteBuilderName": () => (/* binding */ nscRemoteBuilderName) +}); + // EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js var core = __nccwpck_require__(186); // EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js @@ -4345,6 +4367,7 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume +const nscRemoteBuilderName = "remote-nsc"; function run() { return __awaiter(this, void 0, void 0, function* () { var commandExists = __nccwpck_require__(724); @@ -4362,11 +4385,19 @@ Please add a step this step to your workflow's job definition: function prepareBuildx() { return __awaiter(this, void 0, void 0, function* () { try { + yield core.group(`Check if Namespace Cloud Remote Builder`, () => __awaiter(this, void 0, void 0, function* () { + const builderExists = yield remoteNscBuilderExists(); + if (builderExists) { + core.info(` +GitHub runner is already configured to use Namespace Cloud build cluster.`); + return; + } + })); const sock = tmpFile("buildkit-proxy.sock"); yield core.group(`Proxy Buildkit from Namespace Cloud`, () => __awaiter(this, void 0, void 0, function* () { yield ensureNscloudToken(); yield exec.exec(`nsc cluster proxy --kind=buildkit --cluster=build-cluster --sock_path=${sock} --background=${ProxyPidFile}`); - yield exec.exec(`docker buildx create --name remote-nsc --driver remote unix://${sock} --use`); + yield exec.exec(`docker buildx create --name ${nscRemoteBuilderName} --driver remote unix://${sock} --use`); })); yield core.group(`Builder`, () => __awaiter(this, void 0, void 0, function* () { core.info("remote-nsc"); @@ -4391,6 +4422,13 @@ function ensureNscloudToken() { yield exec.exec("nsc auth exchange-github-token --ensure=5m"); }); } +function remoteNscBuilderExists() { + return __awaiter(this, void 0, void 0, function* () { + const { stdout, stderr } = yield exec.getExecOutput(`docker buildx inspect ${nscRemoteBuilderName}`); + const builderNotFoundStr = `no builder "${nscRemoteBuilderName}" found`; + return !(stdout.includes(builderNotFoundStr) || stderr.includes(builderNotFoundStr)); + }); +} run(); })(); diff --git a/main.ts b/main.ts index c7e0e7a..fd9a5c6 100644 --- a/main.ts +++ b/main.ts @@ -3,6 +3,8 @@ import * as exec from "@actions/exec"; import * as fs from "fs"; import { ProxyPidFile, tmpFile } from "./common"; +export const nscRemoteBuilderName = "remote-nsc"; + async function run(): Promise { var commandExists = require("command-exists"); @@ -19,6 +21,15 @@ Please add a step this step to your workflow's job definition: async function prepareBuildx(): Promise { try { + await core.group(`Check if Namespace Cloud Remote Builder`, async () => { + const builderExists = await remoteNscBuilderExists(); + if (builderExists) { + core.info(` +GitHub runner is already configured to use Namespace Cloud build cluster.`); + return; + } + }); + const sock = tmpFile("buildkit-proxy.sock"); await core.group(`Proxy Buildkit from Namespace Cloud`, async () => { @@ -29,7 +40,7 @@ async function prepareBuildx(): Promise { ); await exec.exec( - `docker buildx create --name remote-nsc --driver remote unix://${sock} --use` + `docker buildx create --name ${nscRemoteBuilderName} --driver remote unix://${sock} --use` ); }); @@ -56,4 +67,10 @@ async function ensureNscloudToken() { await exec.exec("nsc auth exchange-github-token --ensure=5m"); } +async function remoteNscBuilderExists(): Promise { + const { stdout, stderr } = await exec.getExecOutput(`docker buildx inspect ${nscRemoteBuilderName}`); + const builderNotFoundStr = `no builder "${nscRemoteBuilderName}" found`; + return !(stdout.includes(builderNotFoundStr) || stderr.includes(builderNotFoundStr)) +} + run();