mirror of
				https://github.com/docker/build-push-action.git
				synced 2025-11-04 00:10:55 +00:00 
			
		
		
		
	Merge pull request #51 from useblacksmith/wrap-err
src: wrap all steps after blacksmith builder in try catch
This commit is contained in:
		
				commit
				
					
						2ca2ad464d
					
				
			
		
					 3 changed files with 202 additions and 227 deletions
				
			
		
							
								
								
									
										2
									
								
								dist/index.js
									
										
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/index.js
									
										
									
										generated
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										2
									
								
								dist/index.js.map
									
										
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/index.js.map
									
										
									
										generated
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										57
									
								
								src/main.ts
									
										
									
									
									
								
							
							
						
						
									
										57
									
								
								src/main.ts
									
										
									
									
									
								
							| 
						 | 
					@ -569,6 +569,10 @@ actionsToolkit.run(
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let buildError: Error | undefined;
 | 
				
			||||||
 | 
					    let buildDurationSeconds: string | undefined;
 | 
				
			||||||
 | 
					    let ref: string | undefined;
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
      if (builderInfo.addr) {
 | 
					      if (builderInfo.addr) {
 | 
				
			||||||
        await core.group(`Creating a builder instance`, async () => {
 | 
					        await core.group(`Creating a builder instance`, async () => {
 | 
				
			||||||
          const name = `blacksmith-${Date.now().toString(36)}`;
 | 
					          const name = `blacksmith-${Date.now().toString(36)}`;
 | 
				
			||||||
| 
						 | 
					@ -583,7 +587,6 @@ actionsToolkit.run(
 | 
				
			||||||
          });
 | 
					          });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
      // If we failed to obtain the address, let's check if we have an already configured builder.
 | 
					 | 
				
			||||||
        await core.group(`Checking for configured builder`, async () => {
 | 
					        await core.group(`Checking for configured builder`, async () => {
 | 
				
			||||||
          try {
 | 
					          try {
 | 
				
			||||||
            const builder = await toolkit.builder.inspect();
 | 
					            const builder = await toolkit.builder.inspect();
 | 
				
			||||||
| 
						 | 
					@ -647,9 +650,7 @@ actionsToolkit.run(
 | 
				
			||||||
      core.debug(`buildCmd.command: ${buildCmd.command}`);
 | 
					      core.debug(`buildCmd.command: ${buildCmd.command}`);
 | 
				
			||||||
      core.debug(`buildCmd.args: ${JSON.stringify(buildCmd.args)}`);
 | 
					      core.debug(`buildCmd.args: ${JSON.stringify(buildCmd.args)}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let err: Error | undefined;
 | 
					 | 
				
			||||||
      const buildStartTime = Date.now();
 | 
					      const buildStartTime = Date.now();
 | 
				
			||||||
    let buildDurationSeconds: string | undefined;
 | 
					 | 
				
			||||||
      await Exec.getExecOutput(buildCmd.command, buildCmd.args, {
 | 
					      await Exec.getExecOutput(buildCmd.command, buildCmd.args, {
 | 
				
			||||||
        ignoreReturnCode: true,
 | 
					        ignoreReturnCode: true,
 | 
				
			||||||
        env: Object.assign({}, process.env, {
 | 
					        env: Object.assign({}, process.env, {
 | 
				
			||||||
| 
						 | 
					@ -658,11 +659,11 @@ actionsToolkit.run(
 | 
				
			||||||
          [key: string]: string;
 | 
					          [key: string]: string;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }).then(res => {
 | 
					      }).then(res => {
 | 
				
			||||||
      if (res.stderr.length > 0 && res.exitCode != 0) {
 | 
					 | 
				
			||||||
        err = Error(`buildx failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
        buildDurationSeconds = Math.round((Date.now() - buildStartTime) / 1000).toString();
 | 
					        buildDurationSeconds = Math.round((Date.now() - buildStartTime) / 1000).toString();
 | 
				
			||||||
        stateHelper.setDockerBuildDurationSeconds(buildDurationSeconds);
 | 
					        stateHelper.setDockerBuildDurationSeconds(buildDurationSeconds);
 | 
				
			||||||
 | 
					        if (res.stderr.length > 0 && res.exitCode != 0) {
 | 
				
			||||||
 | 
					          throw Error(`buildx failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      const imageID = toolkit.buildxBuild.resolveImageID();
 | 
					      const imageID = toolkit.buildxBuild.resolveImageID();
 | 
				
			||||||
| 
						 | 
					@ -730,16 +731,12 @@ actionsToolkit.run(
 | 
				
			||||||
          stateHelper.setSummarySupported();
 | 
					          stateHelper.setSummarySupported();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
    if (builderInfo.addr) {
 | 
					      buildError = error as Error;
 | 
				
			||||||
      if (err) {
 | 
					 | 
				
			||||||
        core.info(`Build failed: ${err.message}`);
 | 
					 | 
				
			||||||
        stateHelper.setDockerBuildStatus('failure');
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        core.info('Build completed successfully');
 | 
					 | 
				
			||||||
        stateHelper.setDockerBuildStatus('success');
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await core.group('Cleaning up Blacksmith builder', async () => {
 | 
				
			||||||
 | 
					      if (builderInfo.addr) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
          const buildxHistory = new BuildxHistory();
 | 
					          const buildxHistory = new BuildxHistory();
 | 
				
			||||||
          const exportRes = await buildxHistory.export({
 | 
					          const exportRes = await buildxHistory.export({
 | 
				
			||||||
| 
						 | 
					@ -761,7 +758,7 @@ actionsToolkit.run(
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          core.info('Unmounted device');
 | 
					          core.info('Unmounted device');
 | 
				
			||||||
        if (!err) {
 | 
					          if (!buildError) {
 | 
				
			||||||
            await reportBuildCompleted(exportRes, builderInfo.buildId, ref, buildDurationSeconds);
 | 
					            await reportBuildCompleted(exportRes, builderInfo.buildId, ref, buildDurationSeconds);
 | 
				
			||||||
          } else {
 | 
					          } else {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
| 
						 | 
					@ -777,9 +774,11 @@ actionsToolkit.run(
 | 
				
			||||||
          core.warning(`Error during Blacksmith builder shutdown: ${error.message}`);
 | 
					          core.warning(`Error during Blacksmith builder shutdown: ${error.message}`);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (err) {
 | 
					    // Re-throw the error after cleanup
 | 
				
			||||||
      throw err;
 | 
					    if (buildError) {
 | 
				
			||||||
 | 
					      throw buildError;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  // post
 | 
					  // post
 | 
				
			||||||
| 
						 | 
					@ -827,27 +826,3 @@ function buildSummaryEnabled(): boolean {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return true;
 | 
					  return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
function buildRecordUploadEnabled(): boolean {
 | 
					 | 
				
			||||||
  if (process.env.DOCKER_BUILD_RECORD_UPLOAD) {
 | 
					 | 
				
			||||||
    return Util.parseBool(process.env.DOCKER_BUILD_RECORD_UPLOAD);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  return true;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function buildRecordRetentionDays(): number | undefined {
 | 
					 | 
				
			||||||
  let val: string | undefined;
 | 
					 | 
				
			||||||
  if (process.env.DOCKER_BUILD_EXPORT_RETENTION_DAYS) {
 | 
					 | 
				
			||||||
    core.warning('DOCKER_BUILD_EXPORT_RETENTION_DAYS is deprecated. Use DOCKER_BUILD_RECORD_RETENTION_DAYS instead.');
 | 
					 | 
				
			||||||
    val = process.env.DOCKER_BUILD_EXPORT_RETENTION_DAYS;
 | 
					 | 
				
			||||||
  } else if (process.env.DOCKER_BUILD_RECORD_RETENTION_DAYS) {
 | 
					 | 
				
			||||||
    val = process.env.DOCKER_BUILD_RECORD_RETENTION_DAYS;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  if (val) {
 | 
					 | 
				
			||||||
    const res = parseInt(val);
 | 
					 | 
				
			||||||
    if (isNaN(res)) {
 | 
					 | 
				
			||||||
      throw Error(`Invalid build record retention days: ${val}`);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return res;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue