Created
February 16, 2023 15:55
-
-
Save bfrancom/674dd0cdad8b912e9730d4462cb13103 to your computer and use it in GitHub Desktop.
CDK Pipeline with GitHub, Artifactory, and Tests
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
import * as cdk from 'aws-cdk-lib'; | |
//import * as codecommit from 'aws-cdk-lib/aws-codecommit'; | |
import { Construct } from 'constructs'; | |
import { BenzPipelineStage } from './pipeline-stage'; | |
import { CodeBuildStep, CodePipeline, CodePipelineSource } from 'aws-cdk-lib/pipelines'; | |
import { aws_codebuild as codebuild } from 'aws-cdk-lib'; | |
export class BenzPipelineStack extends cdk.Stack { | |
constructor(scope: Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
// Set your Github username and repository name | |
const branch = 'main'; | |
const repo = 'bfrancom/cdk-ar-pl'; | |
// The basic pipeline declaration. This sets the initial structure | |
// of our pipeline | |
const pipeline = new CodePipeline(this, 'Pipeline', { | |
pipelineName: 'BenzPipeline', | |
synth: new CodeBuildStep('SynthStep', { | |
//call github to get code | |
input: CodePipelineSource.gitHub(repo, branch, { | |
//generate a PAT in GitHub with correct rights and add the secret in secrets manager | |
authentication: cdk.SecretValue.secretsManager('github-token') | |
}), | |
//Add NPM_TOKEN secret in secrets manager with your Artifactory token | |
buildEnvironment: { | |
environmentVariables: { | |
NPM_TOKEN: { | |
value: 'NPM_TOKEN', | |
type: codebuild.BuildEnvironmentVariableType.SECRETS_MANAGER, | |
}, | |
}, | |
}, | |
installCommands: [ | |
'npm i -g aws-cdk', | |
//'cat package-lock.json', | |
'npm i -g npm@latest' | |
], | |
commands: [ | |
'npm -v', | |
'npm config set registry https://artifactory.yourendpoint.com/api/npm/npmjs/', | |
'npm config get registry', | |
//This command puts NPM_TOKEN into the .npmrc | |
'echo "//artifactory.yourendpoint.com/api/npm/npmjs:_authToken=\\${NPM_TOKEN}" >> .npmrc', | |
'npm ci', | |
'npm run build', | |
'npx cdk synth' | |
] | |
} | |
) | |
}); | |
// Pipeline code goes here | |
const deploy = new BenzPipelineStage(this, 'Deploy'); | |
const deployStage = pipeline.addStage(deploy); | |
deployStage.addPre( | |
deploy. | |
) | |
deployStage.addPost( | |
new CodeBuildStep('TestViewerEndpoint', { | |
projectName: 'TestViewerEndpoint', | |
envFromCfnOutputs: { | |
ENDPOINT_URL: deploy.hcEndpoint | |
}, | |
commands: [ | |
'echo "Curling endpoint URL: https://https://$ENDPOINT_URL"', | |
'curl -Ssf https://$ENDPOINT_URL' | |
] | |
}) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment