Skip to content

Commit

Permalink
fail if workflow run was broken or if no artifacts found
Browse files Browse the repository at this point in the history
  • Loading branch information
maximtop committed Apr 23, 2024
1 parent 58eb6c6 commit c8927ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/lib/GitHubActionsRunner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GithubApiClient } from './github/GithubApiClient';
import { GithubApiManager } from './github/GithubApiManager';
import { GithubApiManager, WORKFLOW_RUN_SUCCESSFUL_CONCLUSION_STATUS } from './github/GithubApiManager';
import { logger, setLoggerLevel } from './utils/logger';

/**
Expand Down Expand Up @@ -183,7 +183,12 @@ export class GitHubActionsRunner {
const logs = await this.githubApiManager.fetchWorkflowRunLogs(workflowRun.id);
logger.info(logs);

if (workflowRun.conclusion !== WORKFLOW_RUN_SUCCESSFUL_CONCLUSION_STATUS) {
throw new Error(`Workflow run failed with conclusion "${workflowRun.conclusion}".`);
}

if (artifactsPath) {
// if no artifacts are found, the method will throw an error
await this.githubApiManager.downloadArtifacts(workflowRun, artifactsPath);
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/lib/github/GithubApiManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type WorkflowRun = WorkflowRuns[number];
type Artifacts = RestEndpointMethodTypes['actions']['listWorkflowRunArtifacts']['response']['data']['artifacts'];
type Artifact = Artifacts[number];

export const WORKFLOW_RUN_SUCCESSFUL_CONCLUSION_STATUS = 'success';

/**
* Statuses for a workflow run, indicating state of the workflow in the progress.
*/
Expand Down Expand Up @@ -439,11 +441,21 @@ export class GithubApiManager {
* @param workflowRun The workflow run to download artifacts from.
* @param artifactsPath The path to save the downloaded artifacts.
* @returns A promise that resolves when all artifacts are downloaded.
* @throws An error if the download fails.
* @throws An error if the download fails or no artifacts are found.
*/
async downloadArtifacts(workflowRun: WorkflowRun, artifactsPath: string): Promise<void> {
logger.info('Downloading artifacts...');

const artifactsList = await this.listWorkflowArtifacts(workflowRun.id);

/**
* This method is called only when an artifacts path is provided, indicating that artifacts are expected.
* Consequently, if no artifacts are found, an error should be thrown.
*/
if (artifactsList.length === 0) {
throw new Error('No artifacts found');
}

logger.info(`Artifacts found: ${artifactsList.map((artifact) => artifact.name).join(', ')}`);

await Promise.all(artifactsList.map((artifact) => {
Expand Down

0 comments on commit c8927ac

Please sign in to comment.