Skip to content

Commit 7e84e00

Browse files
authored
Allow overriding the branch name used to look up configuration (#20)
This PR adds a new `config-name` action input value. It can be used to override the name used to look up the deployment configuration in the `branch_config` section of the appspec.yml file. By default, the current branch name (`master`, or a PR branch) will be used to find a deployment config, by matching the regular expressions from the `branch_config` section in `appspec.yml`. Overriding the value makes sense when running the action multiple times within the same job or workflow to create multiple deployments for a single push or PR.
1 parent 65409bf commit 7e84e00

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
with:
4949
token: ${{ secrets.GITHUB_TOKEN }}
5050
body: |
51-
@${{ github.actor }} this was deployed as [${{ steps.deploy.outputs.deploymentId }}](https://console.aws.amazon.com/codesuite/codedeploy/deployments/${{ steps.deploy.outputs.deploymentId }}?region=eu-central-1) to group `${{ steps.deploy.outputs.deploymentGroupName }}`.
51+
@${{ github.actor }} this was deployed as [${{ steps.deploy.outputs.deploymentId }}](https://console.aws.amazon.com/codesuite/codedeploy/deployments/${{ steps.deploy.outputs.deploymentId }}?region=eu-central-1) to group `${{ steps.deploy.outputs.deploymentGroupName }}`.
5252
```
5353
5454
First, this configures AWS Credentials in the GitHub Action runner. The [aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) action is used for that, and credentials are kept in [GitHub Actions Secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets).
@@ -165,6 +165,7 @@ This workaround should catch a good share of possible out-of-order deployments.
165165

166166
* `application`: The name of the CodeDeploy Application to work with. Defaults to the "short" repo name.
167167
* `skip-sequence-check`: When set to `true`, do not attempt to make sure deployments happen in order. Use this when the workflow count has been reset or changed to a lower value; possible cause is renaming the workflow file.
168+
* `config-name`: Name used to look up the deployment config in the `branch_config` section of the `appspec.yml` file. Defaults to the current branch name. By using this override, you can force a particular config to be used regardless of the branch name. Or, you can run the action several times within the same job to create multiple (different) deployments from the same branch.
168169

169170
### Outputs
170171

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ inputs:
66
skip-sequence-check:
77
description: 'When set, skip the check making sure no earlier workflow results are deployed'
88
default: false
9+
config-name:
10+
description: 'Override name to look up branch_config; default is to use the current branch name.'
11+
default: ''
912
outputs:
1013
deploymentId:
1114
description: AWS CodeDeployment Deployment-ID of the deployment created

cli.js

Lines changed: 1 addition & 1 deletion
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, commitId, null, null, core);
82+
await action.createDeployment(applicationName, fullRepositoryName, branchName, branchName, commitId, null, null, core);
8383
} catch (e) {
8484
console.log(`👉🏻 ${e.message}`);
8585
process.exit(1);

create-deployment.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
function fetchBranchConfig(branchName, core) {
3+
function fetchBranchConfig(configLookupName, core) {
44
const fs = require('fs');
55
const yaml = require('js-yaml');
66

@@ -18,22 +18,22 @@ function fetchBranchConfig(branchName, core) {
1818

1919
for (var prop in data.branch_config) {
2020
var regex = new RegExp('^' + prop + '$', 'i');
21-
if (branchName.match(regex)) {
21+
if (configLookupName.match(regex)) {
2222
if (data.branch_config[prop] == null) {
23-
console.log(`🤷🏻‍♂️ Found an empty appspec.yml -> branch_config for '${branchName}' – skipping deployment`);
23+
console.log(`🤷🏻‍♂️ Found an empty appspec.yml -> branch_config for '${configLookupName}' – skipping deployment`);
2424
process.exit();
2525
}
26-
console.log(`💡 Using appspec.yml -> branch_config '${prop}' for branch '${branchName}'`);
26+
console.log(`💡 Using appspec.yml -> branch_config '${prop}' for '${configLookupName}'`);
2727
return data.branch_config[prop];
2828
}
2929
}
3030

31-
console.log(`❓ Found no matching appspec.yml -> branch_config for '${branchName}' – skipping deployment`);
31+
console.log(`❓ Found no matching appspec.yml -> branch_config for '${configLookupName}' – skipping deployment`);
3232
process.exit();
3333
}
3434

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

dist/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = JSON.parse("{\"name\":\"@octokit/rest\",\"version\":\"16.43.2\"
1616
"use strict";
1717

1818

19-
function fetchBranchConfig(branchName, core) {
19+
function fetchBranchConfig(configLookupName, core) {
2020
const fs = __webpack_require__(5747);
2121
const yaml = __webpack_require__(1917);
2222

@@ -34,22 +34,22 @@ function fetchBranchConfig(branchName, core) {
3434

3535
for (var prop in data.branch_config) {
3636
var regex = new RegExp('^' + prop + '$', 'i');
37-
if (branchName.match(regex)) {
37+
if (configLookupName.match(regex)) {
3838
if (data.branch_config[prop] == null) {
39-
console.log(`🤷🏻‍♂️ Found an empty appspec.yml -> branch_config for '${branchName}' – skipping deployment`);
39+
console.log(`🤷🏻‍♂️ Found an empty appspec.yml -> branch_config for '${configLookupName}' – skipping deployment`);
4040
process.exit();
4141
}
42-
console.log(`💡 Using appspec.yml -> branch_config '${prop}' for branch '${branchName}'`);
42+
console.log(`💡 Using appspec.yml -> branch_config '${prop}' for '${configLookupName}'`);
4343
return data.branch_config[prop];
4444
}
4545
}
4646

47-
console.log(`❓ Found no matching appspec.yml -> branch_config for '${branchName}' – skipping deployment`);
47+
console.log(`❓ Found no matching appspec.yml -> branch_config for '${configLookupName}' – skipping deployment`);
4848
process.exit();
4949
}
5050

51-
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, commitId, runNumber, skipSequenceCheck, core) {
52-
const branchConfig = fetchBranchConfig(branchName, core);
51+
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core) {
52+
const branchConfig = fetchBranchConfig(configLookupName, core);
5353
const safeBranchName = branchName.replace(/[^a-z0-9-/]+/gi, '-').replace(/\/+/, '--');
5454
const deploymentGroupName = branchConfig.deploymentGroupName ? branchConfig.deploymentGroupName.replace('$BRANCH', safeBranchName) : safeBranchName;
5555
const deploymentGroupConfig = branchConfig.deploymentGroupConfig;
@@ -202,6 +202,7 @@ exports.createDeployment = async function(applicationName, fullRepositoryName, b
202202
const isPullRequest = payload.pull_request !== undefined;
203203
const commitId = isPullRequest ? payload.pull_request.head.sha : (payload.head_commit ? payload.head_commit.id : github.context.sha); // like "ec26c3e57ca3a959ca5aad62de7213c562f8c821"
204204
const branchName = isPullRequest ? payload.pull_request.head.ref : payload.ref.replace(/^refs\/heads\//, ''); // like "my/branch_name"
205+
const configLookupName = core.getInput('config-name') || branchName;
205206

206207
const skipSequenceCheck = core.getBooleanInput('skip-sequence-check');
207208

@@ -210,7 +211,7 @@ exports.createDeployment = async function(applicationName, fullRepositoryName, b
210211
const runNumber = process.env['github_run_number'] || process.env['GITHUB_RUN_NUMBER'];
211212

212213
try {
213-
action.createDeployment(applicationName, fullRepositoryName, branchName, commitId, runNumber, skipSequenceCheck, core);
214+
action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
214215
} catch (e) {}
215216
})();
216217

index.js

Lines changed: 2 additions & 1 deletion
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 configLookupName = core.getInput('config-name') || branchName;
1516

1617
const skipSequenceCheck = core.getBooleanInput('skip-sequence-check');
1718

@@ -20,6 +21,6 @@
2021
const runNumber = process.env['github_run_number'] || process.env['GITHUB_RUN_NUMBER'];
2122

2223
try {
23-
action.createDeployment(applicationName, fullRepositoryName, branchName, commitId, runNumber, skipSequenceCheck, core);
24+
action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
2425
} catch (e) {}
2526
})();

0 commit comments

Comments
 (0)