Skip to content

Commit

Permalink
Merge pull request #6 from moia-oss/handle-cancelled-state
Browse files Browse the repository at this point in the history
Handle Cancelled State
  • Loading branch information
snowiow authored Jul 28, 2021
2 parents 6576500 + 31f755f commit e8a9911
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ user or role you login to needs the following permissions:
"Statement": [
{
"Effect": "Allow",
"Action": ["codepipeline:StartPipelineExecution", "codepipeline:GetPipelineExecution"],
"Action": [
"codepipeline:StartPipelineExecution",
"codepipeline:GetPipelineExecution",
"codepipeline:ListPipelineExecutions"
],
"Resource": ["arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${PipelineName}"]
}
]
}
```

`codepipeline:GetPipelineExecution` is only necessary, if you set `wait: true`,
`codepipeline:GetPipelineExecution` and `codepipeline:ListPipelineExecutions` are only necessary, if you set `wait: true`,
otherwise the GitHub Action Workflow continues without checking the pipeline
state.

Expand Down
42 changes: 29 additions & 13 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

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

40 changes: 28 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ import {
StartPipelineExecutionCommand,
GetPipelineExecutionCommand,
PipelineExecutionStatus,
ListPipelineExecutionsCommand,
} from '@aws-sdk/client-codepipeline';

const CLIENT = new CodePipelineClient({});

const sleep = (s: number) => new Promise((resolve) => setTimeout(resolve, s * 1000));

const getNewestExecutionId = async (pipelineName: string): Promise<string> => {
const command = new ListPipelineExecutionsCommand({ pipelineName, maxResults: 1 });
const data = await CLIENT.send(command);
if (data.pipelineExecutionSummaries && data.pipelineExecutionSummaries.length > 0) {
const executionId = data.pipelineExecutionSummaries[0].pipelineExecutionId;
if (executionId) {
return executionId;
}
throw new Error(`Newest pipeline execution of '${pipelineName}' has no ID`);
}
throw new Error('No Pipeline executions found');
};

const waitForPipeline = async (pipelineName: string, pipelineExecutionId: string): Promise<boolean> => {
await sleep(10);
const command = new GetPipelineExecutionCommand({ pipelineName, pipelineExecutionId });
Expand All @@ -25,25 +39,27 @@ const waitForPipeline = async (pipelineName: string, pipelineExecutionId: string
case PipelineExecutionStatus.InProgress:
core.info(`Pipeline '${pipelineName}' in progress waiting for 10 more seconds.`);
return await waitForPipeline(pipelineName, pipelineExecutionId);
case PipelineExecutionStatus.Cancelled: {
core.info(`Pipeline '${pipelineName}' was canceled. Trying to get new execution ID.`);
const newExecutionId = await getNewestExecutionId(pipelineName);
core.info(`Waiting on pipeline '${pipelineName}' with new execution id '${newExecutionId}`);
return await waitForPipeline(pipelineName, newExecutionId);
}
case PipelineExecutionStatus.Succeeded:
core.info(`Pipeline '${pipelineName}' succeeded.`);
return true;
case PipelineExecutionStatus.Failed:
core.info(`Pipeline '${pipelineName}' failed.`);
core.error(`Pipeline '${pipelineName}' failed.`);
return false;
case PipelineExecutionStatus.Stopping || PipelineExecutionStatus.Stopped:
core.info(`Pipeline '${pipelineName}' stopped.`);
core.error(`Pipeline '${pipelineName}' stopped.`);
return false;
case PipelineExecutionStatus.Superseded:
core.info(`Pipeline '${pipelineName}' was superseded.`);
return false;
case PipelineExecutionStatus.Cancelled:
core.info(`Pipeline '${pipelineName}' was canceled. Trying to get new execution ID.`);
// TODO: To implement
core.error(`Pipeline '${pipelineName}' was superseded.`);
return false;

case PipelineExecutionStatus.Succeeded:
core.info(`Pipeline '${pipelineName}' succeeded.`);
return true;
default:
throw new Error(`Unexpected status: ${status} given.`);
core.error(`Unexpected status: ${status} given.`);
return false;
}
} catch (error) {
core.error(
Expand Down

0 comments on commit e8a9911

Please sign in to comment.