From 987520896fd1fc7777f5dd6125cd94e5ae49c9db Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 19 Feb 2023 02:12:29 +0100 Subject: [PATCH] switch to actions-toolkit implementation Signed-off-by: CrazyMax --- .eslintrc.json | 2 +- __tests__/auth.test.ts | 88 ------- __tests__/buildx.test.ts | 261 --------------------- __tests__/context.test.ts | 93 +------- __tests__/docker.test.ts | 16 -- __tests__/fixtures/buildkitd.toml | 3 - __tests__/fixtures/inspect1.txt | 10 - __tests__/fixtures/inspect2.txt | 11 - __tests__/fixtures/inspect3.txt | 11 - __tests__/fixtures/inspect4.txt | 9 - __tests__/fixtures/inspect5.txt | 9 - __tests__/fixtures/inspect6.txt | 9 - __tests__/git.test.ts | 9 - __tests__/util.test.ts | 12 - dev.Dockerfile | 1 + jest.config.ts | 18 +- package.json | 9 +- src/auth.ts | 51 ---- src/buildx.ts | 376 ------------------------------ src/context.ts | 86 ++----- src/docker.ts | 19 -- src/git.ts | 19 -- src/main.ts | 277 +++++++++++----------- src/nodes.ts | 13 -- src/state-helper.ts | 11 +- src/util.ts | 8 - tsconfig.json | 8 +- yarn.lock | 41 ++-- 28 files changed, 209 insertions(+), 1271 deletions(-) delete mode 100644 __tests__/auth.test.ts delete mode 100644 __tests__/buildx.test.ts delete mode 100644 __tests__/docker.test.ts delete mode 100644 __tests__/fixtures/buildkitd.toml delete mode 100644 __tests__/fixtures/inspect1.txt delete mode 100644 __tests__/fixtures/inspect2.txt delete mode 100644 __tests__/fixtures/inspect3.txt delete mode 100644 __tests__/fixtures/inspect4.txt delete mode 100644 __tests__/fixtures/inspect5.txt delete mode 100644 __tests__/fixtures/inspect6.txt delete mode 100644 __tests__/git.test.ts delete mode 100644 __tests__/util.test.ts delete mode 100644 src/auth.ts delete mode 100644 src/buildx.ts delete mode 100644 src/docker.ts delete mode 100644 src/git.ts delete mode 100644 src/nodes.ts delete mode 100644 src/util.ts diff --git a/.eslintrc.json b/.eslintrc.json index ba2a82b..b46613b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -2,7 +2,7 @@ "env": { "node": true, "es2021": true, - "jest/globals": true + "jest": true }, "extends": [ "eslint:recommended", diff --git a/__tests__/auth.test.ts b/__tests__/auth.test.ts deleted file mode 100644 index 46ce6bb..0000000 --- a/__tests__/auth.test.ts +++ /dev/null @@ -1,88 +0,0 @@ -import {describe, expect, test, beforeEach} from '@jest/globals'; -import * as fs from 'fs'; -import * as os from 'os'; -import * as path from 'path'; -import * as auth from '../src/auth'; - -const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-jest')).split(path.sep).join(path.posix.sep); -const dockerConfigHome = path.join(tmpdir, '.docker'); -const credsdir = path.join(dockerConfigHome, 'buildx', 'creds'); - -describe('setCredentials', () => { - beforeEach(() => { - process.env = Object.keys(process.env).reduce((object, key) => { - if (!key.startsWith(auth.envPrefix)) { - object[key] = process.env[key]; - } - return object; - }, {}); - }); - - // prettier-ignore - test.each([ - [ - 'mycontext', - 'docker-container', - {}, - [], - [] - ], - [ - 'docker-container://mycontainer', - 'docker-container', - {}, - [], - [] - ], - [ - 'tcp://graviton2:1234', - 'remote', - {}, - [], - [] - ], - [ - 'tcp://graviton2:1234', - 'remote', - { - 'BUILDER_NODE_0_AUTH_TLS_CACERT': 'foo', - 'BUILDER_NODE_0_AUTH_TLS_CERT': 'foo', - 'BUILDER_NODE_0_AUTH_TLS_KEY': 'foo' - }, - [ - path.join(credsdir, 'cacert_graviton2-1234.pem'), - path.join(credsdir, 'cert_graviton2-1234.pem'), - path.join(credsdir, 'key_graviton2-1234.pem') - ], - [ - `cacert=${path.join(credsdir, 'cacert_graviton2-1234.pem')}`, - `cert=${path.join(credsdir, 'cert_graviton2-1234.pem')}`, - `key=${path.join(credsdir, 'key_graviton2-1234.pem')}` - ] - ], - [ - 'tcp://graviton2:1234', - 'docker-container', - { - 'BUILDER_NODE_0_AUTH_TLS_CACERT': 'foo', - 'BUILDER_NODE_0_AUTH_TLS_CERT': 'foo', - 'BUILDER_NODE_0_AUTH_TLS_KEY': 'foo' - }, - [ - path.join(credsdir, 'cacert_graviton2-1234.pem'), - path.join(credsdir, 'cert_graviton2-1234.pem'), - path.join(credsdir, 'key_graviton2-1234.pem') - ], - [] - ], - ])('given %p endpoint', async (endpoint: string, driver: string, envs: Record, expectedFiles: Array, expectedOpts: Array) => { - fs.mkdirSync(credsdir, {recursive: true}); - for (const [key, value] of Object.entries(envs)) { - process.env[key] = value; - } - expect(auth.setCredentials(credsdir, 0, driver, endpoint)).toEqual(expectedOpts); - expectedFiles.forEach( (file) => { - expect(fs.existsSync(file)).toBe(true); - }); - }); -}); diff --git a/__tests__/buildx.test.ts b/__tests__/buildx.test.ts deleted file mode 100644 index 2399865..0000000 --- a/__tests__/buildx.test.ts +++ /dev/null @@ -1,261 +0,0 @@ -import {describe, expect, it, jest, test} from '@jest/globals'; -import * as fs from 'fs'; -import * as os from 'os'; -import * as path from 'path'; -import * as buildx from '../src/buildx'; -import * as context from '../src/context'; -import * as semver from 'semver'; -import * as exec from '@actions/exec'; - -const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep); -jest.spyOn(context, 'tmpDir').mockImplementation((): string => { - return tmpdir; -}); - -const tmpname = path.join(tmpdir, '.tmpname').split(path.sep).join(path.posix.sep); -jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => { - return tmpname; -}); - -describe('isAvailable', () => { - const execSpy = jest.spyOn(exec, 'getExecOutput'); - buildx.isAvailable(); - - // eslint-disable-next-line jest/no-standalone-expect - expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], { - silent: true, - ignoreReturnCode: true - }); -}); - -describe('isAvailable standalone', () => { - const execSpy = jest.spyOn(exec, 'getExecOutput'); - buildx.isAvailable(true); - - // eslint-disable-next-line jest/no-standalone-expect - expect(execSpy).toHaveBeenCalledWith(`buildx`, [], { - silent: true, - ignoreReturnCode: true - }); -}); - -describe('getVersion', () => { - it('valid', async () => { - const version = await buildx.getVersion(); - expect(semver.valid(version)).not.toBeNull(); - }); -}); - -describe('parseVersion', () => { - test.each([ - ['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'], - ['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'], - ['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'], - ['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971'] - ])('given %p', async (stdout, expected) => { - expect(buildx.parseVersion(stdout)).toEqual(expected); - }); -}); - -describe('satisfies', () => { - test.each([ - ['0.4.1', '>=0.3.2', true], - ['bda4882a65349ca359216b135896bddc1d92461c', '>0.1.0', false], - ['f117971', '>0.6.0', true] - ])('given %p', async (version, range, expected) => { - expect(buildx.satisfies(version, range)).toBe(expected); - }); -}); - -describe('inspect', () => { - it('valid', async () => { - const builder = await buildx.inspect(''); - expect(builder).not.toBeUndefined(); - expect(builder.name).not.toEqual(''); - expect(builder.driver).not.toEqual(''); - expect(builder.nodes).not.toEqual({}); - }, 100000); -}); - -describe('parseInspect', () => { - // prettier-ignore - test.each([ - [ - 'inspect1.txt', - { - "nodes": [ - { - "name": "builder-5cb467f7-0940-47e1-b94b-d51f54054d620", - "endpoint": "unix:///var/run/docker.sock", - "status": "running", - "buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host", - "buildkit": "v0.10.4", - "platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/amd64/v4,linux/arm64,linux/riscv64,linux/386,linux/arm/v7,linux/arm/v6" - } - ], - "name": "builder-5cb467f7-0940-47e1-b94b-d51f54054d62", - "driver": "docker-container" - } - ], - [ - 'inspect2.txt', - { - "nodes": [ - { - "name": "builder-5f449644-ff29-48af-8344-abb0292d06730", - "endpoint": "unix:///var/run/docker.sock", - "driver-opts": [ - "image=moby/buildkit:latest" - ], - "status": "running", - "buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host", - "buildkit": "v0.10.4", - "platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/amd64/v4,linux/386" - } - ], - "name": "builder-5f449644-ff29-48af-8344-abb0292d0673", - "driver": "docker-container" - } - ], - [ - 'inspect3.txt', - { - "nodes": [ - { - "name": "builder-9929e463-7954-4dc3-89cd-514cca29ff800", - "endpoint": "unix:///var/run/docker.sock", - "driver-opts": [ - "image=moby/buildkit:master", - "network=host" - ], - "status": "running", - "buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host", - "buildkit": "3fab389", - "platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/amd64/v4,linux/386" - } - ], - "name": "builder-9929e463-7954-4dc3-89cd-514cca29ff80", - "driver": "docker-container" - } - ], - [ - 'inspect4.txt', - { - "nodes": [ - { - "name": "default", - "endpoint": "default", - "status": "running", - "buildkit": "20.10.17", - "platforms": "linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6" - } - ], - "name": "default", - "driver": "docker" - } - ], - [ - 'inspect5.txt', - { - "nodes": [ - { - "name": "aws_graviton2", - "endpoint": "tcp://1.23.45.67:1234", - "driver-opts": [ - "cert=/home/user/.certs/aws_graviton2/cert.pem", - "key=/home/user/.certs/aws_graviton2/key.pem", - "cacert=/home/user/.certs/aws_graviton2/ca.pem" - ], - "status": "running", - "platforms": "darwin/arm64,linux/arm64,linux/arm/v5,linux/arm/v6,linux/arm/v7,windows/arm64" - } - ], - "name": "remote-builder", - "driver": "remote" - } - ], - [ - 'inspect6.txt', - { - "nodes": [ - { - "name": "builder-17cfff01-48d9-4c3d-9332-9992e308a5100", - "endpoint": "unix:///var/run/docker.sock", - "status": "running", - "buildkitd-flags": "--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host", - "platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/386" - } - ], - "name": "builder-17cfff01-48d9-4c3d-9332-9992e308a510", - "driver": "docker-container" - } - ], - ])('given %p', async (inspectFile, expected) => { - expect(await buildx.parseInspect(fs.readFileSync(path.join(__dirname, 'fixtures', inspectFile)).toString())).toEqual(expected); - }); -}); - -describe('build', () => { - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-')); - - // eslint-disable-next-line jest/no-disabled-tests - it.skip('builds refs/pull/648/head', async () => { - const buildxBin = await buildx.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir, false); - expect(fs.existsSync(buildxBin)).toBe(true); - }, 100000); - - // eslint-disable-next-line jest/no-disabled-tests - it.skip('builds 67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', async () => { - const buildxBin = await buildx.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', tmpDir, false); - expect(fs.existsSync(buildxBin)).toBe(true); - }, 100000); -}); - -describe('install', () => { - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-')); - test.each([ - ['v0.4.1', false], - ['latest', false], - ['v0.4.1', true], - ['latest', true] - ])( - 'acquires %p of buildx (standalone: %p)', - async (version, standalone) => { - const buildxBin = await buildx.install(version, tmpDir, standalone); - expect(fs.existsSync(buildxBin)).toBe(true); - }, - 100000 - ); -}); - -describe('getConfig', () => { - test.each([ - ['debug = true', false, 'debug = true', false], - [`notfound.toml`, true, '', true], - [ - `${path.join(__dirname, 'fixtures', 'buildkitd.toml').split(path.sep).join(path.posix.sep)}`, - true, - `debug = true -[registry."docker.io"] - mirrors = ["mirror.gcr.io"] -`, - false - ] - ])('given %p config', async (val, file, exValue, invalid) => { - try { - let config: string; - if (file) { - config = await buildx.getConfigFile(val); - } else { - config = await buildx.getConfigInline(val); - } - expect(true).toBe(!invalid); - expect(config).toEqual(tmpname); - const configValue = fs.readFileSync(tmpname, 'utf-8'); - expect(configValue).toEqual(exValue); - } catch (err) { - // eslint-disable-next-line jest/no-conditional-expect - expect(true).toBe(invalid); - } - }); -}); diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 3b83183..ca2c437 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -1,19 +1,9 @@ -import {beforeEach, describe, expect, it, jest, test} from '@jest/globals'; -import * as fs from 'fs'; -import * as os from 'os'; -import * as path from 'path'; +import {beforeEach, describe, expect, jest, test} from '@jest/globals'; import * as uuid from 'uuid'; +import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; +import {Node} from '@docker/actions-toolkit/lib/types/builder'; + import * as context from '../src/context'; -import * as nodes from '../src/nodes'; - -const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep); -jest.spyOn(context, 'tmpDir').mockImplementation((): string => { - return tmpdir; -}); - -jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => { - return path.join(tmpdir, '.tmpname').split(path.sep).join(path.posix.sep); -}); jest.mock('uuid'); jest.spyOn(uuid, 'v4').mockReturnValue('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'); @@ -146,7 +136,7 @@ describe('getCreateArgs', () => { setInput(name, value); }); const inp = await context.getInputs(); - const res = await context.getCreateArgs(inp, '0.9.0'); + const res = await context.getCreateArgs(inp, new Toolkit()); expect(res).toEqual(expected); } ); @@ -192,86 +182,17 @@ describe('getAppendArgs', () => { ] ])( '[%d] given %p as inputs, returns %p', - async (num: number, inputs: Map, node: nodes.Node, expected: Array) => { + async (num: number, inputs: Map, node: Node, expected: Array) => { inputs.forEach((value: string, name: string) => { setInput(name, value); }); const inp = await context.getInputs(); - const res = await context.getAppendArgs(inp, node, '0.9.0'); + const res = await context.getAppendArgs(inp, node, new Toolkit()); expect(res).toEqual(expected); } ); }); -describe('getInputList', () => { - it('handles single line correctly', async () => { - await setInput('foo', 'bar'); - const res = await context.getInputList('foo'); - expect(res).toEqual(['bar']); - }); - - it('handles multiple lines correctly', async () => { - setInput('foo', 'bar\nbaz'); - const res = await context.getInputList('foo'); - expect(res).toEqual(['bar', 'baz']); - }); - - it('remove empty lines correctly', async () => { - setInput('foo', 'bar\n\nbaz'); - const res = await context.getInputList('foo'); - expect(res).toEqual(['bar', 'baz']); - }); - - it('handles comma correctly', async () => { - setInput('foo', 'bar,baz'); - const res = await context.getInputList('foo'); - expect(res).toEqual(['bar', 'baz']); - }); - - it('remove empty result correctly', async () => { - setInput('foo', 'bar,baz,'); - const res = await context.getInputList('foo'); - expect(res).toEqual(['bar', 'baz']); - }); - - it('handles different new lines correctly', async () => { - setInput('foo', 'bar\r\nbaz'); - const res = await context.getInputList('foo'); - expect(res).toEqual(['bar', 'baz']); - }); - - it('handles different new lines and comma correctly', async () => { - setInput('foo', 'bar\r\nbaz,bat'); - const res = await context.getInputList('foo'); - expect(res).toEqual(['bar', 'baz', 'bat']); - }); - - it('handles multiple lines and ignoring comma correctly', async () => { - setInput('driver-opts', 'image=moby/buildkit:master\nnetwork=host'); - const res = await context.getInputList('driver-opts', true); - expect(res).toEqual(['image=moby/buildkit:master', 'network=host']); - }); - - it('handles different new lines and ignoring comma correctly', async () => { - setInput('driver-opts', 'image=moby/buildkit:master\r\nnetwork=host'); - const res = await context.getInputList('driver-opts', true); - expect(res).toEqual(['image=moby/buildkit:master', 'network=host']); - }); -}); - -describe('asyncForEach', () => { - it('executes async tasks sequentially', async () => { - const testValues = [1, 2, 3, 4, 5]; - const results: number[] = []; - - await context.asyncForEach(testValues, async value => { - results.push(value); - }); - - expect(results).toEqual(testValues); - }); -}); - // See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67 function getInputName(name: string): string { return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts deleted file mode 100644 index d1b7075..0000000 --- a/__tests__/docker.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {describe, expect, it, jest} from '@jest/globals'; -import * as docker from '../src/docker'; -import * as exec from '@actions/exec'; - -describe('isAvailable', () => { - it('cli', () => { - const execSpy = jest.spyOn(exec, 'getExecOutput'); - docker.isAvailable(); - - // eslint-disable-next-line jest/no-standalone-expect - expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, { - silent: true, - ignoreReturnCode: true - }); - }); -}); diff --git a/__tests__/fixtures/buildkitd.toml b/__tests__/fixtures/buildkitd.toml deleted file mode 100644 index d032d2a..0000000 --- a/__tests__/fixtures/buildkitd.toml +++ /dev/null @@ -1,3 +0,0 @@ -debug = true -[registry."docker.io"] - mirrors = ["mirror.gcr.io"] diff --git a/__tests__/fixtures/inspect1.txt b/__tests__/fixtures/inspect1.txt deleted file mode 100644 index 35c57b4..0000000 --- a/__tests__/fixtures/inspect1.txt +++ /dev/null @@ -1,10 +0,0 @@ -Name: builder-5cb467f7-0940-47e1-b94b-d51f54054d62 -Driver: docker-container - -Nodes: -Name: builder-5cb467f7-0940-47e1-b94b-d51f54054d620 -Endpoint: unix:///var/run/docker.sock -Status: running -Flags: --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host -Buildkit: v0.10.4 -Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/arm64, linux/riscv64, linux/386, linux/arm/v7, linux/arm/v6 diff --git a/__tests__/fixtures/inspect2.txt b/__tests__/fixtures/inspect2.txt deleted file mode 100644 index 6fbe0cb..0000000 --- a/__tests__/fixtures/inspect2.txt +++ /dev/null @@ -1,11 +0,0 @@ -Name: builder-5f449644-ff29-48af-8344-abb0292d0673 -Driver: docker-container - -Nodes: -Name: builder-5f449644-ff29-48af-8344-abb0292d06730 -Endpoint: unix:///var/run/docker.sock -Driver Options: image="moby/buildkit:latest" -Status: running -Flags: --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host -Buildkit: v0.10.4 -Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386 diff --git a/__tests__/fixtures/inspect3.txt b/__tests__/fixtures/inspect3.txt deleted file mode 100644 index a2ec5e2..0000000 --- a/__tests__/fixtures/inspect3.txt +++ /dev/null @@ -1,11 +0,0 @@ -Name: builder-9929e463-7954-4dc3-89cd-514cca29ff80 -Driver: docker-container - -Nodes: -Name: builder-9929e463-7954-4dc3-89cd-514cca29ff800 -Endpoint: unix:///var/run/docker.sock -Driver Options: image="moby/buildkit:master" network="host" -Status: running -Flags: --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host -Buildkit: 3fab389 -Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386 diff --git a/__tests__/fixtures/inspect4.txt b/__tests__/fixtures/inspect4.txt deleted file mode 100644 index 8a6ea90..0000000 --- a/__tests__/fixtures/inspect4.txt +++ /dev/null @@ -1,9 +0,0 @@ -Name: default -Driver: docker - -Nodes: -Name: default -Endpoint: default -Status: running -Buildkit: 20.10.17 -Platforms: linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6 diff --git a/__tests__/fixtures/inspect5.txt b/__tests__/fixtures/inspect5.txt deleted file mode 100644 index 6c17e5d..0000000 --- a/__tests__/fixtures/inspect5.txt +++ /dev/null @@ -1,9 +0,0 @@ -Name: remote-builder -Driver: remote - -Nodes: -Name: aws_graviton2 -Endpoint: tcp://1.23.45.67:1234 -Driver Options: cert="/home/user/.certs/aws_graviton2/cert.pem" key="/home/user/.certs/aws_graviton2/key.pem" cacert="/home/user/.certs/aws_graviton2/ca.pem" -Status: running -Platforms: darwin/arm64*, linux/arm64*, linux/arm/v5*, linux/arm/v6*, linux/arm/v7*, windows/arm64*, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/mips64le, linux/mips64 diff --git a/__tests__/fixtures/inspect6.txt b/__tests__/fixtures/inspect6.txt deleted file mode 100644 index eba8df3..0000000 --- a/__tests__/fixtures/inspect6.txt +++ /dev/null @@ -1,9 +0,0 @@ -Name: builder-17cfff01-48d9-4c3d-9332-9992e308a510 -Driver: docker-container - -Nodes: -Name: builder-17cfff01-48d9-4c3d-9332-9992e308a5100 -Endpoint: unix:///var/run/docker.sock -Status: running -Flags: --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host -Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/386 diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts deleted file mode 100644 index 98694d2..0000000 --- a/__tests__/git.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {describe, expect, it} from '@jest/globals'; -import * as git from '../src/git'; - -describe('git', () => { - it('returns git remote ref', async () => { - const ref: string = await git.getRemoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head'); - expect(ref).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa'); - }); -}); diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts deleted file mode 100644 index b3eab6a..0000000 --- a/__tests__/util.test.ts +++ /dev/null @@ -1,12 +0,0 @@ -import {describe, expect, test} from '@jest/globals'; -import * as util from '../src/util'; - -describe('isValidUrl', () => { - test.each([ - ['https://github.com/docker/buildx.git', true], - ['https://github.com/docker/buildx.git#refs/pull/648/head', true], - ['v0.4.1', false] - ])('given %p', async (url, expected) => { - expect(util.isValidUrl(url)).toEqual(expected); - }); -}); diff --git a/dev.Dockerfile b/dev.Dockerfile index 43b44a1..ac9c89a 100644 --- a/dev.Dockerfile +++ b/dev.Dockerfile @@ -71,6 +71,7 @@ ENV RUNNER_TOOL_CACHE=/tmp/github_tool_cache RUN --mount=type=bind,target=.,rw \ --mount=type=cache,target=/src/node_modules \ --mount=type=bind,from=docker,source=/usr/local/bin/docker,target=/usr/bin/docker \ + --mount=type=bind,from=buildx,source=/buildx,target=/usr/bin/buildx \ --mount=type=bind,from=buildx,source=/buildx,target=/usr/libexec/docker/cli-plugins/docker-buildx \ yarn run test --coverageDirectory=/tmp/coverage diff --git a/jest.config.ts b/jest.config.ts index 7ff4e46..5a901df 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -1,7 +1,21 @@ +import fs from 'fs'; +import os from 'os'; +import path from 'path'; + +const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-action-')).split(path.sep).join(path.posix.sep); + +process.env = Object.assign({}, process.env, { + TEMP: tmpDir, + GITHUB_REPOSITORY: 'docker/setup-buildx-action', + RUNNER_TEMP: path.join(tmpDir, 'runner-temp').split(path.sep).join(path.posix.sep), + RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache').split(path.sep).join(path.posix.sep) +}) as { + [key: string]: string; +}; + module.exports = { clearMocks: true, moduleFileExtensions: ['js', 'ts'], - setupFiles: ['dotenv/config'], testMatch: ['**/*.test.ts'], transform: { '^.+\\.ts$': 'ts-jest' @@ -9,5 +23,7 @@ module.exports = { moduleNameMapper: { '^csv-parse/sync': '/node_modules/csv-parse/dist/cjs/sync.cjs' }, + collectCoverageFrom: ['src/**/{!(main.ts),}.ts'], + coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__tests__/'], verbose: true }; diff --git a/package.json b/package.json index 9f5fd09..6f86676 100644 --- a/package.json +++ b/package.json @@ -29,22 +29,15 @@ "dependencies": { "@actions/core": "^1.10.0", "@actions/exec": "^1.1.1", - "@actions/tool-cache": "^2.0.1", - "@docker/actions-toolkit": "^0.1.0-beta.4", - "csv-parse": "^5.3.4", + "@docker/actions-toolkit": "^0.1.0-beta.15", "js-yaml": "^4.1.0", - "semver": "^7.3.7", - "tmp": "^0.2.1", "uuid": "^9.0.0" }, "devDependencies": { "@types/node": "^16.11.26", - "@types/semver": "^7.3.9", - "@types/tmp": "^0.2.3", "@typescript-eslint/eslint-plugin": "^5.14.0", "@typescript-eslint/parser": "^5.14.0", "@vercel/ncc": "^0.33.3", - "dotenv": "^16.0.0", "eslint": "^8.11.0", "eslint-config-prettier": "^8.5.0", "eslint-plugin-jest": "^26.1.1", diff --git a/src/auth.ts b/src/auth.ts deleted file mode 100644 index a7e543c..0000000 --- a/src/auth.ts +++ /dev/null @@ -1,51 +0,0 @@ -import * as fs from 'fs'; - -export const envPrefix = 'BUILDER_NODE'; - -export function setCredentials(credsdir: string, index: number, driver: string, endpoint: string): Array { - let url: URL; - try { - url = new URL(endpoint); - } catch (e) { - return []; - } - switch (url.protocol) { - case 'tcp:': { - return setBuildKitClientCerts(credsdir, index, driver, url); - } - } - return []; -} - -function setBuildKitClientCerts(credsdir: string, index: number, driver: string, endpoint: URL): Array { - const driverOpts: Array = []; - const buildkitCacert = process.env[`${envPrefix}_${index}_AUTH_TLS_CACERT`] || ''; - const buildkitCert = process.env[`${envPrefix}_${index}_AUTH_TLS_CERT`] || ''; - const buildkitKey = process.env[`${envPrefix}_${index}_AUTH_TLS_KEY`] || ''; - if (buildkitCacert.length == 0 && buildkitCert.length == 0 && buildkitKey.length == 0) { - return driverOpts; - } - let host = endpoint.hostname; - if (endpoint.port.length > 0) { - host += `-${endpoint.port}`; - } - if (buildkitCacert.length > 0) { - const cacertpath = `${credsdir}/cacert_${host}.pem`; - fs.writeFileSync(cacertpath, buildkitCacert); - driverOpts.push(`cacert=${cacertpath}`); - } - if (buildkitCert.length > 0) { - const certpath = `${credsdir}/cert_${host}.pem`; - fs.writeFileSync(certpath, buildkitCert); - driverOpts.push(`cert=${certpath}`); - } - if (buildkitKey.length > 0) { - const keypath = `${credsdir}/key_${host}.pem`; - fs.writeFileSync(keypath, buildkitKey); - driverOpts.push(`key=${keypath}`); - } - if (driver != 'remote') { - return []; - } - return driverOpts; -} diff --git a/src/buildx.ts b/src/buildx.ts deleted file mode 100644 index a609eb3..0000000 --- a/src/buildx.ts +++ /dev/null @@ -1,376 +0,0 @@ -import * as fs from 'fs'; -import * as path from 'path'; -import * as semver from 'semver'; -import * as util from 'util'; -import * as context from './context'; -import * as git from './git'; -import * as core from '@actions/core'; -import * as exec from '@actions/exec'; -import * as tc from '@actions/tool-cache'; -import {Install as BuildxInstall} from '@docker/actions-toolkit/lib/buildx/install'; -import {GitHubRelease} from '@docker/actions-toolkit/lib/types/github'; - -export type Builder = { - name?: string; - driver?: string; - nodes: Node[]; -}; - -export type Node = { - name?: string; - endpoint?: string; - 'driver-opts'?: Array; - status?: string; - 'buildkitd-flags'?: string; - buildkit?: string; - platforms?: string; -}; - -export async function getConfigInline(s: string): Promise { - return getConfig(s, false); -} - -export async function getConfigFile(s: string): Promise { - return getConfig(s, true); -} - -export async function getConfig(s: string, file: boolean): Promise { - if (file) { - if (!fs.existsSync(s)) { - throw new Error(`config file ${s} not found`); - } - s = fs.readFileSync(s, {encoding: 'utf-8'}); - } - const configFile = context.tmpNameSync({ - tmpdir: context.tmpDir() - }); - fs.writeFileSync(configFile, s); - return configFile; -} - -export async function isAvailable(standalone?: boolean): Promise { - const cmd = getCommand([], standalone); - return await exec - .getExecOutput(cmd.commandLine, cmd.args, { - ignoreReturnCode: true, - silent: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - return false; - } - return res.exitCode == 0; - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .catch(error => { - return false; - }); -} - -export async function getVersion(standalone?: boolean): Promise { - const cmd = getCommand(['version'], standalone); - return await exec - .getExecOutput(cmd.commandLine, cmd.args, { - ignoreReturnCode: true, - silent: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - throw new Error(res.stderr.trim()); - } - return parseVersion(res.stdout.trim()); - }); -} - -export function parseVersion(stdout: string): string { - const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout); - if (!matches) { - throw new Error(`Cannot parse buildx version`); - } - return matches[1]; -} - -export function satisfies(version: string, range: string): boolean { - return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null; -} - -export async function inspect(name: string, standalone?: boolean): Promise { - const cmd = getCommand(['inspect', name], standalone); - return await exec - .getExecOutput(cmd.commandLine, cmd.args, { - ignoreReturnCode: true, - silent: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - throw new Error(res.stderr.trim()); - } - return parseInspect(res.stdout); - }); -} - -export async function parseInspect(data: string): Promise { - const builder: Builder = { - nodes: [] - }; - let node: Node = {}; - for (const line of data.trim().split(`\n`)) { - const [key, ...rest] = line.split(':'); - const value = rest.map(v => v.trim()).join(':'); - if (key.length == 0 || value.length == 0) { - continue; - } - switch (key.toLowerCase()) { - case 'name': { - if (builder.name == undefined) { - builder.name = value; - } else { - if (Object.keys(node).length > 0) { - builder.nodes.push(node); - node = {}; - } - node.name = value; - } - break; - } - case 'driver': { - builder.driver = value; - break; - } - case 'endpoint': { - node.endpoint = value; - break; - } - case 'driver options': { - node['driver-opts'] = (value.match(/(\w+)="([^"]*)"/g) || []).map(v => v.replace(/^(.*)="(.*)"$/g, '$1=$2')); - break; - } - case 'status': { - node.status = value; - break; - } - case 'flags': { - node['buildkitd-flags'] = value; - break; - } - case 'buildkit': { - node.buildkit = value; - break; - } - case 'platforms': { - let platforms: Array = []; - // if a preferred platform is being set then use only these - // https://docs.docker.com/engine/reference/commandline/buildx_inspect/#get-information-about-a-builder-instance - if (value.includes('*')) { - for (const platform of value.split(', ')) { - if (platform.includes('*')) { - platforms.push(platform.replace('*', '')); - } - } - } else { - // otherwise set all platforms available - platforms = value.split(', '); - } - node.platforms = platforms.join(','); - break; - } - } - } - if (Object.keys(node).length > 0) { - builder.nodes.push(node); - } - return builder; -} - -export async function build(inputBuildRef: string, dest: string, standalone: boolean): Promise { - // eslint-disable-next-line prefer-const - let [repo, ref] = inputBuildRef.split('#'); - if (ref.length == 0) { - ref = 'master'; - } - - let vspec: string; - if (ref.match(/^[0-9a-fA-F]{40}$/)) { - vspec = ref; - } else { - vspec = await git.getRemoteSha(repo, ref); - } - core.debug(`Tool version spec ${vspec}`); - - let toolPath: string; - toolPath = tc.find('buildx', vspec); - if (!toolPath) { - const outFolder = path.join(context.tmpDir(), 'out').split(path.sep).join(path.posix.sep); - let buildWithStandalone = false; - const standaloneFound = await isAvailable(true); - const pluginFound = await isAvailable(false); - if (standalone && standaloneFound) { - core.debug(`Buildx standalone found, build with it`); - buildWithStandalone = true; - } else if (!standalone && pluginFound) { - core.debug(`Buildx plugin found, build with it`); - buildWithStandalone = false; - } else if (standaloneFound) { - core.debug(`Buildx plugin not found, but standalone found so trying to build with it`); - buildWithStandalone = true; - } else if (pluginFound) { - core.debug(`Buildx standalone not found, but plugin found so trying to build with it`); - buildWithStandalone = false; - } else { - throw new Error(`Neither buildx standalone or plugin have been found to build from ref`); - } - const buildCmd = getCommand(['build', '--target', 'binaries', '--build-arg', 'BUILDKIT_CONTEXT_KEEP_GIT_DIR=1', '--output', `type=local,dest=${outFolder}`, inputBuildRef], buildWithStandalone); - toolPath = await exec - .getExecOutput(buildCmd.commandLine, buildCmd.args, { - ignoreReturnCode: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - core.warning(res.stderr.trim()); - } - return tc.cacheFile(`${outFolder}/buildx`, context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx', 'buildx', vspec); - }); - } - - if (standalone) { - return setStandalone(toolPath, dest); - } - return setPlugin(toolPath, dest); -} - -export async function install(inputVersion: string, dest: string, standalone: boolean): Promise { - const release: GitHubRelease = await BuildxInstall.getRelease(inputVersion); - core.debug(`Release ${release.tag_name} found`); - const version = release.tag_name.replace(/^v+|v+$/g, ''); - - let toolPath: string; - toolPath = tc.find('buildx', version); - if (!toolPath) { - const c = semver.clean(version) || ''; - if (!semver.valid(c)) { - throw new Error(`Invalid Buildx version "${version}".`); - } - toolPath = await download(version); - } - - if (standalone) { - return setStandalone(toolPath, dest); - } - return setPlugin(toolPath, dest); -} - -async function setStandalone(toolPath: string, dest: string): Promise { - core.info('Standalone mode'); - const toolBinPath = path.join(toolPath, context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); - - const binDir = path.join(dest, 'bin'); - core.debug(`Bin dir is ${binDir}`); - if (!fs.existsSync(binDir)) { - fs.mkdirSync(binDir, {recursive: true}); - } - - const filename: string = context.osPlat == 'win32' ? 'buildx.exe' : 'buildx'; - const buildxPath: string = path.join(binDir, filename); - core.debug(`Bin path is ${buildxPath}`); - fs.copyFileSync(toolBinPath, buildxPath); - - core.info('Fixing perms'); - fs.chmodSync(buildxPath, '0755'); - - core.addPath(binDir); - core.info('Added buildx to the path'); - - return buildxPath; -} - -async function setPlugin(toolPath: string, dockerConfigHome: string): Promise { - core.info('Docker plugin mode'); - const toolBinPath = path.join(toolPath, context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); - - const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins'); - core.debug(`Plugins dir is ${pluginsDir}`); - if (!fs.existsSync(pluginsDir)) { - fs.mkdirSync(pluginsDir, {recursive: true}); - } - - const filename: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; - const pluginPath: string = path.join(pluginsDir, filename); - core.debug(`Plugin path is ${pluginPath}`); - fs.copyFileSync(toolBinPath, pluginPath); - - core.info('Fixing perms'); - fs.chmodSync(pluginPath, '0755'); - - return pluginPath; -} - -async function download(version: string): Promise { - const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; - const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, await filename(version)); - core.info(`Downloading ${downloadUrl}`); - const downloadPath = await tc.downloadTool(downloadUrl); - core.debug(`Downloaded to ${downloadPath}`); - return await tc.cacheFile(downloadPath, targetFile, 'buildx', version); -} - -async function filename(version: string): Promise { - let arch: string; - switch (context.osArch) { - case 'x64': { - arch = 'amd64'; - break; - } - case 'ppc64': { - arch = 'ppc64le'; - break; - } - case 'arm': { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const arm_version = (process.config.variables as any).arm_version; - arch = arm_version ? 'arm-v' + arm_version : 'arm'; - break; - } - default: { - arch = context.osArch; - break; - } - } - const platform: string = context.osPlat == 'win32' ? 'windows' : context.osPlat; - const ext: string = context.osPlat == 'win32' ? '.exe' : ''; - return util.format('buildx-v%s.%s-%s%s', version, platform, arch, ext); -} - -export async function getBuildKitVersion(containerID: string): Promise { - return exec - .getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', containerID], { - ignoreReturnCode: true, - silent: true - }) - .then(bkitimage => { - if (bkitimage.exitCode == 0 && bkitimage.stdout.length > 0) { - return exec - .getExecOutput(`docker`, ['run', '--rm', bkitimage.stdout.trim(), '--version'], { - ignoreReturnCode: true, - silent: true - }) - .then(bkitversion => { - if (bkitversion.exitCode == 0 && bkitversion.stdout.length > 0) { - return `${bkitimage.stdout.trim()} => ${bkitversion.stdout.trim()}`; - } else if (bkitversion.stderr.length > 0) { - core.warning(bkitversion.stderr.trim()); - } - return bkitversion.stdout.trim(); - }); - } else if (bkitimage.stderr.length > 0) { - core.warning(bkitimage.stderr.trim()); - } - return bkitimage.stdout.trim(); - }); -} - -export function getCommand(args: Array, standalone?: boolean) { - return { - commandLine: standalone ? 'buildx' : 'docker', - args: standalone ? args : ['buildx', ...args] - }; -} diff --git a/src/context.ts b/src/context.ts index 5146bc1..f527fe5 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,27 +1,10 @@ -import fs from 'fs'; -import * as os from 'os'; -import path from 'path'; -import * as tmp from 'tmp'; import * as uuid from 'uuid'; -import {parse} from 'csv-parse/sync'; -import * as buildx from './buildx'; -import * as nodes from './nodes'; import * as core from '@actions/core'; +import {Util} from '@docker/actions-toolkit/lib/util'; +import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; +import {Node} from '@docker/actions-toolkit/lib/types/builder'; -let _tmpDir: string; -export const osPlat: string = os.platform(); -export const osArch: string = os.arch(); - -export function tmpDir(): string { - if (!_tmpDir) { - _tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep); - } - return _tmpDir; -} - -export function tmpNameSync(options?: tmp.TmpNameOptions): string { - return tmp.tmpNameSync(options); -} +export const builderNodeEnvPrefix = 'BUILDER_NODE'; export interface Inputs { version: string; @@ -43,9 +26,9 @@ export async function getInputs(): Promise { version: core.getInput('version'), name: getBuilderName(core.getInput('driver') || 'docker-container'), driver: core.getInput('driver') || 'docker-container', - driverOpts: await getInputList('driver-opts', true), + driverOpts: Util.getInputList('driver-opts', {ignoreComma: true, quote: false}), buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host', - platforms: await getInputList('platforms', false, true), + platforms: Util.getInputList('platforms'), install: core.getBooleanInput('install'), use: core.getBooleanInput('use'), endpoint: core.getInput('endpoint'), @@ -59,10 +42,10 @@ export function getBuilderName(driver: string): string { return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`; } -export async function getCreateArgs(inputs: Inputs, buildxVersion: string): Promise> { +export async function getCreateArgs(inputs: Inputs, toolkit: Toolkit): Promise> { const args: Array = ['create', '--name', inputs.name, '--driver', inputs.driver]; - if (buildx.satisfies(buildxVersion, '>=0.3.0')) { - await asyncForEach(inputs.driverOpts, async driverOpt => { + if (await toolkit.buildx.versionSatisfies('>=0.3.0')) { + await Util.asyncForEach(inputs.driverOpts, async driverOpt => { args.push('--driver-opt', driverOpt); }); if (inputs.driver != 'remote' && inputs.buildkitdFlags) { @@ -77,9 +60,9 @@ export async function getCreateArgs(inputs: Inputs, buildxVersion: string): Prom } if (inputs.driver != 'remote') { if (inputs.config) { - args.push('--config', await buildx.getConfigFile(inputs.config)); + args.push('--config', toolkit.buildkit.config.resolveFromFile(inputs.config)); } else if (inputs.configInline) { - args.push('--config', await buildx.getConfigInline(inputs.configInline)); + args.push('--config', toolkit.buildkit.config.resolveFromString(inputs.configInline)); } } if (inputs.endpoint) { @@ -88,13 +71,13 @@ export async function getCreateArgs(inputs: Inputs, buildxVersion: string): Prom return args; } -export async function getAppendArgs(inputs: Inputs, node: nodes.Node, buildxVersion: string): Promise> { +export async function getAppendArgs(inputs: Inputs, node: Node, toolkit: Toolkit): Promise> { const args: Array = ['create', '--name', inputs.name, '--append']; if (node.name) { args.push('--node', node.name); } - if (node['driver-opts'] && buildx.satisfies(buildxVersion, '>=0.3.0')) { - await asyncForEach(node['driver-opts'], async driverOpt => { + if (node['driver-opts'] && (await toolkit.buildx.versionSatisfies('>=0.3.0'))) { + await Util.asyncForEach(node['driver-opts'], async driverOpt => { args.push('--driver-opt', driverOpt); }); if (inputs.driver != 'remote' && node['buildkitd-flags']) { @@ -110,47 +93,10 @@ export async function getAppendArgs(inputs: Inputs, node: nodes.Node, buildxVers return args; } -export async function getInspectArgs(inputs: Inputs, buildxVersion: string): Promise> { +export async function getInspectArgs(inputs: Inputs, toolkit: Toolkit): Promise> { const args: Array = ['inspect', '--bootstrap']; - if (buildx.satisfies(buildxVersion, '>=0.4.0')) { + if (await toolkit.buildx.versionSatisfies('>=0.4.0')) { args.push('--builder', inputs.name); } return args; } - -export async function getInputList(name: string, ignoreComma?: boolean, escapeQuotes?: boolean): Promise { - const res: Array = []; - - const items = core.getInput(name); - if (items == '') { - return res; - } - - const records = parse(items, { - columns: false, - relaxQuotes: true, - comment: '#', - relaxColumnCount: true, - skipEmptyLines: true, - quote: escapeQuotes ? `"` : false - }); - - for (const record of records as Array) { - if (record.length == 1) { - res.push(record[0]); - continue; - } else if (!ignoreComma) { - res.push(...record); - continue; - } - res.push(record.join(',')); - } - - return res.filter(item => item).map(pat => pat.trim()); -} - -export const asyncForEach = async (array, callback) => { - for (let index = 0; index < array.length; index++) { - await callback(array[index], index, array); - } -}; diff --git a/src/docker.ts b/src/docker.ts deleted file mode 100644 index 46497b2..0000000 --- a/src/docker.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as exec from '@actions/exec'; - -export async function isAvailable(): Promise { - return await exec - .getExecOutput('docker', undefined, { - ignoreReturnCode: true, - silent: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - return false; - } - return res.exitCode == 0; - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .catch(error => { - return false; - }); -} diff --git a/src/git.ts b/src/git.ts deleted file mode 100644 index 9369af8..0000000 --- a/src/git.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as exec from '@actions/exec'; - -export async function getRemoteSha(repo: string, ref: string): Promise { - return await exec - .getExecOutput(`git`, ['ls-remote', repo, ref], { - ignoreReturnCode: true, - silent: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - throw new Error(res.stderr); - } - const [rsha] = res.stdout.trim().split(/[\s\t]/); - if (rsha.length == 0) { - throw new Error(`Cannot find remote ref for ${repo}#${ref}`); - } - return rsha; - }); -} diff --git a/src/main.ts b/src/main.ts index 46f54d1..c5cae20 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,182 +1,189 @@ import * as fs from 'fs'; -import * as os from 'os'; -import * as path from 'path'; -import * as auth from './auth'; -import * as buildx from './buildx'; -import * as context from './context'; -import * as docker from './docker'; -import * as nodes from './nodes'; -import * as stateHelper from './state-helper'; -import * as util from './util'; +import * as yaml from 'js-yaml'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; +import * as actionsToolkit from '@docker/actions-toolkit'; +import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx'; +import {Docker} from '@docker/actions-toolkit/lib/docker'; +import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; +import {Util} from '@docker/actions-toolkit/lib/util'; +import {Node} from '@docker/actions-toolkit/lib/types/builder'; -async function run(): Promise { - try { +import * as context from './context'; +import * as stateHelper from './state-helper'; + +actionsToolkit.run( + // main + async () => { const inputs: context.Inputs = await context.getInputs(); - const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); + const toolkit = new Toolkit(); - // standalone if docker cli not available - const standalone = !(await docker.isAvailable()); + const standalone = await toolkit.buildx.isStandalone(); stateHelper.setStandalone(standalone); - core.startGroup(`Docker info`); - if (standalone) { - core.info(`Docker info skipped in standalone mode`); - } else { - await exec.exec('docker', ['version'], { - failOnStdErr: false - }); - await exec.exec('docker', ['info'], { - failOnStdErr: false - }); - } - core.endGroup(); + await core.group(`Docker info`, async () => { + try { + await Docker.printVersion(); + await Docker.printInfo(); + } catch (e) { + core.info(e.message); + } + }); - if (util.isValidUrl(inputs.version)) { + let toolPath; + if (Util.isValidUrl(inputs.version)) { if (standalone) { throw new Error(`Cannot build from source without the Docker CLI`); } - core.startGroup(`Build and install buildx`); - await buildx.build(inputs.version, dockerConfigHome, standalone); - core.endGroup(); - } else if (!(await buildx.isAvailable(standalone)) || inputs.version) { - core.startGroup(`Download and install buildx`); - await buildx.install(inputs.version || 'latest', standalone ? context.tmpDir() : dockerConfigHome, standalone); - core.endGroup(); + await core.group(`Build buildx from source`, async () => { + toolPath = await toolkit.buildxInstall.build(inputs.version); + }); + } else if (!(await toolkit.buildx.isAvailable()) || inputs.version) { + await core.group(`Download buildx from GitHub Releases`, async () => { + toolPath = await toolkit.buildxInstall.download(inputs.version || 'latest'); + }); + } + if (toolPath) { + await core.group(`Install buildx`, async () => { + if (standalone) { + await toolkit.buildxInstall.installStandalone(toolPath); + } else { + await toolkit.buildxInstall.installPlugin(toolPath); + } + }); } - const buildxVersion = await buildx.getVersion(standalone); await core.group(`Buildx version`, async () => { - const versionCmd = buildx.getCommand(['version'], standalone); - await exec.exec(versionCmd.commandLine, versionCmd.args, { - failOnStdErr: false - }); + await toolkit.buildx.printVersion(); }); core.setOutput('name', inputs.name); stateHelper.setBuilderName(inputs.name); - const credsdir = path.join(dockerConfigHome, 'buildx', 'creds', inputs.name); - fs.mkdirSync(credsdir, {recursive: true}); - stateHelper.setCredsDir(credsdir); + fs.mkdirSync(Buildx.certsDir, {recursive: true}); + stateHelper.setCertsDir(Buildx.certsDir); if (inputs.driver !== 'docker') { - core.startGroup(`Creating a new builder instance`); - const authOpts = auth.setCredentials(credsdir, 0, inputs.driver, inputs.endpoint); - if (authOpts.length > 0) { - inputs.driverOpts = [...inputs.driverOpts, ...authOpts]; - } - const createCmd = buildx.getCommand(await context.getCreateArgs(inputs, buildxVersion), standalone); - await exec.exec(createCmd.commandLine, createCmd.args); - core.endGroup(); + await core.group(`Creating a new builder instance`, async () => { + const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, inputs.endpoint, { + cacert: process.env[`${context.builderNodeEnvPrefix}_0_AUTH_TLS_CACERT`], + cert: process.env[`${context.builderNodeEnvPrefix}_0_AUTH_TLS_CERT`], + key: process.env[`${context.builderNodeEnvPrefix}_0_AUTH_TLS_KEY`] + }); + if (certsDriverOpts.length > 0) { + inputs.driverOpts = [...inputs.driverOpts, ...certsDriverOpts]; + } + const createCmd = await toolkit.buildx.getCommand(await context.getCreateArgs(inputs, toolkit)); + await exec.exec(createCmd.command, createCmd.args); + }); } if (inputs.append) { - core.startGroup(`Appending node(s) to builder`); - let nodeIndex = 1; - for (const node of nodes.Parse(inputs.append)) { - const authOpts = auth.setCredentials(credsdir, nodeIndex, inputs.driver, node.endpoint || ''); - if (authOpts.length > 0) { - node['driver-opts'] = [...(node['driver-opts'] || []), ...authOpts]; + await core.group(`Appending node(s) to builder`, async () => { + let nodeIndex = 1; + const nodes = yaml.load(inputs.append) as Node[]; + for (const node of nodes) { + const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, `${node.endpoint}`, { + cacert: process.env[`${context.builderNodeEnvPrefix}_${nodeIndex}_AUTH_TLS_CACERT`], + cert: process.env[`${context.builderNodeEnvPrefix}_${nodeIndex}_AUTH_TLS_CERT`], + key: process.env[`${context.builderNodeEnvPrefix}_${nodeIndex}_AUTH_TLS_KEY`] + }); + if (certsDriverOpts.length > 0) { + node['driver-opts'] = [...(node['driver-opts'] || []), ...certsDriverOpts]; + } + const appendCmd = await toolkit.buildx.getCommand(await context.getAppendArgs(inputs, node, toolkit)); + await exec.exec(appendCmd.command, appendCmd.args); + nodeIndex++; } - const appendCmd = buildx.getCommand(await context.getAppendArgs(inputs, node, buildxVersion), standalone); - await exec.exec(appendCmd.commandLine, appendCmd.args); - nodeIndex++; - } - core.endGroup(); + }); } - core.startGroup(`Booting builder`); - const inspectCmd = buildx.getCommand(await context.getInspectArgs(inputs, buildxVersion), standalone); - await exec.exec(inspectCmd.commandLine, inspectCmd.args); - core.endGroup(); + await core.group(`Booting builder`, async () => { + const inspectCmd = await toolkit.buildx.getCommand(await context.getInspectArgs(inputs, toolkit)); + await exec.exec(inspectCmd.command, inspectCmd.args); + }); if (inputs.install) { if (standalone) { throw new Error(`Cannot set buildx as default builder without the Docker CLI`); } - core.startGroup(`Setting buildx as default builder`); - await exec.exec('docker', ['buildx', 'install']); - core.endGroup(); + await core.group(`Setting buildx as default builder`, async () => { + const installCmd = await toolkit.buildx.getCommand(['install']); + await exec.exec(installCmd.command, installCmd.args); + }); } - core.startGroup(`Inspect builder`); - const builder = await buildx.inspect(inputs.name, standalone); - const firstNode = builder.nodes[0]; - const reducedPlatforms: Array = []; - for (const node of builder.nodes) { - for (const platform of node.platforms?.split(',') || []) { - if (reducedPlatforms.indexOf(platform) > -1) { - continue; + const builderInspect = await toolkit.builder.inspect(inputs.name); + const firstNode = builderInspect.nodes[0]; + + await core.group(`Inspect builder`, async () => { + const reducedPlatforms: Array = []; + for (const node of builderInspect.nodes) { + for (const platform of node.platforms?.split(',') || []) { + if (reducedPlatforms.indexOf(platform) > -1) { + continue; + } + reducedPlatforms.push(platform); } - reducedPlatforms.push(platform); } - } - core.info(JSON.stringify(builder, undefined, 2)); - core.setOutput('driver', builder.driver); - core.setOutput('platforms', reducedPlatforms.join(',')); - core.setOutput('nodes', JSON.stringify(builder.nodes, undefined, 2)); - core.setOutput('endpoint', firstNode.endpoint); // TODO: deprecated, to be removed in a later version - core.setOutput('status', firstNode.status); // TODO: deprecated, to be removed in a later version - core.setOutput('flags', firstNode['buildkitd-flags']); // TODO: deprecated, to be removed in a later version - core.endGroup(); + core.info(JSON.stringify(builderInspect, undefined, 2)); + core.setOutput('driver', builderInspect.driver); + core.setOutput('platforms', reducedPlatforms.join(',')); + core.setOutput('nodes', JSON.stringify(builderInspect.nodes, undefined, 2)); + core.setOutput('endpoint', firstNode.endpoint); // TODO: deprecated, to be removed in a later version + core.setOutput('status', firstNode.status); // TODO: deprecated, to be removed in a later version + core.setOutput('flags', firstNode['buildkitd-flags']); // TODO: deprecated, to be removed in a later version + }); - if (!standalone && builder.driver == 'docker-container') { - stateHelper.setContainerName(`buildx_buildkit_${firstNode.name}`); - core.startGroup(`BuildKit version`); - for (const node of builder.nodes) { - const bkvers = await buildx.getBuildKitVersion(`buildx_buildkit_${node.name}`); - core.info(`${node.name}: ${bkvers}`); - } - core.endGroup(); + if (!standalone && builderInspect.driver == 'docker-container') { + stateHelper.setContainerName(`${Buildx.containerNamePrefix}${firstNode.name}`); + await core.group(`BuildKit version`, async () => { + for (const node of builderInspect.nodes) { + const buildkitVersion = await toolkit.buildkit.getVersion(node); + core.info(`${node.name}: ${buildkitVersion}`); + } + }); } if (core.isDebug() || firstNode['buildkitd-flags']?.includes('--debug')) { stateHelper.setDebug('true'); } - } catch (error) { - core.setFailed(error.message); - } -} - -async function cleanup(): Promise { - if (stateHelper.IsDebug && stateHelper.containerName.length > 0) { - core.startGroup(`BuildKit container logs`); - await exec - .getExecOutput('docker', ['logs', `${stateHelper.containerName}`], { - ignoreReturnCode: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - core.warning(res.stderr.trim()); - } + }, + // post + async () => { + if (stateHelper.IsDebug && stateHelper.containerName.length > 0) { + await core.group(`BuildKit container logs`, async () => { + await exec + .getExecOutput('docker', ['logs', `${stateHelper.containerName}`], { + ignoreReturnCode: true + }) + .then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + core.warning(res.stderr.trim()); + } + }); }); - core.endGroup(); - } + } - if (stateHelper.builderName.length > 0) { - core.startGroup(`Removing builder`); - const rmCmd = buildx.getCommand(['rm', stateHelper.builderName], /true/i.test(stateHelper.standalone)); - await exec - .getExecOutput(rmCmd.commandLine, rmCmd.args, { - ignoreReturnCode: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - core.warning(res.stderr.trim()); - } + if (stateHelper.builderName.length > 0) { + await core.group(`Removing builder`, async () => { + const buildx = new Buildx({standalone: /true/i.test(stateHelper.standalone)}); + const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName]); + await exec + .getExecOutput(rmCmd.command, rmCmd.args, { + ignoreReturnCode: true + }) + .then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + core.warning(res.stderr.trim()); + } + }); }); - core.endGroup(); - } + } - if (stateHelper.credsDir.length > 0 && fs.existsSync(stateHelper.credsDir)) { - core.info(`Cleaning up credentials`); - fs.rmSync(stateHelper.credsDir, {recursive: true}); + if (stateHelper.certsDir.length > 0 && fs.existsSync(stateHelper.certsDir)) { + await core.group(`Cleaning up certificates`, async () => { + fs.rmSync(stateHelper.certsDir, {recursive: true}); + }); + } } -} - -if (!stateHelper.IsPost) { - run(); -} else { - cleanup(); -} +); diff --git a/src/nodes.ts b/src/nodes.ts deleted file mode 100644 index 60443e8..0000000 --- a/src/nodes.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as yaml from 'js-yaml'; - -export type Node = { - name?: string; - endpoint?: string; - 'driver-opts'?: Array; - 'buildkitd-flags'?: string; - platforms?: string; -}; - -export function Parse(data: string): Node[] { - return yaml.load(data) as Node[]; -} diff --git a/src/state-helper.ts b/src/state-helper.ts index f048bfa..b347be8 100644 --- a/src/state-helper.ts +++ b/src/state-helper.ts @@ -1,11 +1,10 @@ import * as core from '@actions/core'; -export const IsPost = !!process.env['STATE_isPost']; export const IsDebug = !!process.env['STATE_isDebug']; export const standalone = process.env['STATE_standalone'] || ''; export const builderName = process.env['STATE_builderName'] || ''; export const containerName = process.env['STATE_containerName'] || ''; -export const credsDir = process.env['STATE_credsDir'] || ''; +export const certsDir = process.env['STATE_certsDir'] || ''; export function setDebug(debug: string) { core.saveState('isDebug', debug); @@ -23,10 +22,6 @@ export function setContainerName(containerName: string) { core.saveState('containerName', containerName); } -export function setCredsDir(credsDir: string) { - core.saveState('credsDir', credsDir); -} - -if (!IsPost) { - core.saveState('isPost', 'true'); +export function setCertsDir(certsDir: string) { + core.saveState('certsDir', certsDir); } diff --git a/src/util.ts b/src/util.ts deleted file mode 100644 index 1336453..0000000 --- a/src/util.ts +++ /dev/null @@ -1,8 +0,0 @@ -export function isValidUrl(url: string): boolean { - try { - new URL(url); - } catch (e) { - return false; - } - return true; -} diff --git a/tsconfig.json b/tsconfig.json index 7339491..04c6783 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,19 +1,21 @@ { "compilerOptions": { + "esModuleInterop": true, "target": "es6", "module": "commonjs", + "strict": true, "newLine": "lf", "outDir": "./lib", "rootDir": "./src", - "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "strict": true, "noImplicitAny": false, + "resolveJsonModule": true, "useUnknownInCatchVariables": false, }, "exclude": [ + "./__tests__/**/*", + "./lib/**/*", "node_modules", - "**/*.test.ts", "jest.config.ts" ] } diff --git a/yarn.lock b/yarn.lock index a3b9b92..84f933e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -39,6 +39,11 @@ resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.1.1.tgz#4a157406309e212ab27ed3ae30e8c1d641686a66" integrity sha512-Qi4JoKXjmE0O67wAOH6y0n26QXhMKMFo7GD/4IXNVcrtLjUlGjGuVys6pQgwF3ArfGTQu0XpqaNr0YhED2RaRA== +"@actions/io@^1.1.2": + version "1.1.2" + resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.1.2.tgz#766ac09674a289ce0f1550ffe0a6eac9261a8ea9" + integrity sha512-d+RwPlMp+2qmBfeLYPLXuSRykDIFEwdTA0MMxzS9kh4kvP1ftrc/9fzy6pX6qAjthdXruHQ6/6kjT/DNo5ALuw== + "@actions/tool-cache@^2.0.1": version "2.0.1" resolved "https://registry.yarnpkg.com/@actions/tool-cache/-/tool-cache-2.0.1.tgz#8a649b9c07838d9d750c9864814e66a7660ab720" @@ -558,17 +563,18 @@ dependencies: "@cspotcode/source-map-consumer" "0.8.0" -"@docker/actions-toolkit@^0.1.0-beta.4": - version "0.1.0-beta.4" - resolved "https://registry.yarnpkg.com/@docker/actions-toolkit/-/actions-toolkit-0.1.0-beta.4.tgz#a0b62c299e4efed9baac4ead454b1e5eda06092a" - integrity sha512-pIKuGxKH+41+bjHctEDi15Ub2KxT6z2MFKCDA9KwUL0gCUjsUS/N2xIXGZUNKCfVCRTvWLQPttPxzoQxnlndog== +"@docker/actions-toolkit@^0.1.0-beta.15": + version "0.1.0-beta.15" + resolved "https://registry.yarnpkg.com/@docker/actions-toolkit/-/actions-toolkit-0.1.0-beta.15.tgz#46d9f4b1582f19ce3cb68cf272fbee335e693212" + integrity sha512-YdOHXz+r1fkoYYA5tR+Ji6jiqw5wU7gYsL8ISW+mQicm6f4Ckw4PNNADEZR+X8paEc+96Xl5Osr2tKmM3mOZOA== dependencies: "@actions/core" "^1.10.0" "@actions/exec" "^1.1.1" "@actions/github" "^5.1.1" "@actions/http-client" "^2.0.1" + "@actions/io" "^1.1.2" "@actions/tool-cache" "^2.0.1" - csv-parse "^5.3.4" + csv-parse "^5.3.5" jwt-decode "^3.1.2" semver "^7.3.8" tmp "^0.2.1" @@ -1041,21 +1047,11 @@ resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.4.tgz#5d9b63132df54d8909fce1c3f8ca260fdd693e17" integrity sha512-ReVR2rLTV1kvtlWFyuot+d1pkpG2Fw/XKE3PDAdj57rbM97ttSp9JZ2UsP+2EHTylra9cUf6JA7tGwW1INzUrA== -"@types/semver@^7.3.9": - version "7.3.9" - resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc" - integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ== - "@types/stack-utils@^2.0.0": version "2.0.0" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff" integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw== -"@types/tmp@^0.2.3": - version "0.2.3" - resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.2.3.tgz#908bfb113419fd6a42273674c00994d40902c165" - integrity sha512-dDZH/tXzwjutnuk4UacGgFRwV+JSLaXL1ikvidfJprkb7L9Nx1njcRHHmi3Dsvt7pgqqTEeucQuOrWHPFgzVHA== - "@types/yargs-parser@*": version "20.2.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9" @@ -1566,10 +1562,10 @@ cssstyle@^2.3.0: dependencies: cssom "~0.3.6" -csv-parse@^5.3.4: - version "5.3.4" - resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-5.3.4.tgz#f1f34457091dabd8b447528f741b7e0f080191d1" - integrity sha512-f2E4NzkIX4bVIx5Ff2gKT1BlVwyFQ+2iFy+QrqgUXaFLUo7vSzN6XQ8LV5V/T/p/9g7mJdtYHKLkwG5PiG82fg== +csv-parse@^5.3.5: + version "5.3.5" + resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-5.3.5.tgz#9924bbba9f7056122f06b7af18edc1a7f022ce99" + integrity sha512-8O5KTIRtwmtD3+EVfW6BCgbwZqJbhTYsQZry12F1TP5RUp0sD9tp1UnCWic3n0mLOhzeocYaCZNYxOGSg3dmmQ== data-urls@^2.0.0: version "2.0.0" @@ -1665,11 +1661,6 @@ domexception@^2.0.1: dependencies: webidl-conversions "^5.0.0" -dotenv@^16.0.0: - version "16.0.0" - resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.0.tgz#c619001253be89ebb638d027b609c75c26e47411" - integrity sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q== - electron-to-chromium@^1.3.723: version "1.3.755" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.755.tgz#4b6101f13de910cf3f0a1789ddc57328133b9332" @@ -3212,7 +3203,7 @@ saxes@^5.0.1: dependencies: xmlchars "^2.2.0" -semver@7.x, semver@^7.3.2, semver@^7.3.5, semver@^7.3.7: +semver@7.x, semver@^7.3.2, semver@^7.3.5: version "7.3.7" resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f" integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==