mirror of
https://github.com/docker/build-push-action.git
synced 2025-04-19 18:06:46 +00:00
Add secrets input
Use Git as default context Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
bd6a01893d
commit
f295fbf080
11 changed files with 13571 additions and 299 deletions
|
@ -1,5 +1,6 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import tmp from 'tmp';
|
||||
import * as semver from 'semver';
|
||||
import * as context from './context';
|
||||
import * as exec from './exec';
|
||||
|
@ -16,6 +17,15 @@ export async function getImageID(): Promise<string | undefined> {
|
|||
return fs.readFileSync(iidFile, {encoding: 'utf-8'});
|
||||
}
|
||||
|
||||
export async function getSecret(kvp: string): Promise<string> {
|
||||
const [key, value] = kvp.split('=');
|
||||
const secretFile = tmp.tmpNameSync({
|
||||
tmpdir: context.tmpDir
|
||||
});
|
||||
await fs.writeFileSync(secretFile, value);
|
||||
return `id=${key},src=${secretFile}`;
|
||||
}
|
||||
|
||||
export async function isAvailable(): Promise<Boolean> {
|
||||
return await exec.exec(`docker`, ['buildx'], true).then(res => {
|
||||
if (res.stderr != '' && !res.success) {
|
||||
|
|
|
@ -4,6 +4,7 @@ import * as path from 'path';
|
|||
import * as semver from 'semver';
|
||||
import * as buildx from './buildx';
|
||||
import * as core from '@actions/core';
|
||||
import * as github from '@actions/github';
|
||||
|
||||
export const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-'));
|
||||
|
||||
|
@ -24,12 +25,15 @@ export interface Inputs {
|
|||
outputs: string[];
|
||||
cacheFrom: string[];
|
||||
cacheTo: string[];
|
||||
secrets: string[];
|
||||
}
|
||||
|
||||
export async function getInputs(): Promise<Inputs> {
|
||||
return {
|
||||
context: core.getInput('context') || '.',
|
||||
file: core.getInput('file') || './Dockerfile',
|
||||
context:
|
||||
core.getInput('context') ||
|
||||
`https://github.com/${github.context.repo.owner}/${github.context.repo.repo}#${github.context.ref}`,
|
||||
file: core.getInput('file') || 'Dockerfile',
|
||||
buildArgs: await getInputList('build-args'),
|
||||
labels: await getInputList('labels'),
|
||||
tags: await getInputList('tags'),
|
||||
|
@ -43,7 +47,8 @@ export async function getInputs(): Promise<Inputs> {
|
|||
push: /true/i.test(core.getInput('push')),
|
||||
outputs: await getInputList('outputs', true),
|
||||
cacheFrom: await getInputList('cache-from', true),
|
||||
cacheTo: await getInputList('cache-to', true)
|
||||
cacheTo: await getInputList('cache-to', true),
|
||||
secrets: await getInputList('secrets', true)
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -87,6 +92,9 @@ async function getBuildArgs(inputs: Inputs, buildxVersion: string): Promise<Arra
|
|||
await asyncForEach(inputs.cacheTo, async cacheTo => {
|
||||
args.push('--cache-to', cacheTo);
|
||||
});
|
||||
await asyncForEach(inputs.secrets, async secret => {
|
||||
args.push('--secret', await buildx.getSecret(secret));
|
||||
});
|
||||
if (inputs.file) {
|
||||
args.push('--file', inputs.file);
|
||||
}
|
||||
|
|
22
src/main.ts
22
src/main.ts
|
@ -1,6 +1,8 @@
|
|||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as buildx from './buildx';
|
||||
import {Inputs, getInputs, getArgs} from './context';
|
||||
import * as context from './context';
|
||||
import * as stateHelper from './state-helper';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
|
@ -15,18 +17,19 @@ async function run(): Promise<void> {
|
|||
core.setFailed(`Buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
|
||||
return;
|
||||
}
|
||||
stateHelper.setTmpDir(context.tmpDir);
|
||||
|
||||
const buildxVersion = await buildx.getVersion();
|
||||
core.info(`📣 Buildx version: ${buildxVersion}`);
|
||||
|
||||
let inputs: Inputs = await getInputs();
|
||||
let inputs: context.Inputs = await context.getInputs();
|
||||
if (inputs.builder) {
|
||||
core.info(`📌 Using builder instance ${inputs.builder}`);
|
||||
await buildx.use(inputs.builder);
|
||||
}
|
||||
|
||||
core.info(`🏃 Starting build...`);
|
||||
const args: string[] = await getArgs(inputs, buildxVersion);
|
||||
const args: string[] = await context.getArgs(inputs, buildxVersion);
|
||||
await exec.exec('docker', args);
|
||||
|
||||
const imageID = await buildx.getImageID();
|
||||
|
@ -40,4 +43,15 @@ async function run(): Promise<void> {
|
|||
}
|
||||
}
|
||||
|
||||
run();
|
||||
async function cleanup(): Promise<void> {
|
||||
if (stateHelper.tmpDir.length > 0) {
|
||||
core.info(`🚿 Removing temp folder ${stateHelper.tmpDir}`);
|
||||
fs.rmdirSync(stateHelper.tmpDir, {recursive: true});
|
||||
}
|
||||
}
|
||||
|
||||
if (!stateHelper.IsPost) {
|
||||
run();
|
||||
} else {
|
||||
cleanup();
|
||||
}
|
||||
|
|
12
src/state-helper.ts
Normal file
12
src/state-helper.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import * as core from '@actions/core';
|
||||
|
||||
export const IsPost = !!process.env['STATE_isPost'];
|
||||
export const tmpDir = process.env['STATE_tmpDir'] || '';
|
||||
|
||||
export function setTmpDir(tmpDir: string) {
|
||||
core.saveState('tmpDir', tmpDir);
|
||||
}
|
||||
|
||||
if (!IsPost) {
|
||||
core.saveState('isPost', 'true');
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue