Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
feat: upgrade cicd
Browse files Browse the repository at this point in the history
  • Loading branch information
arantespp committed May 20, 2021
1 parent 7a08146 commit a070754
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 73 deletions.
16 changes: 11 additions & 5 deletions packages/cli/src/deploy/cicd/cicd.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
} from '../baseStack/config';

import type { Pipeline } from './pipelines';
import { ECS_TASK_DEFAULT_CPU, ECS_TASK_DEFAULT_MEMORY } from './config';
import {
ECS_TASK_DEFAULT_CPU,
ECS_TASK_DEFAULT_MEMORY,
PIPELINE_ECS_TASK_EXECUTION_MANUAL_APPROVAL_ACTION_NAME,
PIPELINE_ECS_TASK_EXECUTION_STAGE_NAME,
} from './config';

import {
getTriggerPipelinesObjectKey,
Expand Down Expand Up @@ -109,6 +114,9 @@ export const getCicdTemplate = ({
VPC_PUBLIC_SUBNET_2: {
'Fn::ImportValue': BASE_STACK_VPC_PUBLIC_SUBNET_2_EXPORTED_NAME,
},
ECS_TASK_REPORT_HANDLER_NAME: {
Ref: ECS_TASK_REPORT_HANDLER_LAMBDA_FUNCTION_LOGICAL_ID,
},
};

/**
Expand Down Expand Up @@ -286,8 +294,6 @@ export const getCicdTemplate = ({
'RUN yarn config set cache-folder /home/yarn-cache',

'RUN yarn install',

'RUN git fetch --all',
].join('\n'),
},
},
Expand Down Expand Up @@ -908,10 +914,10 @@ export const getCicdTemplate = ({
Provider: 'Manual',
Version: 1,
},
Name: `Pipeline${pipelinePascalCase}RunECSTasksApproval`,
Name: PIPELINE_ECS_TASK_EXECUTION_MANUAL_APPROVAL_ACTION_NAME,
},
],
Name: `Pipeline${pipelinePascalCase}RunECSTasksStage`,
Name: PIPELINE_ECS_TASK_EXECUTION_STAGE_NAME,
},
],
},
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/deploy/cicd/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const ECS_TASK_DEFAULT_CPU = '1024';
export const ECS_TASK_DEFAULT_CPU = '2048';

export const ECS_TASK_DEFAULT_MEMORY = '2048';
export const ECS_TASK_DEFAULT_MEMORY = '4096';

export const PIPELINE_ECS_TASK_EXECUTION_STAGE_NAME = `PipelineRunECSTasksStage`;

export const PIPELINE_ECS_TASK_EXECUTION_MANUAL_APPROVAL_ACTION_NAME = `PipelineRunECSTasksApproval`;
4 changes: 4 additions & 0 deletions packages/cli/src/deploy/cicd/ecsTaskReportCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const sendEcsTaskReport = async ({ status }: { status: Status }) => {
payload.ecsTaskArn = process.env.ECS_TASK_ARN;
}

if (process.env.PIPELINE_JOB_ID) {
payload.pipelineJobId = process.env.PIPELINE_JOB_ID;
}

await lambda
.invokeAsync({
FunctionName: process.env.ECS_TASK_REPORT_HANDLER_NAME,
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/deploy/cicd/lambdas/executeTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const executeTasks = async ({
* https://stackoverflow.com/questions/2853803/how-to-echo-shell-commands-as-they-are-executed/2853811
*/
'set -x',
`ECS_TASK_ARN=$(curl -X GET "\${ECS_CONTAINER_METADATA_URI}/task" | jq ".TaskARN")`,
`export ECS_TASK_ARN=$(curl -X GET "\${ECS_CONTAINER_METADATA_URI}/task" | jq ".TaskARN")`,
...commands,
]);

Expand Down Expand Up @@ -90,6 +90,10 @@ export const executeTasks = async ({
name: 'ECS_ENABLE_CONTAINER_METADATA',
value: 'true',
},
{
name: 'ECS_TASK_REPORT_HANDLER_NAME',
value: getProcessEnvVariable('ECS_TASK_REPORT_HANDLER_NAME'),
},
...taskEnvironment,
],
},
Expand Down
129 changes: 64 additions & 65 deletions packages/cli/src/deploy/cicd/lambdas/pipelines.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { EventPayloadMap } from '@octokit/webhooks-types';
import AdmZip from 'adm-zip';
import { CodePipelineEvent, CodePipelineHandler } from 'aws-lambda';
import { CodePipeline, S3 } from 'aws-sdk';
Expand Down Expand Up @@ -69,79 +68,79 @@ const putJobFailureResult = (event: CodePipelineEvent, message: string) =>
})
.promise();

const executeMainPipeline = async ({
payload,
}: {
payload: EventPayloadMap['push'];
}) => {
const command = shConditionalCommands({
conditionalCommands: getMainCommands(),
});

await executeTasks({
commands: [command],
tags: [
{
key: 'Pipeline',
value: 'main',
},
{
key: 'AfterCommit',
value: payload.after,
},
],
});
};

const executeTagPipeline = async ({
payload,
}: {
payload: EventPayloadMap['push'];
}) => {
const tag = payload.ref.split('/')[2];

const command = shConditionalCommands({
conditionalCommands: getTagCommands({ tag }),
});

await executeTasks({
commands: [command],
tags: [
{
key: 'Pipeline',
value: 'tag',
},
{
key: 'Tag',
value: tag,
},
{
key: 'AfterCommit',
value: payload.after,
},
],
});
};

export const pipelinesHandler: CodePipelineHandler = async (event) => {
try {
const { pipeline } = getUserParameters(event);

const jobDetails = await getJobDetails(event);

if (pipeline === 'main') {
await executeMainPipeline(jobDetails);
await putJobSuccessResult(event);
return;
const jobId = event['CodePipeline.job'].id;

const executeTasksInput = (() => {
const tags = [
{
key: 'Pipeline',
value: pipeline,
},
{
key: 'AfterCommit',
value: jobDetails.after,
},
{
key: 'PipelineJobId',
value: jobId,
},
];

const taskEnvironment = [
{
name: 'PIPELINE_JOB_ID',
value: jobId,
},
];

if (pipeline === 'main') {
return {
commands: [
shConditionalCommands({
conditionalCommands: getMainCommands(),
}),
],
tags,
taskEnvironment,
};
}

if (pipeline === 'tag') {
const tag = jobDetails.ref.split('/')[2];

return {
commands: [
shConditionalCommands({
conditionalCommands: getTagCommands({ tag }),
}),
],
tags: [
...tags,
{
key: 'Tag',
value: tag,
},
],
taskEnvironment,
};
}

return undefined;
})();

if (!executeTasksInput) {
throw new Error('executeTasksInputUndefined');
}

if (pipeline === 'tag') {
await executeTagPipeline(jobDetails);
await putJobSuccessResult(event);
return;
}
await executeTasks(executeTasksInput);

throw new Error(`Pipeline ${pipeline} was not handled.`);
await putJobSuccessResult(event);
} catch (error) {
await putJobFailureResult(event, error.message.slice(0, 4999));
}
Expand Down

0 comments on commit a070754

Please sign in to comment.