mirror of
https://github.com/docker/setup-buildx-action.git
synced 2025-06-27 21:21:08 +02:00
Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
e673438944 | |||
f40e8894f1 | |||
a1c666d855 | |||
d5b70f51d8 | |||
2e941f2def | |||
eef74457f7 | |||
e168301d39 | |||
29f1eeb9e5 | |||
faca7837b0 | |||
dffa64995b | |||
c0e291b502 | |||
2323559062 | |||
e80b8cc6d8 | |||
31e7cc5f84 | |||
9db0a23fb3 | |||
90e26af07a | |||
01ed3f7910 | |||
11ae4c31f6 | |||
983bf3e000 | |||
9a462131b5 | |||
1806a02fac | |||
c1f17c078a | |||
76bfd425d8 | |||
5a93241d03 | |||
2f13c4010e | |||
1dd5af0c3a | |||
55dd79473c | |||
b1f1f719c7 | |||
68810d1ede | |||
cd8b844a0a | |||
894f000c27 | |||
f080c7125b | |||
49b8353604 | |||
0d135e0c2f | |||
36d8e005ca |
BIN
.github/setup-buildx-action.png
vendored
BIN
.github/setup-buildx-action.png
vendored
Binary file not shown.
Before Width: | Height: | Size: 5.1 KiB After Width: | Height: | Size: 11 KiB |
36
.github/workflows/ci.yml
vendored
36
.github/workflows/ci.yml
vendored
@ -243,6 +243,8 @@ jobs:
|
|||||||
uses: ./
|
uses: ./
|
||||||
with:
|
with:
|
||||||
endpoint: mycontext
|
endpoint: mycontext
|
||||||
|
env:
|
||||||
|
DOCKER_CONTEXT: mycontext
|
||||||
|
|
||||||
config:
|
config:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@ -311,3 +313,37 @@ jobs:
|
|||||||
echo "Status: ${{ steps.buildx.outputs.status }}"
|
echo "Status: ${{ steps.buildx.outputs.status }}"
|
||||||
echo "Flags: ${{ steps.buildx.outputs.flags }}"
|
echo "Flags: ${{ steps.buildx.outputs.flags }}"
|
||||||
echo "Platforms: ${{ steps.buildx.outputs.platforms }}"
|
echo "Platforms: ${{ steps.buildx.outputs.platforms }}"
|
||||||
|
|
||||||
|
build-ref:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
ref:
|
||||||
|
- master
|
||||||
|
- refs/tags/v0.5.1
|
||||||
|
- refs/pull/648/head
|
||||||
|
steps:
|
||||||
|
-
|
||||||
|
name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
-
|
||||||
|
name: Set up Docker Buildx
|
||||||
|
uses: ./
|
||||||
|
with:
|
||||||
|
version: https://github.com/docker/buildx.git#${{ matrix.ref }}
|
||||||
|
-
|
||||||
|
name: Check version
|
||||||
|
run: |
|
||||||
|
docker buildx version
|
||||||
|
-
|
||||||
|
name: Create Dockerfile
|
||||||
|
run: |
|
||||||
|
cat > ./Dockerfile <<EOL
|
||||||
|
FROM alpine
|
||||||
|
EOL
|
||||||
|
-
|
||||||
|
name: Build
|
||||||
|
uses: docker/build-push-action@master
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"printWidth": 120,
|
"printWidth": 240,
|
||||||
"tabWidth": 2,
|
"tabWidth": 2,
|
||||||
"useTabs": false,
|
"useTabs": false,
|
||||||
"semi": true,
|
"semi": true,
|
||||||
|
@ -125,7 +125,7 @@ Following inputs can be used as `step.with` keys
|
|||||||
|
|
||||||
| Name | Type | Description |
|
| Name | Type | Description |
|
||||||
|--------------------|---------|-----------------------------------|
|
|--------------------|---------|-----------------------------------|
|
||||||
| `version` | String | [Buildx](https://github.com/docker/buildx) version. (eg. `v0.3.0`, `latest`) |
|
| `version` | String | [buildx](https://github.com/docker/buildx) version. (eg. `v0.3.0`, `latest`, `https://github.com/docker/buildx.git#master`) |
|
||||||
| `driver` | String | Sets the [builder driver](https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#driver) to be used (default `docker-container`) |
|
| `driver` | String | Sets the [builder driver](https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#driver) to be used (default `docker-container`) |
|
||||||
| `driver-opts` | CSV | List of additional [driver-specific options](https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#driver-opt) (eg. `image=moby/buildkit:master`) |
|
| `driver-opts` | CSV | List of additional [driver-specific options](https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#driver-opt) (eg. `image=moby/buildkit:master`) |
|
||||||
| `buildkitd-flags` | String | [Flags for buildkitd](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md) daemon (since [buildx v0.3.0](https://github.com/docker/buildx/releases/tag/v0.3.0)) |
|
| `buildkitd-flags` | String | [Flags for buildkitd](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md) daemon (since [buildx v0.3.0](https://github.com/docker/buildx/releases/tag/v0.3.0)) |
|
||||||
|
@ -1,33 +1,82 @@
|
|||||||
import fs = require('fs');
|
import * as fs from 'fs';
|
||||||
import * as docker from '../src/docker';
|
|
||||||
import * as buildx from '../src/buildx';
|
|
||||||
import * as path from 'path';
|
|
||||||
import * as os from 'os';
|
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 semver from 'semver';
|
||||||
import * as exec from '@actions/exec';
|
import * as exec from '@actions/exec';
|
||||||
|
|
||||||
|
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
|
||||||
|
const tmpDir = path.join('/tmp/.docker-setup-buildx-jest').split(path.sep).join(path.posix.sep);
|
||||||
|
if (!fs.existsSync(tmpDir)) {
|
||||||
|
fs.mkdirSync(tmpDir, {recursive: true});
|
||||||
|
}
|
||||||
|
return tmpDir;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('isAvailable', () => {
|
||||||
|
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
|
||||||
|
buildx.isAvailable();
|
||||||
|
|
||||||
|
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
|
||||||
|
silent: true,
|
||||||
|
ignoreReturnCode: true
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('getVersion', () => {
|
describe('getVersion', () => {
|
||||||
it('valid', async () => {
|
async function isDaemonRunning() {
|
||||||
await exec.exec('docker', ['buildx', 'version']);
|
return await exec
|
||||||
const version = await buildx.getVersion();
|
.getExecOutput(`docker`, ['version', '--format', '{{.Server.Os}}'], {
|
||||||
console.log(`version: ${version}`);
|
ignoreReturnCode: true,
|
||||||
expect(semver.valid(version)).not.toBeNull();
|
silent: true
|
||||||
}, 100000);
|
})
|
||||||
|
.then(res => {
|
||||||
|
return !res.stdout.trim().includes(' ') && res.exitCode == 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
(isDaemonRunning() ? it : it.skip)(
|
||||||
|
'valid',
|
||||||
|
async () => {
|
||||||
|
const version = await buildx.getVersion();
|
||||||
|
console.log(`version: ${version}`);
|
||||||
|
expect(semver.valid(version)).not.toBeNull();
|
||||||
|
},
|
||||||
|
100000
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('parseVersion', () => {
|
describe('parseVersion', () => {
|
||||||
test.each([
|
test.each([
|
||||||
['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
|
['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.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
|
||||||
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2']
|
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'],
|
||||||
|
['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971']
|
||||||
])('given %p', async (stdout, expected) => {
|
])('given %p', async (stdout, expected) => {
|
||||||
expect(await buildx.parseVersion(stdout)).toEqual(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', () => {
|
describe('inspect', () => {
|
||||||
async function isDaemonRunning() {
|
async function isDaemonRunning() {
|
||||||
return await docker.isDaemonRunning();
|
return await exec
|
||||||
|
.getExecOutput(`docker`, ['version', '--format', '{{.Server.Os}}'], {
|
||||||
|
ignoreReturnCode: true,
|
||||||
|
silent: true
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
return !res.stdout.trim().includes(' ') && res.exitCode == 0;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
(isDaemonRunning() ? it : it.skip)(
|
(isDaemonRunning() ? it : it.skip)(
|
||||||
'valid',
|
'valid',
|
||||||
@ -43,6 +92,15 @@ describe('inspect', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('build', () => {
|
||||||
|
it.skip('valid', async () => {
|
||||||
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
|
||||||
|
const buildxBin = await buildx.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir);
|
||||||
|
console.log(buildxBin);
|
||||||
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
||||||
|
}, 100000);
|
||||||
|
});
|
||||||
|
|
||||||
describe('install', () => {
|
describe('install', () => {
|
||||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
|
||||||
it('acquires v0.4.1 version of buildx', async () => {
|
it('acquires v0.4.1 version of buildx', async () => {
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
|
import * as path from 'path';
|
||||||
import * as context from '../src/context';
|
import * as context from '../src/context';
|
||||||
|
|
||||||
|
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
|
||||||
|
const tmpDir = path.join('/tmp/.docker-setup-buildx-jest').split(path.sep).join(path.posix.sep);
|
||||||
|
if (!fs.existsSync(tmpDir)) {
|
||||||
|
fs.mkdirSync(tmpDir, {recursive: true});
|
||||||
|
}
|
||||||
|
return tmpDir;
|
||||||
|
});
|
||||||
|
|
||||||
describe('getInputList', () => {
|
describe('getInputList', () => {
|
||||||
it('handles single line correctly', async () => {
|
it('handles single line correctly', async () => {
|
||||||
await setInput('foo', 'bar');
|
await setInput('foo', 'bar');
|
||||||
|
9
__tests__/git.test.ts
Normal file
9
__tests__/git.test.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
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');
|
||||||
|
console.log(`ref: ${ref}`);
|
||||||
|
expect(ref).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
|
||||||
|
});
|
||||||
|
});
|
11
__tests__/util.test.ts
Normal file
11
__tests__/util.test.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
3
codecov.yml
Normal file
3
codecov.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
comment: false
|
||||||
|
github_checks:
|
||||||
|
annotations: false
|
12461
dist/index.js
generated
vendored
12461
dist/index.js
generated
vendored
File diff suppressed because it is too large
Load Diff
26
package.json
26
package.json
@ -27,24 +27,24 @@
|
|||||||
],
|
],
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.7",
|
"@actions/core": "^1.4.0",
|
||||||
"@actions/exec": "^1.0.4",
|
"@actions/exec": "^1.1.0",
|
||||||
"@actions/http-client": "^1.0.11",
|
"@actions/http-client": "^1.0.11",
|
||||||
"@actions/tool-cache": "^1.6.1",
|
"@actions/tool-cache": "^1.7.1",
|
||||||
"semver": "^7.3.5",
|
"semver": "^7.3.5",
|
||||||
"uuid": "^8.3.2"
|
"uuid": "^8.3.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^26.0.3",
|
"@types/jest": "^26.0.23",
|
||||||
"@types/node": "^14.0.14",
|
"@types/node": "^14.17.4",
|
||||||
"@vercel/ncc": "^0.23.0",
|
"@vercel/ncc": "^0.28.6",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.6.0",
|
||||||
"jest": "^26.1.0",
|
"jest": "^26.6.3",
|
||||||
"jest-circus": "^26.1.0",
|
"jest-circus": "^26.6.3",
|
||||||
"jest-runtime": "^26.1.0",
|
"jest-runtime": "^26.6.3",
|
||||||
"prettier": "^2.0.5",
|
"prettier": "^2.3.1",
|
||||||
"ts-jest": "^26.1.1",
|
"ts-jest": "^26.5.6",
|
||||||
"typescript": "^3.9.5",
|
"typescript": "^4.3.4",
|
||||||
"typescript-formatter": "^7.2.2"
|
"typescript-formatter": "^7.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
214
src/buildx.ts
214
src/buildx.ts
@ -3,9 +3,10 @@ import * as path from 'path';
|
|||||||
import * as semver from 'semver';
|
import * as semver from 'semver';
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
import * as context from './context';
|
import * as context from './context';
|
||||||
import * as exec from './exec';
|
import * as git from './git';
|
||||||
import * as github from './github';
|
import * as github from './github';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
|
import * as exec from '@actions/exec';
|
||||||
import * as tc from '@actions/tool-cache';
|
import * as tc from '@actions/tool-cache';
|
||||||
|
|
||||||
export type Builder = {
|
export type Builder = {
|
||||||
@ -18,77 +19,124 @@ export type Builder = {
|
|||||||
node_platforms?: string;
|
node_platforms?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getVersion(): Promise<string> {
|
|
||||||
return await exec.exec(`docker`, ['buildx', 'version'], true).then(res => {
|
|
||||||
if (res.stderr != '' && !res.success) {
|
|
||||||
throw new Error(res.stderr);
|
|
||||||
}
|
|
||||||
return parseVersion(res.stdout);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function parseVersion(stdout: string): Promise<string> {
|
|
||||||
const matches = /\sv?([0-9.]+)/.exec(stdout);
|
|
||||||
if (!matches) {
|
|
||||||
throw new Error(`Cannot parse Buildx version`);
|
|
||||||
}
|
|
||||||
return semver.clean(matches[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function isAvailable(): Promise<Boolean> {
|
export async function isAvailable(): Promise<Boolean> {
|
||||||
return await exec.exec(`docker`, ['buildx'], true).then(res => {
|
return await exec
|
||||||
if (res.stderr != '' && !res.success) {
|
.getExecOutput('docker', ['buildx'], {
|
||||||
return false;
|
ignoreReturnCode: true,
|
||||||
}
|
silent: true
|
||||||
return res.success;
|
})
|
||||||
});
|
.then(res => {
|
||||||
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return res.exitCode == 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getVersion(): Promise<string> {
|
||||||
|
return await exec
|
||||||
|
.getExecOutput('docker', ['buildx', 'version'], {
|
||||||
|
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): Promise<Builder> {
|
export async function inspect(name: string): Promise<Builder> {
|
||||||
return await exec.exec(`docker`, ['buildx', 'inspect', name], true).then(res => {
|
return await exec
|
||||||
if (res.stderr != '' && !res.success) {
|
.getExecOutput(`docker`, ['buildx', 'inspect', name], {
|
||||||
throw new Error(res.stderr);
|
ignoreReturnCode: true,
|
||||||
}
|
silent: true
|
||||||
const builder: Builder = {};
|
})
|
||||||
itlines: for (const line of res.stdout.trim().split(`\n`)) {
|
.then(res => {
|
||||||
const [key, ...rest] = line.split(':');
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||||
const value = rest.map(v => v.trim()).join(':');
|
throw new Error(res.stderr.trim());
|
||||||
if (key.length == 0 || value.length == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
switch (key) {
|
const builder: Builder = {};
|
||||||
case 'Name': {
|
itlines: for (const line of res.stdout.trim().split(`\n`)) {
|
||||||
if (builder.name == undefined) {
|
const [key, ...rest] = line.split(':');
|
||||||
builder.name = value;
|
const value = rest.map(v => v.trim()).join(':');
|
||||||
} else {
|
if (key.length == 0 || value.length == 0) {
|
||||||
builder.node_name = value;
|
continue;
|
||||||
|
}
|
||||||
|
switch (key) {
|
||||||
|
case 'Name': {
|
||||||
|
if (builder.name == undefined) {
|
||||||
|
builder.name = value;
|
||||||
|
} else {
|
||||||
|
builder.node_name = value;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Driver': {
|
||||||
|
builder.driver = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Endpoint': {
|
||||||
|
builder.node_endpoint = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Status': {
|
||||||
|
builder.node_status = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Flags': {
|
||||||
|
builder.node_flags = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Platforms': {
|
||||||
|
builder.node_platforms = value.replace(/\s/g, '');
|
||||||
|
break itlines;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'Driver': {
|
|
||||||
builder.driver = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'Endpoint': {
|
|
||||||
builder.node_endpoint = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'Status': {
|
|
||||||
builder.node_status = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'Flags': {
|
|
||||||
builder.node_flags = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'Platforms': {
|
|
||||||
builder.node_platforms = value.replace(/\s/g, '');
|
|
||||||
break itlines;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return builder;
|
||||||
return builder;
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
export async function build(inputBuildRef: string, dockerConfigHome: string): Promise<string> {
|
||||||
|
let [repo, ref] = inputBuildRef.split('#');
|
||||||
|
if (ref.length == 0) {
|
||||||
|
ref = 'master';
|
||||||
|
}
|
||||||
|
|
||||||
|
const sha = await git.getRemoteSha(repo, ref);
|
||||||
|
core.debug(`Remote ref ${sha} found`);
|
||||||
|
|
||||||
|
let toolPath: string;
|
||||||
|
toolPath = tc.find('buildx', sha);
|
||||||
|
if (!toolPath) {
|
||||||
|
const outFolder = path.join(context.tmpDir(), 'out').split(path.sep).join(path.posix.sep);
|
||||||
|
toolPath = await exec
|
||||||
|
.getExecOutput('docker', ['buildx', 'build', '--target', 'binaries', '--build-arg', 'BUILDKIT_CONTEXT_KEEP_GIT_DIR=1', '--output', `type=local,dest=${outFolder}`, inputBuildRef], {
|
||||||
|
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', sha);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return setPlugin(toolPath, dockerConfigHome);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function install(inputVersion: string, dockerConfigHome: string): Promise<string> {
|
export async function install(inputVersion: string, dockerConfigHome: string): Promise<string> {
|
||||||
@ -109,6 +157,10 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
|
|||||||
toolPath = await download(version);
|
toolPath = await download(version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return setPlugin(toolPath, dockerConfigHome);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setPlugin(toolPath: string, dockerConfigHome: string): Promise<string> {
|
||||||
const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins');
|
const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins');
|
||||||
core.debug(`Plugins dir is ${pluginsDir}`);
|
core.debug(`Plugins dir is ${pluginsDir}`);
|
||||||
if (!fs.existsSync(pluginsDir)) {
|
if (!fs.existsSync(pluginsDir)) {
|
||||||
@ -128,11 +180,7 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
|
|||||||
|
|
||||||
async function download(version: string): Promise<string> {
|
async function download(version: string): Promise<string> {
|
||||||
const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
|
const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
|
||||||
const downloadUrl = util.format(
|
const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, await filename(version));
|
||||||
'https://github.com/docker/buildx/releases/download/v%s/%s',
|
|
||||||
version,
|
|
||||||
await filename(version)
|
|
||||||
);
|
|
||||||
let downloadPath: string;
|
let downloadPath: string;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -171,3 +219,31 @@ async function filename(version: string): Promise<string> {
|
|||||||
const ext: string = context.osPlat == 'win32' ? '.exe' : '';
|
const ext: string = context.osPlat == 'win32' ? '.exe' : '';
|
||||||
return util.format('buildx-v%s.%s-%s%s', version, platform, arch, ext);
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -1,10 +1,20 @@
|
|||||||
|
import fs from 'fs';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
|
import path from 'path';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import {issueCommand} from '@actions/core/lib/command';
|
import {issueCommand} from '@actions/core/lib/command';
|
||||||
|
|
||||||
|
let _tmpDir: string;
|
||||||
export const osPlat: string = os.platform();
|
export const osPlat: string = os.platform();
|
||||||
export const osArch: string = os.arch();
|
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 interface Inputs {
|
export interface Inputs {
|
||||||
version: string;
|
version: string;
|
||||||
driver: string;
|
driver: string;
|
||||||
@ -21,11 +31,9 @@ export async function getInputs(): Promise<Inputs> {
|
|||||||
version: core.getInput('version'),
|
version: core.getInput('version'),
|
||||||
driver: core.getInput('driver') || 'docker-container',
|
driver: core.getInput('driver') || 'docker-container',
|
||||||
driverOpts: await getInputList('driver-opts', true),
|
driverOpts: await getInputList('driver-opts', true),
|
||||||
buildkitdFlags:
|
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
||||||
core.getInput('buildkitd-flags') ||
|
install: core.getBooleanInput('install'),
|
||||||
'--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
use: core.getBooleanInput('use'),
|
||||||
install: /true/i.test(core.getInput('install')),
|
|
||||||
use: /true/i.test(core.getInput('use')),
|
|
||||||
endpoint: core.getInput('endpoint'),
|
endpoint: core.getInput('endpoint'),
|
||||||
config: core.getInput('config')
|
config: core.getInput('config')
|
||||||
};
|
};
|
||||||
@ -39,10 +47,7 @@ export async function getInputList(name: string, ignoreComma?: boolean): Promise
|
|||||||
return items
|
return items
|
||||||
.split(/\r?\n/)
|
.split(/\r?\n/)
|
||||||
.filter(x => x)
|
.filter(x => x)
|
||||||
.reduce<string[]>(
|
.reduce<string[]>((acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), []);
|
||||||
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()),
|
|
||||||
[]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const asyncForEach = async (array, callback) => {
|
export const asyncForEach = async (array, callback) => {
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
import * as exec from './exec';
|
|
||||||
|
|
||||||
export async function isDaemonRunning(): Promise<boolean> {
|
|
||||||
return await exec.exec(`docker`, ['version', '--format', '{{.Server.Os}}'], true).then(res => {
|
|
||||||
return !res.stdout.includes(' ') && res.success;
|
|
||||||
});
|
|
||||||
}
|
|
34
src/exec.ts
34
src/exec.ts
@ -1,34 +0,0 @@
|
|||||||
import * as aexec from '@actions/exec';
|
|
||||||
import {ExecOptions} from '@actions/exec';
|
|
||||||
|
|
||||||
export interface ExecResult {
|
|
||||||
success: boolean;
|
|
||||||
stdout: string;
|
|
||||||
stderr: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const exec = async (command: string, args: string[] = [], silent: boolean): Promise<ExecResult> => {
|
|
||||||
let stdout: string = '';
|
|
||||||
let stderr: string = '';
|
|
||||||
|
|
||||||
const options: ExecOptions = {
|
|
||||||
silent: silent,
|
|
||||||
ignoreReturnCode: true
|
|
||||||
};
|
|
||||||
options.listeners = {
|
|
||||||
stdout: (data: Buffer) => {
|
|
||||||
stdout += data.toString();
|
|
||||||
},
|
|
||||||
stderr: (data: Buffer) => {
|
|
||||||
stderr += data.toString();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const returnCode: number = await aexec.exec(command, args, options);
|
|
||||||
|
|
||||||
return {
|
|
||||||
success: returnCode === 0,
|
|
||||||
stdout: stdout.trim(),
|
|
||||||
stderr: stderr.trim()
|
|
||||||
};
|
|
||||||
};
|
|
19
src/git.ts
Normal file
19
src/git.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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, rref] = res.stdout.trim().split(/[\s\t]/);
|
||||||
|
if (rsha.length == 0) {
|
||||||
|
throw new Error(`Cannot find remote ref for ${repo}#${ref}`);
|
||||||
|
}
|
||||||
|
return rsha;
|
||||||
|
});
|
||||||
|
}
|
52
src/main.ts
52
src/main.ts
@ -1,12 +1,11 @@
|
|||||||
import * as core from '@actions/core';
|
|
||||||
import * as exec from '@actions/exec';
|
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as semver from 'semver';
|
|
||||||
import * as buildx from './buildx';
|
import * as buildx from './buildx';
|
||||||
import * as context from './context';
|
import * as context from './context';
|
||||||
import * as mexec from './exec';
|
|
||||||
import * as stateHelper from './state-helper';
|
import * as stateHelper from './state-helper';
|
||||||
|
import * as util from './util';
|
||||||
|
import * as core from '@actions/core';
|
||||||
|
import * as exec from '@actions/exec';
|
||||||
|
|
||||||
async function run(): Promise<void> {
|
async function run(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@ -18,15 +17,17 @@ async function run(): Promise<void> {
|
|||||||
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 dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
||||||
|
|
||||||
if (!(await buildx.isAvailable()) || inputs.version) {
|
if (util.isValidUrl(inputs.version)) {
|
||||||
core.startGroup(`Installing buildx`);
|
core.startGroup(`Build and install buildx`);
|
||||||
|
await buildx.build(inputs.version, dockerConfigHome);
|
||||||
|
core.endGroup();
|
||||||
|
} else if (!(await buildx.isAvailable()) || inputs.version) {
|
||||||
|
core.startGroup(`Download and install buildx`);
|
||||||
await buildx.install(inputs.version || 'latest', dockerConfigHome);
|
await buildx.install(inputs.version || 'latest', dockerConfigHome);
|
||||||
core.endGroup();
|
core.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
const buildxVersion = await buildx.getVersion();
|
const buildxVersion = await buildx.getVersion();
|
||||||
core.info(`Using buildx ${buildxVersion}`);
|
|
||||||
|
|
||||||
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${require('uuid').v4()}`;
|
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${require('uuid').v4()}`;
|
||||||
context.setOutput('name', builderName);
|
context.setOutput('name', builderName);
|
||||||
stateHelper.setBuilderName(builderName);
|
stateHelper.setBuilderName(builderName);
|
||||||
@ -34,7 +35,7 @@ async function run(): Promise<void> {
|
|||||||
if (inputs.driver !== 'docker') {
|
if (inputs.driver !== 'docker') {
|
||||||
core.startGroup(`Creating a new builder instance`);
|
core.startGroup(`Creating a new builder instance`);
|
||||||
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
|
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
|
||||||
if (semver.satisfies(buildxVersion, '>=0.3.0')) {
|
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
|
||||||
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
|
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
|
||||||
createArgs.push('--driver-opt', driverOpt);
|
createArgs.push('--driver-opt', driverOpt);
|
||||||
});
|
});
|
||||||
@ -56,7 +57,7 @@ async function run(): Promise<void> {
|
|||||||
|
|
||||||
core.startGroup(`Booting builder`);
|
core.startGroup(`Booting builder`);
|
||||||
let bootstrapArgs: Array<string> = ['buildx', 'inspect', '--bootstrap'];
|
let bootstrapArgs: Array<string> = ['buildx', 'inspect', '--bootstrap'];
|
||||||
if (semver.satisfies(buildxVersion, '>=0.4.0')) {
|
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
|
||||||
bootstrapArgs.push('--builder', builderName);
|
bootstrapArgs.push('--builder', builderName);
|
||||||
}
|
}
|
||||||
await exec.exec('docker', bootstrapArgs);
|
await exec.exec('docker', bootstrapArgs);
|
||||||
@ -81,6 +82,9 @@ async function run(): Promise<void> {
|
|||||||
|
|
||||||
if (inputs.driver == 'docker-container') {
|
if (inputs.driver == 'docker-container') {
|
||||||
stateHelper.setContainerName(`buildx_buildkit_${builder.node_name}`);
|
stateHelper.setContainerName(`buildx_buildkit_${builder.node_name}`);
|
||||||
|
core.startGroup(`BuildKit version`);
|
||||||
|
core.info(await buildx.getBuildKitVersion(`buildx_buildkit_${builder.node_name}`));
|
||||||
|
core.endGroup();
|
||||||
}
|
}
|
||||||
if (core.isDebug() || builder.node_flags?.includes('--debug')) {
|
if (core.isDebug() || builder.node_flags?.includes('--debug')) {
|
||||||
stateHelper.setDebug('true');
|
stateHelper.setDebug('true');
|
||||||
@ -93,21 +97,29 @@ async function run(): Promise<void> {
|
|||||||
async function cleanup(): Promise<void> {
|
async function cleanup(): Promise<void> {
|
||||||
if (stateHelper.IsDebug && stateHelper.containerName.length > 0) {
|
if (stateHelper.IsDebug && stateHelper.containerName.length > 0) {
|
||||||
core.startGroup(`BuildKit container logs`);
|
core.startGroup(`BuildKit container logs`);
|
||||||
await mexec.exec('docker', ['logs', `${stateHelper.containerName}`], false).then(res => {
|
await exec
|
||||||
if (res.stderr != '' && !res.success) {
|
.getExecOutput('docker', ['logs', `${stateHelper.containerName}`], {
|
||||||
core.warning(res.stderr);
|
ignoreReturnCode: true
|
||||||
}
|
})
|
||||||
});
|
.then(res => {
|
||||||
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||||
|
core.warning(res.stderr.trim());
|
||||||
|
}
|
||||||
|
});
|
||||||
core.endGroup();
|
core.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stateHelper.builderName.length > 0) {
|
if (stateHelper.builderName.length > 0) {
|
||||||
core.startGroup(`Removing builder`);
|
core.startGroup(`Removing builder`);
|
||||||
await mexec.exec('docker', ['buildx', 'rm', `${stateHelper.builderName}`], false).then(res => {
|
await exec
|
||||||
if (res.stderr != '' && !res.success) {
|
.getExecOutput('docker', ['buildx', 'rm', `${stateHelper.builderName}`], {
|
||||||
core.warning(res.stderr);
|
ignoreReturnCode: true
|
||||||
}
|
})
|
||||||
});
|
.then(res => {
|
||||||
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||||
|
core.warning(res.stderr.trim());
|
||||||
|
}
|
||||||
|
});
|
||||||
core.endGroup();
|
core.endGroup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
src/util.ts
Normal file
8
src/util.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export function isValidUrl(url: string): boolean {
|
||||||
|
try {
|
||||||
|
new URL(url);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
Reference in New Issue
Block a user