Skip to content

Commit de07259

Browse files
authored
Add a $PR_NUMBER placeholder for deployment group names (#26)
This adds a second placeholder `$PR_NUMBER` (to the already existing `$BRANCH`) that can be used to derive deployment group names based on Pull Request IDs.
1 parent df8aebb commit de07259

File tree

6 files changed

+12
-8
lines changed

6 files changed

+12
-8
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ The first entry makes the action skip the deployment (do nothing at all) when th
9898

9999
Commits on the `master` branch are to be deployed in a Deployment Group called `production`. All other commits will create or update a Deployment Group named `$BRANCH.staging.acme.tld`, where `$BRANCH` will be replaced with a DNS-safe name derived from the current branch. Basically, a branch called `feat/123/new_gimmick` will use `feat-123-new-gimmick` for `$BRANCH`. Since the Deployment Group Name is available in the `$DEPLOYMENT_GROUP_NAME` environment variable inside your CodeDeploy Lifecycle Scripts, you can use that to create "staging" environments with a single, generic configuration statement.
100100

101+
Similar to `$BRANCH`, for workflows triggered by Pull Requests, the string `$PR_NUMBER` will be replaced by the pull request number.
102+
101103
The `deploymentGroupConfig` and `deploymentConfig` keys in each of the two cases contain configuration that is passed as-is to the
102104
[`CreateDeploymentGroup`](https://docs.aws.amazon.com/codedeploy/latest/APIReference/API_CreateDeploymentGroup.html) or
103105
[`UpdateDeploymentGroup`](https://docs.aws.amazon.com/codedeploy/latest/APIReference/API_UpdateDeploymentGroup.html) API calls (for

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ outputs:
1717
deploymentGroupCreated:
1818
description: True, if a new deployment group was created; false if an already existing group was used.
1919
runs:
20-
using: 'node12'
20+
using: 'node16'
2121
main: 'dist/index.js'
2222

2323
branding:

cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979

8080
const action = require('./create-deployment');
8181
try {
82-
await action.createDeployment(applicationName, fullRepositoryName, branchName, branchName, commitId, null, null, core);
82+
await action.createDeployment(applicationName, fullRepositoryName, branchName, null, branchName, commitId, null, null, core);
8383
} catch (e) {
8484
console.log(`👉🏻 ${e.message}`);
8585
process.exit(1);

create-deployment.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ function fetchBranchConfig(configLookupName, core) {
3232
process.exit();
3333
}
3434

35-
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core) {
35+
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, pullRequestNumber, configLookupName, commitId, runNumber, skipSequenceCheck, core) {
3636
const branchConfig = fetchBranchConfig(configLookupName, core);
3737
const safeBranchName = branchName.replace(/[^a-z0-9-/]+/gi, '-').replace(/\/+/, '--');
38-
const deploymentGroupName = branchConfig.deploymentGroupName ? branchConfig.deploymentGroupName.replace('$BRANCH', safeBranchName) : safeBranchName;
38+
const deploymentGroupName = (branchConfig.deploymentGroupName ?? safeBranchName).replace('$BRANCH', safeBranchName).replace('$PR_NUMBER', pullRequestNumber);
3939
const deploymentGroupConfig = branchConfig.deploymentGroupConfig;
4040
const deploymentConfig = branchConfig.deploymentConfig;
4141

dist/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ function fetchBranchConfig(configLookupName, core) {
4848
process.exit();
4949
}
5050

51-
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core) {
51+
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, pullRequestNumber, configLookupName, commitId, runNumber, skipSequenceCheck, core) {
5252
const branchConfig = fetchBranchConfig(configLookupName, core);
5353
const safeBranchName = branchName.replace(/[^a-z0-9-/]+/gi, '-').replace(/\/+/, '--');
54-
const deploymentGroupName = branchConfig.deploymentGroupName ? branchConfig.deploymentGroupName.replace('$BRANCH', safeBranchName) : safeBranchName;
54+
const deploymentGroupName = (branchConfig.deploymentGroupName ?? safeBranchName).replace('$BRANCH', safeBranchName).replace('$PR_NUMBER', pullRequestNumber);
5555
const deploymentGroupConfig = branchConfig.deploymentGroupConfig;
5656
const deploymentConfig = branchConfig.deploymentConfig;
5757

@@ -210,6 +210,7 @@ exports.createDeployment = async function(applicationName, fullRepositoryName, b
210210
const isPullRequest = payload.pull_request !== undefined;
211211
const commitId = isPullRequest ? payload.pull_request.head.sha : (payload.head_commit ? payload.head_commit.id : github.context.sha); // like "ec26c3e57ca3a959ca5aad62de7213c562f8c821"
212212
const branchName = isPullRequest ? payload.pull_request.head.ref : payload.ref.replace(/^refs\/heads\//, ''); // like "my/branch_name"
213+
const pullRequestNumber = isPullRequest ? payload.pull_request.number : undefined;
213214
const configLookupName = core.getInput('config-name') || branchName;
214215

215216
const skipSequenceCheck = core.getBooleanInput('skip-sequence-check');
@@ -219,7 +220,7 @@ exports.createDeployment = async function(applicationName, fullRepositoryName, b
219220
const runNumber = process.env['github_run_number'] || process.env['GITHUB_RUN_NUMBER'];
220221

221222
try {
222-
await action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
223+
await action.createDeployment(applicationName, fullRepositoryName, branchName, pullRequestNumber, configLookupName, commitId, runNumber, skipSequenceCheck, core);
223224
} catch (e) {
224225
console.log(`👉🏻 ${e.message}`);
225226
process.exit(1);

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
const isPullRequest = payload.pull_request !== undefined;
1313
const commitId = isPullRequest ? payload.pull_request.head.sha : (payload.head_commit ? payload.head_commit.id : github.context.sha); // like "ec26c3e57ca3a959ca5aad62de7213c562f8c821"
1414
const branchName = isPullRequest ? payload.pull_request.head.ref : payload.ref.replace(/^refs\/heads\//, ''); // like "my/branch_name"
15+
const pullRequestNumber = isPullRequest ? payload.pull_request.number : undefined;
1516
const configLookupName = core.getInput('config-name') || branchName;
1617

1718
const skipSequenceCheck = core.getBooleanInput('skip-sequence-check');
@@ -21,7 +22,7 @@
2122
const runNumber = process.env['github_run_number'] || process.env['GITHUB_RUN_NUMBER'];
2223

2324
try {
24-
await action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
25+
await action.createDeployment(applicationName, fullRepositoryName, branchName, pullRequestNumber, configLookupName, commitId, runNumber, skipSequenceCheck, core);
2526
} catch (e) {
2627
console.log(`👉🏻 ${e.message}`);
2728
process.exit(1);

0 commit comments

Comments
 (0)