Skip to content

Commit

Permalink
Refactor Docker config
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 ac03ceb commit f7cac3b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
name: Checkout
uses: actions/checkout@v2.3.1
-
name: Build
name: Build and push
uses: ./
with:
context: ./test
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
context: ./test
file: ./test/Dockerfile-${{ matrix.dockerfile }}
builder: ${{ steps.buildx.outputs.builder }}
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/386,linux/ppc64le,linux/s390x
platforms: linux/amd64,linux/arm64,linux/386
#push: true
tags: |
localhost:5000/name/app:latest
Expand Down
37 changes: 22 additions & 15 deletions dist/index.js

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

24 changes: 3 additions & 21 deletions src/buildx.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import fs from 'fs';
import path from 'path';
import os from 'os';
import * as docker from './docker';
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 @@ -22,15 +11,8 @@ 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';
const dockerCfg = await docker.config();
return dockerCfg?.aliases?.builder == 'buildx';
}

export async function use(builder: string): Promise<void> {
Expand Down
24 changes: 24 additions & 0 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import path from 'path';
import os from 'os';
import fs from 'fs';

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

export interface Image {
registry?: string;
namespace?: string;
repository: string;
tag?: string;
}

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

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

return JSON.parse(fs.readFileSync(file, {encoding: 'utf-8'})) as Config;
}

export const parseImage = async (image: string): Promise<Image | undefined> => {
const match = image.match(/^(?:([^\/]+)\/)?(?:([^\/]+)\/)?([^@:\/]+)(?:[@:](.+))?$/);
if (!match) {
Expand Down

0 comments on commit f7cac3b

Please sign in to comment.