Skip to content

Instantly share code, notes, and snippets.

@andreybleme
Last active November 28, 2020 19:59
Show Gist options
  • Save andreybleme/2a376e9211191c837462154e5296b226 to your computer and use it in GitHub Desktop.
Save andreybleme/2a376e9211191c837462154e5296b226 to your computer and use it in GitHub Desktop.
andreybleme.com | Running Cypress tests in parallel at AWS Codepipeline CI
AWSTemplateFormatVersion: 2010-09-09
Description: >
This stack creates a codepipeline for running the end-to-end tests
Last Modified: 08.10.2020
Author: Lucas Bleme
Parameters:
Owner:
Description: Enter Team or Individual Name Responsible for the Stack.
Type: String
Default: Lucas Bleme
Project:
Description: Enter Project Name
Type: String
Default: myProject
Subproject:
Description: Enter Project Name.
Type: String
Default: mySubProject
GitHubAccessToken:
Description: A token to perform actions into the Github's API
Type : 'AWS::SSM::Parameter::Value<String>'
Default: GIT_ACCESS_TOKEN
CypressSecretAccessKey:
Description: A token to perform actions into AWS through Cypress
Type : 'AWS::SSM::Parameter::Value<String>'
Default: CYPRESS_SECRET_ACCESS_KEY
CypressAccessKeyId:
Description: A token to perform actions into AWS through Cypress
Type : 'AWS::SSM::Parameter::Value<String>'
Default: CYPRESS_ACCESS_KEY_ID
CypressRecordKey:
Description: The Cypress dashboard record key
Type : 'AWS::SSM::Parameter::Value<String>'
Default: CYPRESS_RECORD_KEY
GitRepositoryRoot:
Description: GitHub source repository for Codepipeline Source Stage
Type: String
Default: andreyblemerepo
GitHubBranch:
Description: CodeCommit or GitHub git repository branch - change triggers a new build
Type: String
Default: dev
GitHubUser:
Type: String
Description: GitHub username or organization
Default: andreybleme
CodeBuildImage:
Type: String
Default: aws/codebuild/standard:4.0
AppName:
Description: App name for our website (example). Only alphanumeric characters,
dash, and underscore are supported.
Type: String
Default: baseapp
Resources:
E2EGroup1:
Type: AWS::CodeBuild::Project
Properties:
Description: This project runs the e2e tests
Name:
Fn::Join:
- _
- - Ref: AppName
- E2EGroup1
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM
Image: !Ref CodeBuildImage
Type: LINUX_CONTAINER
PrivilegedMode: true
EnvironmentVariables:
- Name: CYPRESS_SECRET_ACCESS_KEY
Value: !Ref CypressSecretAccessKey
- Name: CYPRESS_ACCESS_KEY_ID
Value: !Ref CypressAccessKeyId
- Name: CYPRESS_RECORD_KEY
Value: !Ref CypressRecordKey
LogsConfig:
CloudWatchLogs:
Status: ENABLED
StreamName:
Fn::ImportValue: !Sub ${EnvironmentName}-CodebuildLogstreamName
S3Logs:
Status: ENABLED
Location: !Join
- ""
- - Fn::ImportValue: !Sub ${EnvironmentName}-CodebuildBucket-Name
- !Sub /${StageName}
- /build/e2e/
- !Ref AppName
ServiceRole:
Fn::ImportValue: !Sub ${EnvironmentName}-CodeBuildRoleARN
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
install:
runtime-versions:
nodejs: 12
commands:
- echo ">>> global installing dependencies"
- npm install -g typescript lerna
pre_build:
commands:
- echo ">>> Authenticating CI at Github private NPM registry"
- bash ci_npm_auth.sh
- export CYPRESS_SECRET_ACCESS_KEY=${CYPRESS_SECRET_ACCESS_KEY}
- export CYPRESS_ACCESS_KEY_ID=${CYPRESS_ACCESS_KEY_ID}
- echo ">>> Compiling typescript files"
- lerna bootstrap --hoist
- echo ">>> Installing frontend dependencies"
- cd frontend
- rm -fr node_modules && npm install
- npm install --save wait-on
- echo ">>> Starting frontend application"
- npm run serve & ./node_modules/.bin/wait-on http://localhost:8080
- echo ">>> Running cypress tests"
- export BUILD_TAG=${CODEBUILD_SOURCE_VERSION}
- ./node_modules/.bin/cypress run --record --key=${CYPRESS_RECORD_KEY} --parallel --browser electron --group electron --ci-build-id $BUILD_TAG --headless --tag "nightly"
build:
commands:
- echo ">>> Nothing to do at BUILD phase"
post_build:
commands:
- echo ">>> Nothing to do at POST_BUILD phase"
TimeoutInMinutes: 20
Cache:
Location: !Join
- ""
- - Fn::ImportValue: !Sub ${EnvironmentName}-CodebuildBucket-Name
- !Sub /${StageName}
- /build/e2e/cache/
- !Ref AppName
Type: S3
Tags:
- Key: Name
Value: !Sub Codebuild-${Project}-${StageName}
E2EGroup2:
Type: AWS::CodeBuild::Project
Properties:
Description: This project runs the e2e tests
Name:
Fn::Join:
- _
- - Ref: AppName
- E2EGroup2
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM
Image: !Ref CodeBuildImage
Type: LINUX_CONTAINER
PrivilegedMode: true
EnvironmentVariables:
- Name: CYPRESS_SECRET_ACCESS_KEY
Value: !Ref CypressSecretAccessKey
- Name: CYPRESS_ACCESS_KEY_ID
Value: !Ref CypressAccessKeyId
- Name: CYPRESS_RECORD_KEY
Value: !Ref CypressRecordKey
LogsConfig:
CloudWatchLogs:
Status: ENABLED
StreamName:
Fn::ImportValue: !Sub ${EnvironmentName}-CodebuildLogstreamName
S3Logs:
Status: ENABLED
Location: !Join
- ""
- - Fn::ImportValue: !Sub ${EnvironmentName}-CodebuildBucket-Name
- !Sub /${StageName}
- /build/e2e/
- !Ref AppName
ServiceRole:
Fn::ImportValue: !Sub ${EnvironmentName}-CodeBuildRoleARN
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
install:
runtime-versions:
nodejs: 12
commands:
- echo ">>> global installing dependencies"
- npm install -g typescript lerna
pre_build:
commands:
- echo ">>> Authenticating CI at Github private NPM registry"
- bash ci_npm_auth.sh
- export CYPRESS_SECRET_ACCESS_KEY=${CYPRESS_SECRET_ACCESS_KEY}
- export CYPRESS_ACCESS_KEY_ID=${CYPRESS_ACCESS_KEY_ID}
- echo ">>> Compiling typescript files"
- lerna bootstrap --hoist
- echo ">>> Installing frontend dependencies"
- cd frontend
- rm -fr node_modules && npm install
- npm install --save wait-on
- echo ">>> Starting frontend application"
- npm run serve & ./node_modules/.bin/wait-on http://localhost:8080
- echo ">>> Running cypress tests"
- export BUILD_TAG=${CODEBUILD_SOURCE_VERSION}
- ./node_modules/.bin/cypress run --record --key=${CYPRESS_RECORD_KEY} --parallel --browser electron --group electron --ci-build-id $BUILD_TAG --headless --tag "nightly"
build:
commands:
- echo ">>> Nothing to do at BUILD phase"
post_build:
commands:
- echo ">>> Nothing to do at POST_BUILD phase"
TimeoutInMinutes: 20
Cache:
Location: !Join
- ""
- - Fn::ImportValue: !Sub ${EnvironmentName}-CodebuildBucket-Name
- !Sub /${StageName}
- /build/e2e/cache/
- !Ref AppName
Type: S3
Tags:
- Key: Name
Value: !Sub Codebuild-${Project}-${StageName}
CodePipelineEndToEndTestingRoot:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: e2e-Test-pipeline
ArtifactStore:
Type: S3
Location:
Fn::ImportValue: !Sub ${EnvironmentName}-${Project}-DevArtifactsBucketName
EncryptionKey:
Id:
Fn::ImportValue: !Sub ${EnvironmentName}-CustomKMSKey-Arn
Type: KMS
RestartExecutionOnUpdate: true
RoleArn:
Fn::ImportValue:
!Sub ${EnvironmentName}-CodePipelineRoleARN
# Source Stage Definition
Stages:
- Name: Source
Actions:
- Name: SourceAction
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
InputArtifacts: []
OutputArtifacts:
- Name: RepoCode
Configuration:
Owner: !Ref GitHubUser
Repo: !Ref GitRepositoryRoot
Branch: !Ref GitHubBranch
OAuthToken: !Ref GitHubAccessToken
PollForSourceChanges: false
RunOrder: 1
- Name: EndToEndTests
Actions:
- Name: E2EGroup1
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
InputArtifacts:
- Name: RepoCode
OutputArtifacts: []
Configuration:
ProjectName: !Ref E2EGroup1
RunOrder: 1
- Name: E2EGroup2
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
InputArtifacts:
- Name: RepoCode
OutputArtifacts: []
Configuration:
ProjectName: !Ref E2EGroup2
RunOrder: 1
Outputs:
StackName:
Description: returns the Stack Name
Value: !Ref AWS::StackName
Owner:
Description: Team or Individual that Owns this Formation.
Value: !Ref Owner
Project:
Description: The project name
Value: !Ref Project
Subproject:
Description: The project name
Value: !Ref Subproject
EntToEndCodepipelineName:
Description: returns the name of the Codepipeline
Value: !Ref CodePipelineEndToEndTestingRoot
Export:
Name: !Sub ${EnvironmentName}-${Project}-EntToEndCodepipelineName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment