Skip to content

Commit

Permalink
Add image output
Browse files Browse the repository at this point in the history
It is sometimes useful to easily refer to the image built. This adds an
output image which is the full image including a SHA tag.
  • Loading branch information
Benjamin Nørgaard committed Mar 18, 2021
1 parent 8973199 commit 516b492
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ inputs:
This flag can be used in case only authentication against docker
repo is needed
default: false
outputs:
builder:
description: buildx builder name
image:
description: full image name including git SHA tag
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
17 changes: 12 additions & 5 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import {info, debug, warning, startGroup, endGroup} from '@actions/core';
import {exec} from './exec';
import {Inputs} from './inputs';
import * as state from './state';
import * as outputs from './outputs';

export async function build(inputs: Inputs): Promise<void> {
startGroup('🏃 Starting build');

const args = await getBuildArgs(inputs);
const shaTag = await getSHATag(inputs.repository);
outputs.setImage(shaTag);
const args = await getBuildArgs(inputs, shaTag);
const res = await exec('docker', args, false);
if (res.stderr !== '' && !res.success) {
throw new Error(`buildx call failed: ${res.stderr.trim()}`);
Expand All @@ -15,7 +18,7 @@ export async function build(inputs: Inputs): Promise<void> {
endGroup();
}

async function getBuildArgs(inputs: Inputs): Promise<string[]> {
async function getBuildArgs(inputs: Inputs, shaTag: string): Promise<string[]> {
const args = ['buildx', 'build'];
if (inputs.file) {
args.push('--file', inputs.file);
Expand All @@ -26,7 +29,7 @@ async function getBuildArgs(inputs: Inputs): Promise<string[]> {
await asyncForEach(inputs.tags, async tag => {
args.push('--tag', tag);
});
args.push('--tag', await shaTag(inputs.repository));
args.push('--tag', shaTag);
if (inputs.push) {
args.push('--push');
}
Expand All @@ -52,7 +55,7 @@ export function isDockerhubRepository(repository: string): boolean {
return registry === '';
}

async function shaTag(repository: string): Promise<string> {
async function getSHATag(repository: string): Promise<string> {
const res = await exec('git', ['rev-parse', 'HEAD'], true);
if (res.stderr !== '' && !res.success) {
throw new Error(`git rev-parse HEAD failed: ${res.stderr.trim()}`);
Expand Down
4 changes: 4 additions & 0 deletions src/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ import {setOutput} from '@actions/core';
export function setBuilder(name: string): void {
setOutput('builder', name);
}

export function setImage(name: string): void {
setOutput('image', name);
}

0 comments on commit 516b492

Please sign in to comment.