This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from zxkane/pipeline
ci/cd pipeline support
- Loading branch information
Showing
20 changed files
with
3,694 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: CD pipeline | ||
|
||
on: | ||
# Trigger the workflow on push for the master branch | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
trigger-job: | ||
name: Pipeline Trigger | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Trigger pipeline on AWS | ||
id: trigger-pipeline | ||
env: | ||
COMMIT: ${{ github.sha }} | ||
TRIGGER_URL: ${{ secrets.PIPELINE_TRIGGER_URL }} | ||
run: | | ||
# Trigger pipeline state machine | ||
if [ $(curl -LI -s -o /dev/null -w '%{http_code}\n' -X PUT $TRIGGER_URL?commit=$COMMIT) != "200" ]; then exit 1; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env node | ||
import 'source-map-support/register'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
|
||
const app = new cdk.App(); | ||
|
||
const appPrefix = app.node.tryGetContext('stackPrefix') || 'OpenTuna'; | ||
const env = { | ||
region: process.env.CDK_DEFAULT_REGION, | ||
account: process.env.CDK_DEFAULT_ACCOUNT, | ||
}; | ||
|
||
const suffix = app.node.tryGetContext('stackSuffix') || ''; | ||
|
||
const trustedAccount = app.node.tryGetContext('trustedAccount'); | ||
if (!trustedAccount) { | ||
throw new Error(`Pls specify the trusted account for pipeline deployment via context 'trustedAccount'.`); | ||
} | ||
|
||
const stack = new cdk.Stack(app, `PipelineCrossAccountDeploymentSetupStack`, { | ||
env, | ||
}); | ||
|
||
// the role to assume when the CDK is in write mode, i.e. deploy | ||
// allow roles from the trusted account to assume this role | ||
const openTunaDeployRole = new iam.Role(stack, 'DeployRole', { | ||
assumedBy: new iam.AccountPrincipal(trustedAccount), | ||
roleName: `opentuna-deployment-trust-${trustedAccount}-role`, | ||
}); | ||
|
||
// Attach the AdministratorAccess policy to this role. | ||
openTunaDeployRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess')); | ||
|
||
cdk.Tags.of(app).add('app', `${appPrefix}${suffix}`); | ||
|
||
new cdk.CfnOutput(stack, `DeployRoleFor${trustedAccount}`, { | ||
value: `${openTunaDeployRole.roleArn}`, | ||
description: `Deployment role for trusted account ${trustedAccount}.` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env node | ||
import 'source-map-support/register'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { PipelineStack, Stage } from '../lib/pipeline-stack'; | ||
import { CommonStack } from '../lib/common-stack'; | ||
|
||
const app = new cdk.App(); | ||
|
||
const appPrefix = app.node.tryGetContext('stackPrefix') || 'OpenTuna'; | ||
const env = { | ||
region: process.env.CDK_DEFAULT_REGION, | ||
account: process.env.CDK_DEFAULT_ACCOUNT, | ||
}; | ||
|
||
const suffix = app.node.tryGetContext('stackSuffix') || ''; | ||
|
||
const commonStack = new CommonStack(app, `${appPrefix}CommonStack${suffix}`, { | ||
env, | ||
}); | ||
|
||
// workaround for CDK StringParameter.valueFromLookup | ||
// see https://github.com/aws/aws-cdk/issues/8699 for detail | ||
const uatJsonFile = app.node.tryGetContext('UATConf') || `../cdk.out/uat.json`; | ||
const uat: Stage = JSON.parse(fs.readFileSync(path.join(__dirname, uatJsonFile), 'utf-8')); | ||
const prodJsonFile = app.node.tryGetContext('ProdConf') || `../cdk.out/prod.json`; | ||
const prod: Stage = JSON.parse(fs.readFileSync(path.join(__dirname, prodJsonFile), 'utf-8')); | ||
|
||
new PipelineStack(app, `${appPrefix}PipelineStack${suffix}`, { | ||
env, | ||
topic: commonStack.notifyTopic, | ||
uat, | ||
prod, | ||
}); | ||
|
||
cdk.Tags.of(app).add('app', `${appPrefix}${suffix}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { APIGatewayProxyHandlerV2 } from 'aws-lambda'; | ||
import * as aws from 'aws-sdk'; | ||
|
||
const stepfunctions = new aws.StepFunctions(); | ||
|
||
export const pipelineApprovalAction: APIGatewayProxyHandlerV2 = async (event, _context, callback) => { | ||
console.info(`Receiving pipeline approval action event ${JSON.stringify(event, null, 2)}.`); | ||
|
||
// workaround to disable slack link auto preview | ||
// https://slack.com/help/articles/204399343-Share-links-and-set-preview-preferences | ||
if (event.requestContext.http.userAgent.indexOf('Slackbot') > -1) { | ||
return { | ||
statusCode: 401, | ||
body: `This url does not support Slack LinkExpanding.`, | ||
}; | ||
} | ||
|
||
var message = {}; | ||
|
||
const action = event.queryStringParameters?.action; | ||
|
||
if (action === "approve") { | ||
message = { "Status": "Approved" }; | ||
} else if (action === "reject") { | ||
message = { "Status": "Rejected" }; | ||
} else { | ||
console.error(`Unrecognized action "${action}". Expected: approve, reject.`); | ||
return { | ||
statusCode: 400, | ||
body: `Failed to process the request. Unrecognized Action "${action}".`, | ||
}; | ||
} | ||
|
||
const taskToken = event.queryStringParameters!.taskToken; | ||
const statemachineName = event.queryStringParameters!.sm; | ||
const executionName = event.queryStringParameters!.ex; | ||
|
||
try { | ||
await stepfunctions.sendTaskSuccess({ | ||
output: JSON.stringify(message), | ||
taskToken: taskToken, | ||
}).promise(); | ||
} catch (err) { | ||
console.error(err, err.stack); | ||
return { | ||
statusCode: 500, | ||
body: err.message, | ||
} | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: `Deployment pipeline "${statemachineName}" with execution "${executionName}" is ${action === 'approve' ? 'approved' : 'rejected'}.`, | ||
}; | ||
} |
Oops, something went wrong.