plumb through the dockerfile path when creating a build_task

This commit is contained in:
Aayush 2024-09-12 17:11:11 -07:00
parent fca077e64d
commit cb250fea79
No known key found for this signature in database
4 changed files with 74 additions and 63 deletions

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -79,6 +79,19 @@ export async function getInputs(): Promise<Inputs> {
};
}
// getDockerfilePath resolves the path to the build entity. This is basically
// {context}/{file} or {context}/{dockerfile} depending on the inputs.
export function getDockerfilePath(inputs: Inputs): string {
const context = inputs.context || Context.gitContext();
if (inputs.file) {
return context + '/' + inputs.file;
} else if (inputs['dockerfile']) {
return context + '/' + inputs['dockerfile'];
} else {
return context;
}
}
export function sanitizeInputs(inputs: Inputs) {
const res = {};
for (const key of Object.keys(inputs)) {
@ -284,12 +297,12 @@ async function getAttestArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<st
return args;
}
export async function getRemoteBuilderArgs(name: string, inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
export async function getRemoteBuilderArgs(name: string, builderUrl: string): Promise<Array<string>> {
const args: Array<string> = ['create', '--name', name, '--driver', 'remote'];
if (inputs.platforms.length > 0) {
args.push('--platform', inputs.platforms.join(','));
}
args.push('--platform', 'linux/amd64');
// Always use the remote builder, overriding whatever has been configured so far.
args.push('--use');
// Use the provided builder URL
args.push(builderUrl);
return args;
}

View file

@ -20,7 +20,7 @@ import axios, { AxiosInstance } from 'axios';
import * as context from './context';
const buildxVersion = "v0.17.0"
const buildxVersion = 'v0.17.0';
async function getBlacksmithHttpClient(): Promise<AxiosInstance> {
return axios.create({
@ -45,8 +45,7 @@ async function reportBuildCompleted() {
async function reportBuildFailed() {
try {
const client = await getBlacksmithHttpClient();
// TODO(adityamaru): This endpoint doesn't exist yet.
const response = await client.post(`/${stateHelper.blacksmithBuildTaskId}/failed`);
const response = await client.post(`/${stateHelper.blacksmithBuildTaskId}/fail`);
core.info(`Docker build failed, tearing down Blacksmith builder for ${stateHelper.blacksmithBuildTaskId}: ${JSON.stringify(response.data)}`);
} catch (error) {
core.warning('Error completing Blacksmith build:', error);
@ -56,12 +55,19 @@ async function reportBuildFailed() {
// getRemoteBuilderAddr resolves the address to a remote Docker builder.
// If it is unable to do so because of a timeout or an error it returns null.
async function getRemoteBuilderAddr(): Promise<string | null> {
async function getRemoteBuilderAddr(inputs: context.Inputs): Promise<string | null> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000);
try {
const client = await getBlacksmithHttpClient();
const response = await client.post('');
const dockerfilePath = context.getDockerfilePath(inputs);
let payload = {};
if (dockerfilePath && dockerfilePath.length > 0) {
payload = {dockerfile_path: dockerfilePath};
core.info(`Using dockerfile path: ${dockerfilePath}`);
}
core.info(`Waiting for Blacksmith builder agent to be ready...`);
const response = await client.post('', payload);
const data = response.data;
const taskId = data['id'] as string;
@ -77,11 +83,10 @@ async function getRemoteBuilderAddr(): Promise<string | null> {
while (Date.now() - startTime < 60000) {
const response = await client.get(`/${taskId}`);
const data = response.data;
core.info(`Got response from Blacksmith builder ${taskId}: ${JSON.stringify(data, null, 2)}`);
const ec2Instance = data['ec2_instance'] ?? null;
if (ec2Instance) {
const elapsedTime = Date.now() - startTime;
core.info(`Got EC2 instance IP after ${elapsedTime} ms`);
const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
core.info(`Blacksmith builder agent ready after ${elapsedTime} seconds`);
return `tcp://${ec2Instance['instance_ip']}:4242` as string;
}
await new Promise(resolve => setTimeout(resolve, 200));
@ -122,13 +127,11 @@ async function setupBuildx(version: string, toolkit: Toolkit): Promise<void> {
});
}
actionsToolkit.run(
// main
async () => {
const startedTime = new Date();
const inputs: context.Inputs = await context.getInputs();
core.debug(`inputs: ${JSON.stringify(inputs)}`);
stateHelper.setInputs(inputs);
const toolkit = new Toolkit();
@ -159,23 +162,18 @@ actionsToolkit.run(
}
});
let remoteBuilderAddr: string | null = null;
await core.group(`Starting Blacksmith remote builder`, async () => {
// TODO(adityamaru): Plumb the dockerfile path as the entity name.
remoteBuilderAddr = await getRemoteBuilderAddr();
if (remoteBuilderAddr) {
core.info(`Successfully obtained Blacksmith remote builder address: ${remoteBuilderAddr}`);
} else {
remoteBuilderAddr = await getRemoteBuilderAddr(inputs);
if (!remoteBuilderAddr) {
core.warning('Failed to obtain Blacksmith remote builder address. Falling back to a local build.');
}
});
if (remoteBuilderAddr) {
await core.group(`Creating a remote builder instance`, async () => {
// TODO(during review): do we want this to be something useful?
const name = `test-name`
const createCmd = await toolkit.buildx.getCommand(await context.getRemoteBuilderArgs(name, inputs, toolkit));
const name = `blacksmith`;
const createCmd = await toolkit.buildx.getCommand(await context.getRemoteBuilderArgs(name, remoteBuilderAddr!));
core.info(`Creating builder with command: ${createCmd.command}`);
await Exec.getExecOutput(createCmd.command, createCmd.args, {
ignoreReturnCode: true
@ -194,7 +192,7 @@ actionsToolkit.run(
core.debug(`Found configured builder: ${builder.name}`);
} else {
// TODO(adityamaru): Setup a "default" builder that will build locally.
core.setFailed("No builder found. Please configure a builder before running this action.");
core.setFailed('No builder found. Please configure a builder before running this action.');
}
} catch (error) {
core.setFailed(`Error configuring builder: ${error.message}`);
@ -334,13 +332,6 @@ actionsToolkit.run(
},
// post
async () => {
if (stateHelper.remoteDockerBuildStatus != '') {
if (stateHelper.remoteDockerBuildStatus == 'success') {
await reportBuildCompleted();
} else {
await reportBuildFailed();
}
}
if (stateHelper.isSummarySupported) {
await core.group(`Generating build summary`, async () => {
try {
@ -375,6 +366,13 @@ actionsToolkit.run(
}
});
}
if (stateHelper.remoteDockerBuildStatus != '') {
if (stateHelper.remoteDockerBuildStatus == 'success') {
await reportBuildCompleted();
} else {
await reportBuildFailed();
}
}
if (stateHelper.tmpDir.length > 0) {
await core.group(`Removing temp folder ${stateHelper.tmpDir}`, async () => {
fs.rmSync(stateHelper.tmpDir, {recursive: true});