Skip to content

Commit 06c714d

Browse files
author
Hubert Ott
committed
M2C-6569: Revise pipeline to support Serverless v4
1 parent f1bcb60 commit 06c714d

6 files changed

Lines changed: 38 additions & 34 deletions

File tree

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@ Add the following your `bitbucket-pipelines.yml` file:
1919
variables:
2020
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
2121
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
22+
SERVERLESS_LICENSE_KEY: ${SERVERLESS_LICENSE_KEY}
2223
```
2324
2425
## Variables
2526
26-
| Variable | Usage |
27-
| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
28-
| AWS_ACCESS_KEY_ID | (Required) Injects AWS Access key |
29-
| AWS_SECRET_ACCESS_KEY | (Required) Injects AWS Secret key |
30-
| CFN_ROLE | [CloudFormation service role](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-servicerole.html) to use for deployment |
31-
| STAGE | Define the stage to deploy. Must be exact three characters. (Default: `stg`) |
32-
| DEBUG | Turn on extra debug information. (Accepted values: `true`/`false`) |
33-
| UPLOAD_BADGE | Whether or not to upload a deployment badge to the repositories downloads section. (Accepted values: `true`/`false`) |
34-
| APP_USERNAME | The user to upload the badge as. Required if UPLOAD_BADGE is set to `true`. |
35-
| APP_PASSWORD | The app password of the user uploading the badge. Required if UPLOAD_BADGE is set to `true`. |
36-
| TIMEZONE | Which time zone the time in the badge should use (Default: `Australia/Adelaide`) |
37-
| PROFILE | The profile name that is used for deployment (Default: `bitbucket-deployer`) |
38-
| CMD | The command that this pipe will run (Eg: `deploy`, `remove`. Default: `deploy`) |
39-
| SERVICES_PATH | The relative path from root folder to the folder where applications are defined (Default: `services`) |
27+
| Variable | Usage |
28+
| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
29+
| AWS_ACCESS_KEY_ID | (Required) AWS Access key |
30+
| AWS_SECRET_ACCESS_KEY | (Required) AWS Secret key |
31+
| SERVERLESS_LICENSE_KEY | (Required) Serverless Framework v4 license key |
32+
| CFN_ROLE | [CloudFormation service role](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-servicerole.html) to use for deployment |
33+
| STAGE | Define the stage to deploy. Must be exact three characters. (Default: `stg`) |
34+
| DEBUG | Turn on extra debug information. (Accepted values: `true`/`false`) |
35+
| UPLOAD_BADGE | Whether or not to upload a deployment badge to the repositories downloads section. (Accepted values: `true`/`false`) |
36+
| APP_USERNAME | The user to upload the badge as. Required if UPLOAD_BADGE is set to `true`. |
37+
| APP_PASSWORD | The app password of the user uploading the badge. Required if UPLOAD_BADGE is set to `true`. |
38+
| TIMEZONE | Which time zone the time in the badge should use (Default: `Australia/Adelaide`) |
39+
| CMD | The command that this pipe will run (Eg: `deploy`, `remove`. Default: `deploy`) |
40+
| SERVICES_PATH | The relative path from root folder to the folder where applications are defined (Default: `services`) |
4041

4142
- Default pipelines variables that are available for builds: https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/
4243
- Please check: https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/ for how to generate an app password.
@@ -117,7 +118,6 @@ docker build --build-arg="NODE_TAG=20" -t aligent/nx-pipe:20-alpine .
117118
docker run -it --memory=4g --memory-swap=4g --memory-swappiness=0 --cpus=4 --entrypoint=/bin/sh \
118119
-v $(pwd):/app/work --workdir=/app/work \
119120
-e BITBUCKET_CLONE_DIR=/app/work \
120-
-e PROFILE=bitbucket-deployer \
121121
-e STAGE=stg \
122122
-e AWS_ACCESS_KEY_ID=test-access-key-id \
123123
-e AWS_SECRET_ACCESS_KEY=test-secret-access-key \

bin/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,25 @@ async function main() {
2020
const {
2121
awsAccessKeyId,
2222
awsSecretAccessKey,
23+
serverlessLicenseKey,
2324
bitbucketCloneDir,
2425
cfnRole,
2526
cmd,
2627
debug,
27-
profile,
2828
servicesPath,
2929
stage,
3030
} = env;
3131

3232
if (!awsAccessKeyId || !awsSecretAccessKey) {
3333
throw new Error(
34-
'AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY not set'
34+
'AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be set'
3535
);
3636
}
3737

38+
if (!serverlessLicenseKey) {
39+
throw new Error('SERVERLESS_LICENSE_KEY must be set for Serverless Framework v4');
40+
}
41+
3842
// Setup SSH credentials for the pipeline, these are generated by Bitbucket Pipelines
3943
await setupSshCredentials();
4044

@@ -51,16 +55,14 @@ async function main() {
5155
serverlessFiles.map((file) => injectCfnRole(file, cfnRole))
5256
);
5357

54-
const commands = [
55-
`npx serverless config credentials --provider aws --profile ${profile} --key ${awsAccessKeyId} --secret ${awsSecretAccessKey}`,
56-
];
58+
const commands: string[] = [];
5759

5860
const isNodeModulesExists = await nodeModulesDirectoryExist(
5961
bitbucketCloneDir
6062
);
6163
if (!isNodeModulesExists) {
6264
const installCmd = getInstallCommand(packageManager);
63-
commands.unshift(installCmd);
65+
commands.push(installCmd);
6466
}
6567

6668
const baseCommand = isMonorepo
@@ -69,7 +71,7 @@ async function main() {
6971
const verbose = debug ? '--verbose' : '';
7072

7173
commands.push(
72-
`${baseCommand} --stage ${stage} --aws-profile ${profile} ${verbose}`
74+
`${baseCommand} --stage ${stage} ${verbose}`
7375
);
7476

7577
await runCLICommand(commands, bitbucketCloneDir);

lib/env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
interface Env {
22
debug: boolean;
33
stage: string;
4-
profile: string;
54
cmd: string;
65
awsAccessKeyId?: string;
76
awsSecretAccessKey?: string;
7+
serverlessLicenseKey?: string;
88
cfnRole?: string;
99
uploadBadge: boolean;
1010
appUsername?: string;
@@ -20,10 +20,10 @@ interface Env {
2020
export const env: Env = {
2121
debug: process.env.DEBUG === 'true',
2222
stage: process.env.STAGE || 'stg',
23-
profile: process.env.PROFILE || 'bitbucket-deployer',
2423
cmd: process.env.CMD || 'deploy',
2524
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
2625
awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
26+
serverlessLicenseKey: process.env.SERVERLESS_LICENSE_KEY,
2727
cfnRole: process.env.CFN_ROLE,
2828
uploadBadge: process.env.UPLOAD_BADGE === 'true',
2929
appUsername: process.env.APP_USERNAME,

lib/injectCfnRole.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
import type { AWS } from '@serverless/typescript';
21
import chalk from 'chalk';
32
import { readFile, writeFile } from 'fs/promises';
43
import { dump, load } from 'js-yaml';
54
import { CLOUDFORMATION_SCHEMA } from 'js-yaml-cloudformation-schema';
65
import logSymbols from 'log-symbols';
76
import { env } from './env';
87

8+
interface ServerlessConfig {
9+
provider: {
10+
cfnRole?: string;
11+
iam?: {
12+
deploymentRole?: string;
13+
};
14+
[key: string]: unknown;
15+
};
16+
[key: string]: unknown;
17+
}
18+
919
export async function injectCfnRole(
1020
serverlessYamlPath: string,
1121
cfnRole: string | undefined
@@ -14,7 +24,7 @@ export async function injectCfnRole(
1424
// Parse yaml file as a JSON object, while extending the yaml schema with
1525
// AWS Intrinsic functions (!Sub, !Ref etc) as custom tags
1626
const yaml = await readFile(serverlessYamlPath, 'utf8');
17-
const serverless = load(yaml, { schema: CLOUDFORMATION_SCHEMA }) as AWS;
27+
const serverless = load(yaml, { schema: CLOUDFORMATION_SCHEMA }) as ServerlessConfig;
1828

1929
if (env.debug) {
2030
console.log(

package-lock.json

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"yaml": "^2.3.2"
2222
},
2323
"devDependencies": {
24-
"@serverless/typescript": "^3.30.1",
2524
"@types/js-yaml": "^4.0.6",
2625
"@types/node": "^20.6.2",
2726
"tsx": "^4.19.1",

0 commit comments

Comments
 (0)