Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
briananstett committed Sep 5, 2023
0 parents commit 93ce504
Show file tree
Hide file tree
Showing 11 changed files with 4,591 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Code Deployment
on:
push:
branches:
- master
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 18
- name: Install dependencies
run: |
npm install
npm i --no-save @vercel/ncc semantic-release @semantic-release/changelog @semantic-release/git @semantic-release/exec conventional-changelog-conventionalcommits
- name: Release
run: |
npx ncc build index.js
git add --force dist/
git commit -m'chore(release) distribution files [skip ci]
git push origin master
npx semantic-release
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 2,
"printWidth": 100,
"singleQuote": true,
"trailingComma": "es5"
}
18 changes: 18 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Hello World"
description: "Greet someone and record the time"
inputs:
regexReplaces:
description: "Stringified array Javascript replace patterns."
required: false
default: '[["feature\/","dev-"],["release\/","qa-"]]'
environmentMap:
description: "Stringified object where the keys are branch names and the values is an array of environments to map to."
default: '{}'
outputs:
environment:
description: "The environment to deploy to based on the regex replacements."
environmentMatrix:
description: "Matrix containing mapped environments."
runs:
using: "node16"
main: "dist/index.js"
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const core = require('@actions/core');
const getVariables = require('./lib/util/variables.util');
const translateBranch = require('./lib/branchTranslation');

(async () => {
try {
const variables = getVariables();
// TODO Validate

// Get current branch
const currentBranch = process.env.GITHUB_REF.split('refs/heads/')[1];
const translatedCurrentBranch = translateBranch(currentBranch, variables.regexReplaces);

core.setOutput('environment', translatedCurrentBranch);

const matrix = variables.environmentMap[translatedCurrentBranch] || [translatedCurrentBranch];

core.setOutput('environmentMatrix', JSON.stringify(matrix));
} catch (error) {
core.setFailed(error.message);
}
})();
10 changes: 10 additions & 0 deletions lib/branchTranslation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = (branch, regexReplacePatterns) => {
let translatedBranch = branch;

regexReplacePatterns.forEach(([pattern, replacement]) => {
const regex = new RegExp(pattern, 'g');
translatedBranch = translatedBranch.replace(regex, replacement);
});

return translatedBranch;
};
29 changes: 29 additions & 0 deletions lib/util/variables.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const core = require('@actions/core');
const yaml = require('yaml');
const fs = require('fs');
const path = require('path');

module.exports = () => {
let actionDotYaml;
const variables = {};

try {
actionDotYaml = fs.readFileSync(path.join(__dirname, '../../action.yml'), 'utf8');
} catch (fsError) {
console.error(fsError);
throw new Error(
"Error while trying to parse the 'action.yml' file in the root of the directory. Make sure it exists."
);
}

const parsedActionDotYaml = yaml.parse(actionDotYaml);
Object.keys(parsedActionDotYaml.inputs).forEach((inputVariable) => {
try {
variables[inputVariable] = JSON.parse(core.getInput(inputVariable));
} catch (error) {
variables[inputVariable] = core.getInput(inputVariable);
}
});

return variables;
};
Loading

0 comments on commit 93ce504

Please sign in to comment.