Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #130 from crazy-max/update-dev
chore: update dev dependencies and workflow
CrazyMax authored and GitHub committed Mar 21, 2022
2 parents c62171c + 2e23606 commit 20111c6
Showing 21 changed files with 2,157 additions and 16,299 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,23 @@
{
"env": {
"node": true,
"es2021": true,
"jest/globals": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"jest",
"prettier"
]
}
72 changes: 22 additions & 50 deletions __tests__/buildx.test.ts
@@ -1,3 +1,4 @@
import {describe, expect, it, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
@@ -21,35 +22,21 @@ jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => {
});

describe('isAvailable', () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
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('getVersion', () => {
async function 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)(
'valid',
async () => {
const version = await buildx.getVersion();
console.log(`version: ${version}`);
expect(semver.valid(version)).not.toBeNull();
},
100000
);
it('valid', async () => {
const version = await buildx.getVersion();
expect(semver.valid(version)).not.toBeNull();
});
});

describe('parseVersion', () => {
@@ -74,54 +61,41 @@ describe('satisfies', () => {
});

describe('inspect', () => {
async function 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)(
'valid',
async () => {
const builder = await buildx.inspect('');
console.log('builder', builder);
expect(builder).not.toBeUndefined();
expect(builder.name).not.toEqual('');
expect(builder.driver).not.toEqual('');
expect(builder.node_platforms).not.toEqual('');
},
100000
);
it('valid', async () => {
const builder = await buildx.inspect('');
expect(builder).not.toBeUndefined();
expect(builder.name).not.toEqual('');
expect(builder.driver).not.toEqual('');
expect(builder.node_platforms).not.toEqual('');
}, 100000);
});

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);
console.log(buildxBin);
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);
console.log(buildxBin);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);
});

describe('install', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));

