Skip to content

Allow overriding the branch name used to look up configuration #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
body: |
@${{ 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 }}`.
@${{ 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 }}`.
```

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).
Expand Down Expand Up @@ -165,6 +165,7 @@ This workaround should catch a good share of possible out-of-order deployments.

* `application`: The name of the CodeDeploy Application to work with. Defaults to the "short" repo name.
* `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.
* `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.

### Outputs

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ inputs:
skip-sequence-check:
description: 'When set, skip the check making sure no earlier workflow results are deployed'
default: false
config-name:
description: 'Override name to look up branch_config; default is to use the current branch name.'
default: ''
outputs:
deploymentId:
description: AWS CodeDeployment Deployment-ID of the deployment created
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, commitId, null, null, core);
await action.createDeployment(applicationName, fullRepositoryName, branchName, branchName, commitId, null, null, core);
} catch (e) {
console.log(`👉🏻 ${e.message}`);
process.exit(1);
Expand Down
14 changes: 7 additions & 7 deletions create-deployment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function fetchBranchConfig(branchName, core) {
function fetchBranchConfig(configLookupName, core) {
const fs = require('fs');
const yaml = require('js-yaml');

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

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

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

exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, commitId, runNumber, skipSequenceCheck, core) {
const branchConfig = fetchBranchConfig(branchName, core);
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, 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 deploymentGroupConfig = branchConfig.deploymentGroupConfig;
Expand Down
17 changes: 9 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = JSON.parse("{\"name\":\"@octokit/rest\",\"version\":\"16.43.2\"
"use strict";


function fetchBranchConfig(branchName, core) {
function fetchBranchConfig(configLookupName, core) {
const fs = __webpack_require__(5747);
const yaml = __webpack_require__(1917);

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

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

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

exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, commitId, runNumber, skipSequenceCheck, core) {
const branchConfig = fetchBranchConfig(branchName, core);
exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, 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 deploymentGroupConfig = branchConfig.deploymentGroupConfig;
Expand Down Expand Up @@ -202,6 +202,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 configLookupName = core.getInput('config-name') || branchName;

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

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

try {
action.createDeployment(applicationName, fullRepositoryName, branchName, commitId, runNumber, skipSequenceCheck, core);
action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
} catch (e) {}
})();

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 configLookupName = core.getInput('config-name') || branchName;

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

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

try {
action.createDeployment(applicationName, fullRepositoryName, branchName, commitId, runNumber, skipSequenceCheck, core);
action.createDeployment(applicationName, fullRepositoryName, branchName, configLookupName, commitId, runNumber, skipSequenceCheck, core);
} catch (e) {}
})();