Skip to content

Commit

Permalink
Check if buildx installed (builder alias)
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
CrazyMax committed Aug 16, 2020
1 parent 6b0b71d commit ac03ceb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
27 changes: 24 additions & 3 deletions dist/index.js

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

24 changes: 24 additions & 0 deletions src/buildx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from 'fs';
import path from 'path';
import os from 'os';
import * as exec from './exec';

interface DockerConfig {
credsStore?: string;
experimental?: string;
stackOrchestrator?: string;
aliases?: {
builder?: string;
};
}

export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr != '' && !res.success) {
Expand All @@ -9,6 +21,18 @@ export async function isAvailable(): Promise<Boolean> {
});
}

export async function isInstalled(): Promise<Boolean> {
const dockerHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');

const dockerCfgFile: string = path.join(dockerHome, 'config.json');
if (!fs.existsSync(dockerCfgFile)) {
return false;
}

const dockerCfg: DockerConfig = JSON.parse(fs.readFileSync(dockerCfgFile, {encoding: 'utf-8'}));
return dockerCfg.aliases?.builder == 'buildx';
}

export async function use(builder: string): Promise<void> {
return await exec.exec(`docker`, ['buildx', 'use', '--builder', builder], false).then(res => {
if (res.stderr != '' && !res.success) {
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ async function run(): Promise<void> {
}

const inputs: Inputs = await loadInputs();
const buildxAvailable = await buildx.isAvailable();
const buildxInstalled = buildxAvailable && (await buildx.isInstalled());
const buildxEnabled = (await mustBuildx(inputs)) || buildxInstalled;
let buildArgs: Array<string> = [];
const buildxEnabled = await mustBuildx(inputs);

// Check buildx
if (buildxEnabled) {
if (await !buildx.isAvailable()) {
if (!buildxAvailable) {
core.setFailed(`Buildx is required but not available`);
return;
}
Expand Down

0 comments on commit ac03ceb

Please sign in to comment.