it('acquires v0.4.1 version of buildx', async () => {
const buildxBin = await buildx.install('v0.4.1', tmpDir);
console.log(buildxBin);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);

it('acquires latest version of buildx', async () => {
const buildxBin = await buildx.install('latest', tmpDir);
console.log(buildxBin);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);
});
@@ -148,13 +122,11 @@ describe('getConfig', () => {
config = await buildx.getConfigInline(val);
}
expect(true).toBe(!invalid);
console.log(`config: ${config}`);
expect(config).toEqual(`${tmpNameSync}`);
const configValue = await fs.readFileSync(tmpNameSync, 'utf-8');
console.log(`configValue: ${configValue}`);
const configValue = fs.readFileSync(tmpNameSync, 'utf-8');
expect(configValue).toEqual(exValue);
} catch (err) {
console.log(err);
// eslint-disable-next-line jest/no-conditional-expect
expect(true).toBe(invalid);
}
});
15 changes: 5 additions & 10 deletions __tests__/context.test.ts
@@ -1,3 +1,4 @@
import {beforeEach, describe, expect, it, jest} from '@jest/globals';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
@@ -19,63 +20,54 @@ describe('getInputList', () => {
it('handles single line correctly', async () => {
await setInput('foo', 'bar');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar']);
});

it('handles multiple lines correctly', async () => {
setInput('foo', 'bar\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('remove empty lines correctly', async () => {
setInput('foo', 'bar\n\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('remove empty result correctly', async () => {
setInput('foo', 'bar,baz,');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = await context.getInputList('foo');
console.log(res);
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');
console.log(res);
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);
console.log(res);
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);
console.log(res);
expect(res).toEqual(['image=moby/buildkit:master', 'network=host']);
});
});
@@ -95,19 +87,22 @@ describe('asyncForEach', () => {

describe('setOutput', () => {
beforeEach(() => {
process.stdout.write = jest.fn();
process.stdout.write = jest.fn() as typeof process.stdout.write;
});

// eslint-disable-next-line jest/expect-expect
it('setOutput produces the correct command', () => {
context.setOutput('some output', 'some value');
assertWriteCalls([`::set-output name=some output::some value${os.EOL}`]);
});

// eslint-disable-next-line jest/expect-expect
it('setOutput handles bools', () => {
context.setOutput('some output', false);
assertWriteCalls([`::set-output name=some output::false${os.EOL}`]);
});

// eslint-disable-next-line jest/expect-expect
it('setOutput handles numbers', () => {
context.setOutput('some output', 1.01);
assertWriteCalls([`::set-output name=some output::1.01${os.EOL}`]);
2 changes: 1 addition & 1 deletion __tests__/git.test.ts
@@ -1,9 +1,9 @@
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');
console.log(`ref: ${ref}`);
expect(ref).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
});
});
3 changes: 1 addition & 2 deletions __tests__/github.test.ts
@@ -1,16 +1,15 @@
import {describe, expect, it} from '@jest/globals';
import * as github from '../src/github';

describe('github', () => {
it('returns latest buildx GitHub release', async () => {
const release = await github.getRelease('latest');
console.log(release);
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});

it('returns v0.2.2 buildx GitHub release', async () => {
const release = await github.getRelease('v0.2.2');
console.log(release);
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.2.2');
});
1 change: 1 addition & 0 deletions __tests__/util.test.ts
@@ -1,3 +1,4 @@
import {describe, expect, test} from '@jest/globals';
import * as util from '../src/util';

describe('isValidUrl', () => {
12 changes: 6 additions & 6 deletions hack/build.Dockerfile → dev.Dockerfile
@@ -1,8 +1,8 @@
# syntax=docker/dockerfile:1.3-labs
# syntax=docker/dockerfile:1

ARG NODE_VERSION
ARG DOCKER_VERSION=20.10.10
ARG BUILDX_VERSION=0.7.0
ARG NODE_VERSION=12
ARG DOCKER_VERSION=20.10.13
ARG BUILDX_VERSION=0.8.1

FROM node:${NODE_VERSION}-alpine AS base
RUN apk add --no-cache cpio findutils git
@@ -57,10 +57,10 @@ RUN --mount=type=bind,target=.,rw \
FROM scratch AS format-update
COPY --from=format /out /

FROM deps AS format-validate
FROM deps AS lint
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/src/node_modules \
yarn run format-check
yarn run lint

FROM docker:${DOCKER_VERSION} as docker
FROM docker/buildx-bin:${BUILDX_VERSION} as buildx
14,564 changes: 3 additions & 14,561 deletions dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/index.js.map

Large diffs are not rendered by default.

475 changes: 475 additions & 0 deletions dist/licenses.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/sourcemap-register.js

Large diffs are not rendered by default.

37 changes: 10 additions & 27 deletions docker-bake.hcl
@@ -1,13 +1,3 @@
variable "NODE_VERSION" {
default = "12"
}

target "node-version" {
args = {
NODE_VERSION = NODE_VERSION
}
}

group "default" {
targets = ["build"]
}
@@ -17,54 +7,47 @@ group "pre-checkin" {
}

group "validate" {
targets = ["format-validate", "build-validate", "vendor-validate"]
targets = ["lint", "build-validate", "vendor-validate"]
}

target "build" {
inherits = ["node-version"]
dockerfile = "./hack/build.Dockerfile"
dockerfile = "dev.Dockerfile"
target = "build-update"
output = ["."]
}

target "build-validate" {
inherits = ["node-version"]
dockerfile = "./hack/build.Dockerfile"
dockerfile = "dev.Dockerfile"
target = "build-validate"
output = ["type=cacheonly"]
}

target "format" {
inherits = ["node-version"]
dockerfile = "./hack/build.Dockerfile"
dockerfile = "dev.Dockerfile"
target = "format-update"
output = ["."]
}

target "format-validate" {
inherits = ["node-version"]
dockerfile = "./hack/build.Dockerfile"
target = "format-validate"
target "lint" {
dockerfile = "dev.Dockerfile"
target = "lint"
output = ["type=cacheonly"]
}

target "vendor-update" {
inherits = ["node-version"]
dockerfile = "./hack/build.Dockerfile"
dockerfile = "dev.Dockerfile"
target = "vendor-update"
output = ["."]
}

target "vendor-validate" {
inherits = ["node-version"]
dockerfile = "./hack/build.Dockerfile"
dockerfile = "dev.Dockerfile"
target = "vendor-validate"
output = ["type=cacheonly"]
}

target "test" {
inherits = ["node-version"]
dockerfile = "./hack/build.Dockerfile"
dockerfile = "dev.Dockerfile"
target = "test-coverage"
output = ["./coverage"]
}
4 changes: 1 addition & 3 deletions jest.config.js → jest.config.ts
@@ -2,11 +2,9 @@ module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
setupFiles: ["dotenv/config"],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: false
verbose: true
}
33 changes: 19 additions & 14 deletions package.json
@@ -3,11 +3,11 @@
"description": "Set up Docker Buildx",
"main": "lib/main.js",
"scripts": {
"build": "tsc && ncc build",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"build": "ncc build src/main.ts --source-map --minify --license licenses.txt",
"lint": "eslint src/**/*.ts __tests__/**/*.ts",
"format": "eslint --fix src/**/*.ts __tests__/**/*.ts",
"test": "jest --coverage",
"pre-checkin": "yarn run format && yarn run build"
"all": "yarn run build && yarn run format && yarn test"
},
"repository": {
"type": "git",
@@ -36,16 +36,21 @@
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/node": "^14.17.4",
"@vercel/ncc": "^0.28.6",
"dotenv": "^8.6.0",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"jest-runtime": "^26.6.3",
"@types/node": "^16.11.26",
"@types/semver": "^7.3.9",
"@types/tmp": "^0.2.3",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"@vercel/ncc": "^0.33.3",
"dotenv": "^16.0.0",
"eslint": "^8.11.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^26.1.1",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.2.5",
"prettier": "^2.3.1",
"ts-jest": "^26.5.6",
"typescript": "^4.3.4",
"typescript-formatter": "^7.2.2"
"ts-jest": "^27.1.2",
"ts-node": "^10.7.0",
"typescript": "^4.4.4"
}
}
17 changes: 6 additions & 11 deletions src/buildx.ts
@@ -41,7 +41,7 @@ export async function getConfig(s: string, file: boolean): Promise<string> {
return configFile;
}

export async function isAvailable(): Promise<Boolean> {
export async function isAvailable(): Promise<boolean> {
return await exec
.getExecOutput('docker', ['buildx'], {
ignoreReturnCode: true,
@@ -134,6 +134,7 @@ export async function inspect(name: string): Promise<Builder> {
}

export async function build(inputBuildRef: string, dockerConfigHome: string): Promise<string> {
// eslint-disable-next-line prefer-const
let [repo, ref] = inputBuildRef.split('#');
if (ref.length == 0) {
ref = 'master';
@@ -208,16 +209,9 @@ async function setPlugin(toolPath: string, dockerConfigHome: string): Promise<st
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));
let downloadPath: string;

try {
core.info(`Downloading ${downloadUrl}`);
downloadPath = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded to ${downloadPath}`);
} catch (error) {
throw error;
}

core.info(`Downloading ${downloadUrl}`);
const downloadPath = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded to ${downloadPath}`);
return await tc.cacheFile(downloadPath, targetFile, 'buildx', version);
}

@@ -233,6 +227,7 @@ async function filename(version: string): Promise<string> {
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;
2 changes: 1 addition & 1 deletion src/context.ts
@@ -64,6 +64,6 @@ export const asyncForEach = async (array, callback) => {
};

// FIXME: Temp fix https://github.com/actions/toolkit/issues/777
export function setOutput(name: string, value: any): void {
export function setOutput(name: string, value: unknown): void {
issueCommand('set-output', {name}, value);
}
2 changes: 1 addition & 1 deletion src/git.ts
@@ -10,7 +10,7 @@ export async function getRemoteSha(repo: string, ref: string): Promise<string> {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
const [rsha, rref] = res.stdout.trim().split(/[\s\t]/);
const [rsha] = res.stdout.trim().split(/[\s\t]/);
if (rsha.length == 0) {
throw new Error(`Cannot find remote ref for ${repo}#${ref}`);
}
2 changes: 1 addition & 1 deletion src/github.ts
@@ -6,7 +6,7 @@ export interface GitHubRelease {
}

export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const url: string = `https://github.com/docker/buildx/releases/${version}`;
const url = `https://github.com/docker/buildx/releases/${version}`;
const http: httpm.HttpClient = new httpm.HttpClient('setup-buildx');
return (await http.getJson<GitHubRelease>(url)).result;
};
7 changes: 4 additions & 3 deletions src/main.ts
@@ -1,5 +1,6 @@
import * as os from 'os';
import * as path from 'path';
import * as uuid from 'uuid';
import * as buildx from './buildx';
import * as context from './context';
import * as stateHelper from './state-helper';
@@ -28,13 +29,13 @@ async function run(): Promise<void> {
}

const buildxVersion = await buildx.getVersion();
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${require('uuid').v4()}`;
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
context.setOutput('name', builderName);
stateHelper.setBuilderName(builderName);

if (inputs.driver !== 'docker') {
core.startGroup(`Creating a new builder instance`);
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
const createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
createArgs.push('--driver-opt', driverOpt);
@@ -58,7 +59,7 @@ async function run(): Promise<void> {
core.endGroup();

core.startGroup(`Booting builder`);
let bootstrapArgs: Array<string> = ['buildx', 'inspect', '--bootstrap'];
const bootstrapArgs: Array<string> = ['buildx', 'inspect', '--bootstrap'];
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
bootstrapArgs.push('--builder', builderName);
}
15 changes: 8 additions & 7 deletions tsconfig.json
@@ -2,17 +2,18 @@
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"newLine": "lf",
"outDir": "./lib",
"rootDir": "./src",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true,
"sourceMap": true
"useUnknownInCatchVariables": false,
},
"exclude": ["node_modules", "**/*.test.ts"]
"exclude": [
"node_modules",
"**/*.test.ts",
"jest.config.ts"
]
}
3,168 changes: 1,567 additions & 1,601 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit 20111c6

Please sign in to comment.