switch to actions-toolkit implementation

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-02-19 02:12:29 +01:00
parent 2dd22fa31c
commit 987520896f
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
28 changed files with 209 additions and 1271 deletions

View File

@ -2,7 +2,7 @@
"env": { "env": {
"node": true, "node": true,
"es2021": true, "es2021": true,
"jest/globals": true "jest": true
}, },
"extends": [ "extends": [
"eslint:recommended", "eslint:recommended",

View File

@ -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<string, string>, expectedFiles: Array<string>, expectedOpts: Array<string>) => {
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);
});
});
});

View File

@ -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);
}
});
});

View File

@ -1,19 +1,9 @@
import {beforeEach, describe, expect, it, jest, test} from '@jest/globals'; import {beforeEach, describe, expect, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as uuid from 'uuid'; 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 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.mock('uuid');
jest.spyOn(uuid, 'v4').mockReturnValue('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'); jest.spyOn(uuid, 'v4').mockReturnValue('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d');
@ -146,7 +136,7 @@ describe('getCreateArgs', () => {
setInput(name, value); setInput(name, value);
}); });
const inp = await context.getInputs(); 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); expect(res).toEqual(expected);
} }
); );
@ -192,86 +182,17 @@ describe('getAppendArgs', () => {
] ]
])( ])(
'[%d] given %p as inputs, returns %p', '[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, node: nodes.Node, expected: Array<string>) => { async (num: number, inputs: Map<string, string>, node: Node, expected: Array<string>) => {
inputs.forEach((value: string, name: string) => { inputs.forEach((value: string, name: string) => {
setInput(name, value); setInput(name, value);
}); });
const inp = await context.getInputs(); 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); 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 // See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string { function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;

View File

@ -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
});
});
});

View File

@ -1,3 +0,0 @@
debug = true
[registry."docker.io"]
mirrors = ["mirror.gcr.io"]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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');
});
});

View File

@ -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);
});
});

View File

@ -71,6 +71,7 @@ ENV RUNNER_TOOL_CACHE=/tmp/github_tool_cache
RUN --mount=type=bind,target=.,rw \ RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/src/node_modules \ --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=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 \ --mount=type=bind,from=buildx,source=/buildx,target=/usr/libexec/docker/cli-plugins/docker-buildx \
yarn run test --coverageDirectory=/tmp/coverage yarn run test --coverageDirectory=/tmp/coverage

View File

