From 0cc5e42c7a187f402f99b74ff98be45c32823c90 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 12 Aug 2020 20:27:50 +0200 Subject: [PATCH] Add name output to identify builder instance name Signed-off-by: CrazyMax --- .github/workflows/setup-buildx-ci.yml | 27 +++++++++++++++++++++++++++ setup-buildx/README.md | 4 ++++ setup-buildx/__tests__/buildx.test.ts | 8 +++++++- setup-buildx/action.yml | 2 ++ setup-buildx/dist/index.js | 27 +++++++++++++++++---------- setup-buildx/src/buildx.ts | 9 +++++++++ setup-buildx/src/main.ts | 15 ++++++--------- 7 files changed, 72 insertions(+), 20 deletions(-) diff --git a/.github/workflows/setup-buildx-ci.yml b/.github/workflows/setup-buildx-ci.yml index b09b813..ffa6971 100644 --- a/.github/workflows/setup-buildx-ci.yml +++ b/.github/workflows/setup-buildx-ci.yml @@ -40,6 +40,9 @@ jobs: uses: ./setup-buildx/ with: buildx-version: ${{ matrix.buildx-version }} + - + name: Builder instance name + run: echo ${{ steps.buildx.outputs.name }} - name: Available platforms run: echo ${{ steps.buildx.outputs.platforms }} @@ -47,6 +50,27 @@ jobs: name: Dump context uses: crazy-max/ghaction-dump-context@v1 + multi: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2.3.1 + - + name: Set up Docker Buildx 1 + id: buildx1 + uses: ./setup-buildx/ + - + name: Builder 1 instance name + run: echo ${{ steps.buildx1.outputs.name }} + - + name: Set up Docker Buildx 2 + id: buildx2 + uses: ./setup-buildx/ + - + name: Builder 2 instance name + run: echo ${{ steps.buildx2.outputs.name }} + install: runs-on: ubuntu-latest steps: @@ -135,3 +159,6 @@ jobs: - name: Available platforms run: echo ${{ steps.buildx.outputs.platforms }} + - + name: Builder instance name + run: echo ${{ steps.buildx.outputs.name }} diff --git a/setup-buildx/README.md b/setup-buildx/README.md index a5cfeb9..fb6314a 100644 --- a/setup-buildx/README.md +++ b/setup-buildx/README.md @@ -77,6 +77,9 @@ jobs: uses: docker/actions/setup-buildx@v1 with: buildx-version: latest + - + name: Builder instance name + run: echo ${{ steps.buildx.outputs.name }} - name: Available platforms run: echo ${{ steps.buildx.outputs.platforms }} @@ -102,6 +105,7 @@ Following outputs are available | Name | Type | Description | |---------------|---------|---------------------------------------| +| `name` | String | Builder instance name | | `platforms` | String | Available platforms (comma separated) | ### environment variables diff --git a/setup-buildx/__tests__/buildx.test.ts b/setup-buildx/__tests__/buildx.test.ts index 65e8de6..b4f2612 100644 --- a/setup-buildx/__tests__/buildx.test.ts +++ b/setup-buildx/__tests__/buildx.test.ts @@ -8,7 +8,13 @@ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-')); describe('buildx', () => { it('is available', async () => { expect(await buildx.isAvailable()).toBe(true); - }, 100000); + }); + + it('count builders', async () => { + const countBuilders = await buildx.countBuilders(); + console.log(`countBuilders: ${countBuilders}`); + expect(countBuilders).toBeGreaterThan(0); + }); it('acquires v0.2.2 version of buildx', async () => { const buildxBin = await buildx.install('v0.2.2', tmpDir); diff --git a/setup-buildx/action.yml b/setup-buildx/action.yml index 1ee677d..73485dd 100644 --- a/setup-buildx/action.yml +++ b/setup-buildx/action.yml @@ -27,6 +27,8 @@ inputs: required: false outputs: + name: + description: 'Builder instance name' platforms: description: 'Available platforms (comma separated)' diff --git a/setup-buildx/dist/index.js b/setup-buildx/dist/index.js index d0bf75b..a654131 100644 --- a/setup-buildx/dist/index.js +++ b/setup-buildx/dist/index.js @@ -2501,15 +2501,11 @@ function run() { } core.info('📣 Buildx info'); yield exec.exec('docker', ['buildx', 'version'], false); + const builderName = `builder-${(yield buildx.countBuilders()) + 1}-${process.env.GITHUB_JOB}`; + core.saveState('builderName', builderName); + core.setOutput('name', builderName); core.info('🔨 Creating a new builder instance...'); - let createArgs = [ - 'buildx', - 'create', - '--name', - `builder-${process.env.GITHUB_SHA}`, - '--driver', - driver - ]; + let createArgs = ['buildx', 'create', '--name', builderName, '--driver', driver]; if (driverOpt) { createArgs.push('--driver-opt', driverOpt); } @@ -2547,7 +2543,7 @@ function cleanup() { return __awaiter(this, void 0, void 0, function* () { try { core.info('🚿 Removing builder instance...'); - yield exec.exec('docker', ['buildx', 'rm', `builder-${process.env.GITHUB_SHA}`], false); + yield exec.exec('docker', ['buildx', 'rm', `${process.env.STATE_builderName}`], false); } catch (error) { core.warning(error.message); @@ -7243,7 +7239,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.install = exports.isAvailable = void 0; +exports.install = exports.countBuilders = exports.isAvailable = void 0; const fs = __importStar(__webpack_require__(747)); const os = __importStar(__webpack_require__(87)); const path = __importStar(__webpack_require__(622)); @@ -7265,6 +7261,17 @@ function isAvailable() { }); } exports.isAvailable = isAvailable; +function countBuilders() { + return __awaiter(this, void 0, void 0, function* () { + return yield exec.exec(`docker`, ['buildx', 'ls'], true).then(res => { + if (res.stderr != '' && !res.success) { + throw new Error(`Cannot list builders: ${res.stderr}`); + } + return (res.stdout.trim().split(`\n`).length - 1) / 2; + }); + }); +} +exports.countBuilders = countBuilders; function install(inputVersion, dockerConfigHome) { return __awaiter(this, void 0, void 0, function* () { const release = yield github.getRelease(inputVersion); diff --git a/setup-buildx/src/buildx.ts b/setup-buildx/src/buildx.ts index 0e43fc7..9896be5 100644 --- a/setup-buildx/src/buildx.ts +++ b/setup-buildx/src/buildx.ts @@ -19,6 +19,15 @@ export async function isAvailable(): Promise { }); } +export async function countBuilders(): Promise { + return await exec.exec(`docker`, ['buildx', 'ls'], true).then(res => { + if (res.stderr != '' && !res.success) { + throw new Error(`Cannot list builders: ${res.stderr}`); + } + return (res.stdout.trim().split(`\n`).length - 1) / 2; + }); +} + export async function install(inputVersion: string, dockerConfigHome: string): Promise { const release: github.GitHubRelease | null = await github.getRelease(inputVersion); if (!release) { diff --git a/setup-buildx/src/main.ts b/setup-buildx/src/main.ts index c65efa1..9ab2442 100644 --- a/setup-buildx/src/main.ts +++ b/setup-buildx/src/main.ts @@ -27,15 +27,12 @@ async function run(): Promise { core.info('📣 Buildx info'); await exec.exec('docker', ['buildx', 'version'], false); + const builderName: string = `builder-${(await buildx.countBuilders()) + 1}-${process.env.GITHUB_JOB}`; + core.saveState('builderName', builderName); + core.setOutput('name', builderName); + core.info('🔨 Creating a new builder instance...'); - let createArgs: Array = [ - 'buildx', - 'create', - '--name', - `builder-${process.env.GITHUB_SHA}`, - '--driver', - driver - ]; + let createArgs: Array = ['buildx', 'create', '--name', builderName, '--driver', driver]; if (driverOpt) { createArgs.push('--driver-opt', driverOpt); } @@ -76,7 +73,7 @@ async function run(): Promise { async function cleanup(): Promise { try { core.info('🚿 Removing builder instance...'); - await exec.exec('docker', ['buildx', 'rm', `builder-${process.env.GITHUB_SHA}`], false); + await exec.exec('docker', ['buildx', 'rm', `${process.env.STATE_builderName}`], false); } catch (error) { core.warning(error.message); }