Skip to content

Commit

Permalink
Add build-ref input
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 30, 2021
1 parent a1c666d commit f258f34
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 10 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -313,3 +313,26 @@ jobs:
echo "Status: ${{ steps.buildx.outputs.status }}"
echo "Flags: ${{ steps.buildx.outputs.flags }}"
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:
build-ref: https://github.com/docker/buildx.git#${{ matrix.ref }}
-
name: Check version
run: |
docker buildx version
38 changes: 35 additions & 3 deletions __tests__/buildx.test.ts
@@ -1,10 +1,19 @@
import fs = require('fs');
import * as buildx from '../src/buildx';
import * as path from 'path';
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';

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();
Expand Down Expand Up @@ -72,6 +81,29 @@ describe('inspect', () => {
);
});

// describe('build', () => {
// 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 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', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
it('acquires v0.4.1 version of buildx', async () => {
Expand Down
10 changes: 10 additions & 0 deletions __tests__/context.test.ts
@@ -1,6 +1,16 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
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', () => {
it('handles single line correctly', async () => {
await setInput('foo', 'bar');
Expand Down
9 changes: 9 additions & 0 deletions __tests__/git.test.ts
@@ -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');
});
});
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -10,6 +10,9 @@ inputs:
version:
description: 'Buildx version. (eg. v0.3.0)'
required: false
build-ref:
description: 'Build and install buildx from a Git ref'
required: false
driver:
description: 'Sets the builder driver to be used'
default: 'docker-container'
Expand Down
122 changes: 117 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/buildx.ts
Expand Up @@ -3,6 +3,7 @@ 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 github from './github';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
Expand Down Expand Up @@ -106,6 +107,43 @@ export async function inspect(name: string): Promise<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', '--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> {
const release: github.GitHubRelease | null = await github.getRelease(inputVersion);
if (!release) {
Expand All @@ -124,6 +162,10 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
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');
core.debug(`Plugins dir is ${pluginsDir}`);
if (!fs.existsSync(pluginsDir)) {
Expand Down
12 changes: 12 additions & 0 deletions src/context.ts
@@ -1,12 +1,23 @@
import fs from 'fs';
import * as os from 'os';
import path from 'path';
import * as core from '@actions/core';
import {issueCommand} from '@actions/core/lib/command';

let _tmpDir: string;
export const osPlat: string = os.platform();
export const osArch: string = os.arch();

export function tmpDir(): string {
if (!_tmpDir) {
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep);
}
return _tmpDir;
}

export interface Inputs {
version: string;
buildRef: string;
driver: string;
driverOpts: string[];
buildkitdFlags: string;
Expand All @@ -19,6 +30,7 @@ export interface Inputs {
export async function getInputs(): Promise<Inputs> {
return {
version: core.getInput('version'),
buildRef: core.getInput('build-ref'),
driver: core.getInput('driver') || 'docker-container',
driverOpts: await getInputList('driver-opts', true),
buildkitdFlags:
Expand Down

0 comments on commit f258f34

Please sign in to comment.