@ -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 = { module.exports = {
clearMocks: true, clearMocks: true,
moduleFileExtensions: ['js', 'ts'], moduleFileExtensions: ['js', 'ts'],
setupFiles: ['dotenv/config'],
testMatch: ['**/*.test.ts'], testMatch: ['**/*.test.ts'],
transform: { transform: {
'^.+\\.ts$': 'ts-jest' '^.+\\.ts$': 'ts-jest'
@ -9,5 +23,7 @@ module.exports = {
moduleNameMapper: { moduleNameMapper: {
'^csv-parse/sync': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs' '^csv-parse/sync': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs'
}, },
collectCoverageFrom: ['src/**/{!(main.ts),}.ts'],
coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__tests__/'],
verbose: true verbose: true
}; };

View File

@ -29,22 +29,15 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.10.0", "@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"@actions/tool-cache": "^2.0.1", "@docker/actions-toolkit": "^0.1.0-beta.15",
"@docker/actions-toolkit": "^0.1.0-beta.4",
"csv-parse": "^5.3.4",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"semver": "^7.3.7",
"tmp": "^0.2.1",
"uuid": "^9.0.0" "uuid": "^9.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.26", "@types/node": "^16.11.26",
"@types/semver": "^7.3.9",
"@types/tmp": "^0.2.3",
"@typescript-eslint/eslint-plugin": "^5.14.0", "@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0", "@typescript-eslint/parser": "^5.14.0",
"@vercel/ncc": "^0.33.3", "@vercel/ncc": "^0.33.3",
"dotenv": "^16.0.0",
"eslint": "^8.11.0", "eslint": "^8.11.0",
"eslint-config-prettier": "^8.5.0", "eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^26.1.1", "eslint-plugin-jest": "^26.1.1",

View File

@ -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<string> {
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<string> {
const driverOpts: Array<string> = [];
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;
}

View File

@ -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<string>;
status?: string;
'buildkitd-flags'?: string;
buildkit?: string;
platforms?: string;
};
export async function getConfigInline(s: string): Promise<string> {
return getConfig(s, false);
}
export async function getConfigFile(s: string): Promise<string> {
return getConfig(s, true);
}
export async function getConfig(s: string, file: boolean): Promise<string> {
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<boolean> {
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<string> {
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<Builder> {
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<Builder> {
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<string> = [];
// 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<string> {
// 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<string> {
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<string> {
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<string> {
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<string> {
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<string> {
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<string> {
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<string>, standalone?: boolean) {
return {
commandLine: standalone ? 'buildx' : 'docker',
args: standalone ? args : ['buildx', ...args]
};
}

View File

@ -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 * 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 * 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 builderNodeEnvPrefix = 'BUILDER_NODE';
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 interface Inputs { export interface Inputs {
version: string; version: string;
@ -43,9 +26,9 @@ export async function getInputs(): Promise<Inputs> {
version: core.getInput('version'), version: core.getInput('version'),
name: getBuilderName(core.getInput('driver') || 'docker-container'), name: getBuilderName(core.getInput('driver') || 'docker-container'),
driver: 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', 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'), install: core.getBooleanInput('install'),
use: core.getBooleanInput('use'), use: core.getBooleanInput('use'),
endpoint: core.getInput('endpoint'), endpoint: core.getInput('endpoint'),
@ -59,10 +42,10 @@ export function getBuilderName(driver: string): string {
return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`; return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
} }
export async function getCreateArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> { export async function getCreateArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
const args: Array<string> = ['create', '--name', inputs.name, '--driver', inputs.driver]; const args: Array<string> = ['create', '--name', inputs.name, '--driver', inputs.driver];
if (buildx.satisfies(buildxVersion, '>=0.3.0')) { if (await toolkit.buildx.versionSatisfies('>=0.3.0')) {
await asyncForEach(inputs.driverOpts, async driverOpt => { await Util.asyncForEach(inputs.driverOpts, async driverOpt => {
args.push('--driver-opt', driverOpt); args.push('--driver-opt', driverOpt);
}); });
if (inputs.driver != 'remote' && inputs.buildkitdFlags) { 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.driver != 'remote') {
if (inputs.config) { if (inputs.config) {
args.push('--config', await buildx.getConfigFile(inputs.config)); args.push('--config', toolkit.buildkit.config.resolveFromFile(inputs.config));
} else if (inputs.configInline) { } else if (inputs.configInline) {
args.push('--config', await buildx.getConfigInline(inputs.configInline)); args.push('--config', toolkit.buildkit.config.resolveFromString(inputs.configInline));
} }
} }
if (inputs.endpoint) { if (inputs.endpoint) {
@ -88,13 +71,13 @@ export async function getCreateArgs(inputs: Inputs, buildxVersion: string): Prom
return args; return args;
} }
export async function getAppendArgs(inputs: Inputs, node: nodes.Node, buildxVersion: string): Promise<Array<string>> { export async function getAppendArgs(inputs: Inputs, node: Node, toolkit: Toolkit): Promise<Array<string>> {
const args: Array<string> = ['create', '--name', inputs.name, '--append']; const args: Array<string> = ['create', '--name', inputs.name, '--append'];
if (node.name) { if (node.name) {
args.push('--node', node.name); args.push('--node', node.name);
} }
if (node['driver-opts'] && buildx.satisfies(buildxVersion, '>=0.3.0')) { if (node['driver-opts'] && (await toolkit.buildx.versionSatisfies('>=0.3.0'))) {
await asyncForEach(node['driver-opts'], async driverOpt => { await Util.asyncForEach(node['driver-opts'], async driverOpt => {
args.push('--driver-opt', driverOpt); args.push('--driver-opt', driverOpt);
}); });
if (inputs.driver != 'remote' && node['buildkitd-flags']) { if (inputs.driver != 'remote' && node['buildkitd-flags']) {
@ -110,47 +93,10 @@ export async function getAppendArgs(inputs: Inputs, node: nodes.Node, buildxVers
return args; return args;
} }
export async function getInspectArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> { export async function getInspectArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
const args: Array<string> = ['inspect', '--bootstrap']; const args: Array<string> = ['inspect', '--bootstrap'];
if (buildx.satisfies(buildxVersion, '>=0.4.0')) { if (await toolkit.buildx.versionSatisfies('>=0.4.0')) {
args.push('--builder', inputs.name); args.push('--builder', inputs.name);
} }
return args; return args;
} }
export async function getInputList(name: string, ignoreComma?: boolean, escapeQuotes?: boolean): Promise<string[]> {
const res: Array<string> = [];
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<string[]>) {
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);
}
};

View File

@ -1,19 +0,0 @@
import * as exec from '@actions/exec';
export async function isAvailable(): Promise<boolean> {
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;
});
}

View File

@ -1,19 +0,0 @@
import * as exec from '@actions/exec';
export async function getRemoteSha(repo: string, ref: string): Promise<string> {
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;
});
}

View File

@ -1,111 +1,124 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as os from 'os'; import * as yaml from 'js-yaml';
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 core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; 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<void> { import * as context from './context';
try { import * as stateHelper from './state-helper';
actionsToolkit.run(
// main
async () => {
const inputs: context.Inputs = await context.getInputs(); 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 toolkit.buildx.isStandalone();
const standalone = !(await docker.isAvailable());
stateHelper.setStandalone(standalone); stateHelper.setStandalone(standalone);
core.startGroup(`Docker info`); await core.group(`Docker info`, async () => {
if (standalone) { try {
core.info(`Docker info skipped in standalone mode`); await Docker.printVersion();
} else { await Docker.printInfo();
await exec.exec('docker', ['version'], { } catch (e) {
failOnStdErr: false core.info(e.message);
});
await exec.exec('docker', ['info'], {
failOnStdErr: false
});
} }
core.endGroup(); });
if (util.isValidUrl(inputs.version)) { let toolPath;
if (Util.isValidUrl(inputs.version)) {
if (standalone) { if (standalone) {
throw new Error(`Cannot build from source without the Docker CLI`); throw new Error(`Cannot build from source without the Docker CLI`);
} }
core.startGroup(`Build and install buildx`); await core.group(`Build buildx from source`, async () => {
await buildx.build(inputs.version, dockerConfigHome, standalone); toolPath = await toolkit.buildxInstall.build(inputs.version);
core.endGroup(); });
} else if (!(await buildx.isAvailable(standalone)) || inputs.version) { } else if (!(await toolkit.buildx.isAvailable()) || inputs.version) {
core.startGroup(`Download and install buildx`); await core.group(`Download buildx from GitHub Releases`, async () => {
await buildx.install(inputs.version || 'latest', standalone ? context.tmpDir() : dockerConfigHome, standalone); toolPath = await toolkit.buildxInstall.download(inputs.version || 'latest');
core.endGroup(); });
}
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 () => { await core.group(`Buildx version`, async () => {
const versionCmd = buildx.getCommand(['version'], standalone); await toolkit.buildx.printVersion();
await exec.exec(versionCmd.commandLine, versionCmd.args, {
failOnStdErr: false
});
}); });
core.setOutput('name', inputs.name); core.setOutput('name', inputs.name);
stateHelper.setBuilderName(inputs.name); stateHelper.setBuilderName(inputs.name);
const credsdir = path.join(dockerConfigHome, 'buildx', 'creds', inputs.name); fs.mkdirSync(Buildx.certsDir, {recursive: true});
fs.mkdirSync(credsdir, {recursive: true}); stateHelper.setCertsDir(Buildx.certsDir);
stateHelper.setCredsDir(credsdir);
if (inputs.driver !== 'docker') { if (inputs.driver !== 'docker') {
core.startGroup(`Creating a new builder instance`); await core.group(`Creating a new builder instance`, async () => {
const authOpts = auth.setCredentials(credsdir, 0, inputs.driver, inputs.endpoint); const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, inputs.endpoint, {
if (authOpts.length > 0) { cacert: process.env[`${context.builderNodeEnvPrefix}_0_AUTH_TLS_CACERT`],
inputs.driverOpts = [...inputs.driverOpts, ...authOpts]; 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 = buildx.getCommand(await context.getCreateArgs(inputs, buildxVersion), standalone); const createCmd = await toolkit.buildx.getCommand(await context.getCreateArgs(inputs, toolkit));
await exec.exec(createCmd.commandLine, createCmd.args); await exec.exec(createCmd.command, createCmd.args);
core.endGroup(); });
} }
if (inputs.append) { if (inputs.append) {
core.startGroup(`Appending node(s) to builder`); await core.group(`Appending node(s) to builder`, async () => {
let nodeIndex = 1; let nodeIndex = 1;
for (const node of nodes.Parse(inputs.append)) { const nodes = yaml.load(inputs.append) as Node[];
const authOpts = auth.setCredentials(credsdir, nodeIndex, inputs.driver, node.endpoint || ''); for (const node of nodes) {
if (authOpts.length > 0) { const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, `${node.endpoint}`, {
node['driver-opts'] = [...(node['driver-opts'] || []), ...authOpts]; 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 = buildx.getCommand(await context.getAppendArgs(inputs, node, buildxVersion), standalone); const appendCmd = await toolkit.buildx.getCommand(await context.getAppendArgs(inputs, node, toolkit));
await exec.exec(appendCmd.commandLine, appendCmd.args); await exec.exec(appendCmd.command, appendCmd.args);
nodeIndex++; nodeIndex++;
} }
core.endGroup(); });
} }
core.startGroup(`Booting builder`); await core.group(`Booting builder`, async () => {
const inspectCmd = buildx.getCommand(await context.getInspectArgs(inputs, buildxVersion), standalone); const inspectCmd = await toolkit.buildx.getCommand(await context.getInspectArgs(inputs, toolkit));
await exec.exec(inspectCmd.commandLine, inspectCmd.args); await exec.exec(inspectCmd.command, inspectCmd.args);
core.endGroup(); });
if (inputs.install) { if (inputs.install) {
if (standalone) { if (standalone) {
throw new Error(`Cannot set buildx as default builder without the Docker CLI`); throw new Error(`Cannot set buildx as default builder without the Docker CLI`);
} }
core.startGroup(`Setting buildx as default builder`); await core.group(`Setting buildx as default builder`, async () => {
await exec.exec('docker', ['buildx', 'install']); const installCmd = await toolkit.buildx.getCommand(['install']);
core.endGroup(); await exec.exec(installCmd.command, installCmd.args);
});
} }
core.startGroup(`Inspect builder`); const builderInspect = await toolkit.builder.inspect(inputs.name);
const builder = await buildx.inspect(inputs.name, standalone); const firstNode = builderInspect.nodes[0];
const firstNode = builder.nodes[0];
await core.group(`Inspect builder`, async () => {
const reducedPlatforms: Array<string> = []; const reducedPlatforms: Array<string> = [];
for (const node of builder.nodes) { for (const node of builderInspect.nodes) {
for (const platform of node.platforms?.split(',') || []) { for (const platform of node.platforms?.split(',') || []) {
if (reducedPlatforms.indexOf(platform) > -1) { if (reducedPlatforms.indexOf(platform) > -1) {
continue; continue;
@ -113,35 +126,32 @@ async function run(): Promise<void> {
reducedPlatforms.push(platform); reducedPlatforms.push(platform);
} }
} }
core.info(JSON.stringify(builder, undefined, 2)); core.info(JSON.stringify(builderInspect, undefined, 2));
core.setOutput('driver', builder.driver); core.setOutput('driver', builderInspect.driver);
core.setOutput('platforms', reducedPlatforms.join(',')); core.setOutput('platforms', reducedPlatforms.join(','));
core.setOutput('nodes', JSON.stringify(builder.nodes, undefined, 2)); 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('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('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.setOutput('flags', firstNode['buildkitd-flags']); // TODO: deprecated, to be removed in a later version
core.endGroup(); });
if (!standalone && builder.driver == 'docker-container') { if (!standalone && builderInspect.driver == 'docker-container') {
stateHelper.setContainerName(`buildx_buildkit_${firstNode.name}`); stateHelper.setContainerName(`${Buildx.containerNamePrefix}${firstNode.name}`);
core.startGroup(`BuildKit version`); await core.group(`BuildKit version`, async () => {
for (const node of builder.nodes) { for (const node of builderInspect.nodes) {
const bkvers = await buildx.getBuildKitVersion(`buildx_buildkit_${node.name}`); const buildkitVersion = await toolkit.buildkit.getVersion(node);
core.info(`${node.name}: ${bkvers}`); core.info(`${node.name}: ${buildkitVersion}`);
} }
core.endGroup(); });
} }
if (core.isDebug() || firstNode['buildkitd-flags']?.includes('--debug')) { if (core.isDebug() || firstNode['buildkitd-flags']?.includes('--debug')) {
stateHelper.setDebug('true'); stateHelper.setDebug('true');
} }
} catch (error) { },
core.setFailed(error.message); // post
} async () => {
}
async function cleanup(): Promise<void> {
if (stateHelper.IsDebug && stateHelper.containerName.length > 0) { if (stateHelper.IsDebug && stateHelper.containerName.length > 0) {
core.startGroup(`BuildKit container logs`); await core.group(`BuildKit container logs`, async () => {
await exec await exec
.getExecOutput('docker', ['logs', `${stateHelper.containerName}`], { .getExecOutput('docker', ['logs', `${stateHelper.containerName}`], {
ignoreReturnCode: true ignoreReturnCode: true
@ -151,14 +161,15 @@ async function cleanup(): Promise<void> {
core.warning(res.stderr.trim()); core.warning(res.stderr.trim());
} }
}); });
core.endGroup(); });
} }
if (stateHelper.builderName.length > 0) { if (stateHelper.builderName.length > 0) {
core.startGroup(`Removing builder`); await core.group(`Removing builder`, async () => {
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName], /true/i.test(stateHelper.standalone)); const buildx = new Buildx({standalone: /true/i.test(stateHelper.standalone)});
const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName]);
await exec await exec
.getExecOutput(rmCmd.commandLine, rmCmd.args, { .getExecOutput(rmCmd.command, rmCmd.args, {
ignoreReturnCode: true ignoreReturnCode: true
}) })
.then(res => { .then(res => {
@ -166,17 +177,13 @@ async function cleanup(): Promise<void> {
core.warning(res.stderr.trim()); core.warning(res.stderr.trim());
} }
}); });
core.endGroup(); });
} }
if (stateHelper.credsDir.length > 0 && fs.existsSync(stateHelper.credsDir)) { if (stateHelper.certsDir.length > 0 && fs.existsSync(stateHelper.certsDir)) {
core.info(`Cleaning up credentials`); await core.group(`Cleaning up certificates`, async () => {
fs.rmSync(stateHelper.credsDir, {recursive: true}); fs.rmSync(stateHelper.certsDir, {recursive: true});
});
} }
} }
);
if (!stateHelper.IsPost) {
run();
} else {
cleanup();
}

View File

@ -1,13 +0,0 @@
import * as yaml from 'js-yaml';
export type Node = {
name?: string;
endpoint?: string;
'driver-opts'?: Array<string>;
'buildkitd-flags'?: string;
platforms?: string;
};
export function Parse(data: string): Node[] {
return yaml.load(data) as Node[];
}

View File

@ -1,11 +1,10 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
export const IsPost = !!process.env['STATE_isPost'];
export const IsDebug = !!process.env['STATE_isDebug']; export const IsDebug = !!process.env['STATE_isDebug'];
export const standalone = process.env['STATE_standalone'] || ''; export const standalone = process.env['STATE_standalone'] || '';
export const builderName = process.env['STATE_builderName'] || ''; export const builderName = process.env['STATE_builderName'] || '';
export const containerName = process.env['STATE_containerName'] || ''; 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) { export function setDebug(debug: string) {
core.saveState('isDebug', debug); core.saveState('isDebug', debug);
@ -23,10 +22,6 @@ export function setContainerName(containerName: string) {
core.saveState('containerName', containerName); core.saveState('containerName', containerName);
} }
export function setCredsDir(credsDir: string) { export function setCertsDir(certsDir: string) {
core.saveState('credsDir', credsDir); core.saveState('certsDir', certsDir);
}
if (!IsPost) {
core.saveState('isPost', 'true');
} }

View File

@ -1,8 +0,0 @@
export function isValidUrl(url: string): boolean {
try {
new URL(url);
} catch (e) {
return false;
}
return true;
}

View File

@ -1,19 +1,21 @@
{ {
"compilerOptions": { "compilerOptions": {
"esModuleInterop": true,
"target": "es6", "target": "es6",
"module": "commonjs", "module": "commonjs",
"strict": true,
"newLine": "lf", "newLine": "lf",
"outDir": "./lib", "outDir": "./lib",
"rootDir": "./src", "rootDir": "./src",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": false, "noImplicitAny": false,
"resolveJsonModule": true,
"useUnknownInCatchVariables": false, "useUnknownInCatchVariables": false,
}, },
"exclude": [ "exclude": [
"./__tests__/**/*",
"./lib/**/*",
"node_modules", "node_modules",
"**/*.test.ts",
"jest.config.ts" "jest.config.ts"
] ]
} }

View File

@ -39,6 +39,11 @@
resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.1.1.tgz#4a157406309e212ab27ed3ae30e8c1d641686a66" resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.1.1.tgz#4a157406309e212ab27ed3ae30e8c1d641686a66"
integrity sha512-Qi4JoKXjmE0O67wAOH6y0n26QXhMKMFo7GD/4IXNVcrtLjUlGjGuVys6pQgwF3ArfGTQu0XpqaNr0YhED2RaRA== 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": "@actions/tool-cache@^2.0.1":
version "2.0.1" version "2.0.1"
resolved "https://registry.yarnpkg.com/@actions/tool-cache/-/tool-cache-2.0.1.tgz#8a649b9c07838d9d750c9864814e66a7660ab720" resolved "https://registry.yarnpkg.com/@actions/tool-cache/-/tool-cache-2.0.1.tgz#8a649b9c07838d9d750c9864814e66a7660ab720"
@ -558,17 +563,18 @@
dependencies: dependencies:
"@cspotcode/source-map-consumer" "0.8.0" "@cspotcode/source-map-consumer" "0.8.0"
"@docker/actions-toolkit@^0.1.0-beta.4": "@docker/actions-toolkit@^0.1.0-beta.15":
version "0.1.0-beta.4" version "0.1.0-beta.15"
resolved "https://registry.yarnpkg.com/@docker/actions-toolkit/-/actions-toolkit-0.1.0-beta.4.tgz#a0b62c299e4efed9baac4ead454b1e5eda06092a" resolved "https://registry.yarnpkg.com/@docker/actions-toolkit/-/actions-toolkit-0.1.0-beta.15.tgz#46d9f4b1582f19ce3cb68cf272fbee335e693212"
integrity sha512-pIKuGxKH+41+bjHctEDi15Ub2KxT6z2MFKCDA9KwUL0gCUjsUS/N2xIXGZUNKCfVCRTvWLQPttPxzoQxnlndog== integrity sha512-YdOHXz+r1fkoYYA5tR+Ji6jiqw5wU7gYsL8ISW+mQicm6f4Ckw4PNNADEZR+X8paEc+96Xl5Osr2tKmM3mOZOA==
dependencies: dependencies:
"@actions/core" "^1.10.0" "@actions/core" "^1.10.0"
"@actions/exec" "^1.1.1" "@actions/exec" "^1.1.1"
"@actions/github" "^5.1.1" "@actions/github" "^5.1.1"
"@actions/http-client" "^2.0.1" "@actions/http-client" "^2.0.1"
"@actions/io" "^1.1.2"
"@actions/tool-cache" "^2.0.1" "@actions/tool-cache" "^2.0.1"
csv-parse "^5.3.4" csv-parse "^5.3.5"
jwt-decode "^3.1.2" jwt-decode "^3.1.2"
semver "^7.3.8" semver "^7.3.8"
tmp "^0.2.1" tmp "^0.2.1"
@ -1041,21 +1047,11 @@
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.4.tgz#5d9b63132df54d8909fce1c3f8ca260fdd693e17" resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.4.tgz#5d9b63132df54d8909fce1c3f8ca260fdd693e17"
integrity sha512-ReVR2rLTV1kvtlWFyuot+d1pkpG2Fw/XKE3PDAdj57rbM97ttSp9JZ2UsP+2EHTylra9cUf6JA7tGwW1INzUrA== 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": "@types/stack-utils@^2.0.0":
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw== 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@*": "@types/yargs-parser@*":
version "20.2.0" version "20.2.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9"
@ -1566,10 +1562,10 @@ cssstyle@^2.3.0:
dependencies: dependencies:
cssom "~0.3.6" cssom "~0.3.6"
csv-parse@^5.3.4: csv-parse@^5.3.5:
version "5.3.4" version "5.3.5"
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-5.3.4.tgz#f1f34457091dabd8b447528f741b7e0f080191d1" resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-5.3.5.tgz#9924bbba9f7056122f06b7af18edc1a7f022ce99"
integrity sha512-f2E4NzkIX4bVIx5Ff2gKT1BlVwyFQ+2iFy+QrqgUXaFLUo7vSzN6XQ8LV5V/T/p/9g7mJdtYHKLkwG5PiG82fg== integrity sha512-8O5KTIRtwmtD3+EVfW6BCgbwZqJbhTYsQZry12F1TP5RUp0sD9tp1UnCWic3n0mLOhzeocYaCZNYxOGSg3dmmQ==
data-urls@^2.0.0: data-urls@^2.0.0:
version "2.0.0" version "2.0.0"
@ -1665,11 +1661,6 @@ domexception@^2.0.1:
dependencies: dependencies:
webidl-conversions "^5.0.0" 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: electron-to-chromium@^1.3.723:
version "1.3.755" version "1.3.755"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.755.tgz#4b6101f13de910cf3f0a1789ddc57328133b9332" 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: dependencies:
xmlchars "^2.2.0" 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" version "7.3.7"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f" resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g== integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==