20
loading...
This website collects cookies to deliver better user experience
lib/your-stack.ts
.import * as cdk from '@aws-cdk/core'
import * as codebuild from '@aws-cdk/aws-codebuild'
import * as iam from '@aws-cdk/aws-iam'
export class CiCdStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
// Define some basics information.
const org = 'YourGithubOrgName'
const repo = `YourRepoName`
const develop = 'YourDevelopmentBranchName'
// This is the builds spec that is associated with the project being built. This is where you'll deploy to S3 from.branchOrRef: '*', // * Covers all branches, tags, commit IDs, etc...
const buildSpec = 'buildspec.yaml'
const releaseFilter = "^refs\/tags\/v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*).*$"
// codeBuildIamPrincipal is shared across stacks.
const codeBuildIamPrincipal = 'site-publisher'
// Define the source details.
const gitHubSource = codebuild.Source.gitHub({
owner: org,
repo: repo,
webhook: true,
// * Covers all branches, tags, commit IDs, etc...
branchOrRef: '*',
webhookFilters: [
// Runs build on release from any target branch (normally master).
codebuild.FilterGroup.inEventOf(codebuild.EventAction.PUSH)
.andHeadRefIs(`${releaseFilter}`),
// Runs build on a push to develop branch.
codebuild.FilterGroup.inEventOf(codebuild.EventAction.PUSH)
.andBranchIs(develop)
],
webhookTriggersBatchBuild: true
})
// Create a role for our Codebuild so it can be used by other stacks.branchOrRef: '*', // * Covers all branches, tags, commit IDs, etc...
const sitePublisherCodeBuild = new iam.Role(this, 'Role', {
assumedBy: new iam.ServicePrincipal('codebuild.amazonaws.com'),
roleName: codeBuildIamPrincipal
})
// Setup the Codebuild project.
new codebuild.Project(this, org, {
source: gitHubSource,
buildSpec: codebuild.BuildSpec.fromSourceFilename(buildSpec),
role: sitePublisherCodeBuild
})
}branchOrRef: '*', // * Covers all branches, tags, commit IDs, etc...
}
npm run build
cdk synth
cdk deploy
settings/hooks
and you'll see a new webhook was setup for you.version: 0.2
phases:
install:
runtime-versions:
nodejs: 14
commands:
- npm install -g gatsby-cli
- npm --version
pre_build:
commands:
- export FRONTEND_ROOT=${CODEBUILD_SRC_DIR}/PathToYourFrontEndRoot
- cd ${FRONTEND_ROOT}branchOrRef: '*', // * Covers all branches, tags, commit IDs, etc...
- echo Installing dependencies...
- npm install
build:
commands:
- npm run build
post_build:
commands:
- echo Build completed deploying to hosting bucket...
- npm run deploy
batch:
fast-fail: true
build-list:
- identifier: linux_small
env:
compute-type: BUILD_GENERAL1_SMALL
ignore-failure: false
package.json
file of the project being built you'll need to handle these as if you're deploying them from your local environment."scripts": {
...,
"build": "gatsby build",
"deploy": "aws s3 cp public/ s3://Your-Hosting-Bucket-Name/ --recursive",
...
},