Skip to content

Commit

Permalink
Add a $PR_NUMBER placeholder for deployment group names (#26)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mpdude authored Oct 25, 2022
1 parent df8aebb commit de07259
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ The first entry makes the action skip the deployment (do nothing at all) when th

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.

Similar to `$BRANCH`, for workflows triggered by Pull Requests, the string `$PR_NUMBER` will be replaced by the pull request number.

The `deploymentGroupConfig` and `deploymentConfig` keys in each of the two cases contain configuration that is passed as-is to the
[`CreateDeploymentGroup`](https://docs.aws.amazon.com/codedeploy/latest/APIReference/API_CreateDeploymentGroup.html) or
[`UpdateDeploymentGroup`](https://docs.aws.amazon.com/codedeploy/latest/APIReference/API_UpdateDeploymentGroup.html) API calls (for
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ outputs:
deploymentGroupCreated:
description: True, if a new deployment group was created; false if an already existing group was used.
runs:
using: 'node12'
using: 'node16'
main: 'dist/index.js'

branding:
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

const action = require('./create-deployment');
try {
await action.createDeployment(applicationName, fullRepositoryName, branchName, branchName, commitId, null, null, core);
await action.createDeployment(applicationName, fullRepositoryName, branchName, null, branchName, commitId, null, null, core);
} catch (e) {
console.log(`👉🏻 ${e.message}`);
process.exit(1);
Expand Down
4 changes: 2 additions & 2 deletions create-deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ function fetchBranchConfig(configLookupName, core) {
process.exit();
}

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

Expand Down
7 changes: 4 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ function fetchBranchConfig(configLookupName, core) {
process.exit();
}

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

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

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

try {
await action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
await action.createDeployment(applicationName, fullRepositoryName, branchName, pullRequestNumber, configLookupName, commitId, runNumber, skipSequenceCheck, core);
} catch (e) {
console.log(`👉🏻 ${e.message}`);
process.exit(1);
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const isPullRequest = payload.pull_request !== undefined;
const commitId = isPullRequest ? payload.pull_request.head.sha : (payload.head_commit ? payload.head_commit.id : github.context.sha); // like "ec26c3e57ca3a959ca5aad62de7213c562f8c821"
const branchName = isPullRequest ? payload.pull_request.head.ref : payload.ref.replace(/^refs\/heads\//, ''); // like "my/branch_name"
const pullRequestNumber = isPullRequest ? payload.pull_request.number : undefined;
const configLookupName = core.getInput('config-name') || branchName;

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

try {
await action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
await action.createDeployment(applicationName, fullRepositoryName, branchName, pullRequestNumber, configLookupName, commitId, runNumber, skipSequenceCheck, core);
} catch (e) {
console.log(`👉🏻 ${e.message}`);
process.exit(1);
Expand Down

0 comments on commit de07259

Please sign in to comment.