Skip to content

Commit

Permalink
feature: add prerun script (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonraid authored Jan 9, 2021
1 parent d8242cb commit a0c5442
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env*
test_prerun_script.sh
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ADD https://github.com/roboll/helmfile/releases/download/${HELMFILE_VERSION}/hel

COPY . .

RUN npm install \
RUN npm install --only=prod \
&& mv /tmp/helmfile_linux_amd64 /usr/local/bin/helmfile \
&& chmod +x /usr/local/bin/helmfile \
&& curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash \
Expand All @@ -26,6 +26,6 @@ RUN npm install \
RUN mkdir -p ${HELM_PLUGIN_PATH} \
&& helm plugin install https://github.com/zendesk/helm-secrets \
# copy must be executed as last command in order to copy all plugins it ${HELM_PLUGIN_PATH}
&& cp -r /root/.cache/helm/plugins/* /opt/helm/plugins/
&& cp -r /root/.cache/helm/plugins/* ${HELM_PLUGIN_PATH}

ENTRYPOINT [ "node", "/app/src/index.js" ]
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ env:
inputKey3: inputValue3
```
| Input | Description | Example | Required |
| :--------- | ----------------------------------------: | -----------------------------: | -------: |
| TYPE | processor type | `ubuntu` | yes |
| FILE | path to file to be updated (JSON or YAML) | `deploy/ami.json` | yes |
| KEY | key in file to be updated | `builders.source_ami` | yes |
| REPOSITORY | repository | `dragonraid/deployment-bumper` | yes |
| USERNAME | github username | `dragonraid` | yes |
| PASSWORD | password or github personal access token | `xxxx` | yes |
| Input | Description | Example | Required |
| :------------- | ------------------------------------------: | -----------------------------: | -------: |
| TYPE | processor type | `ubuntu` | yes |
| FILE | path to file to be updated (JSON or YAML) | `deploy/ami.json` | yes |
| KEY | key in file to be updated | `builders.source_ami` | yes |
| REPOSITORY | repository | `dragonraid/deployment-bumper` | yes |
| USERNAME | github username | `dragonraid` | yes |
| PASSWORD | password or github personal access token | `xxxx` | yes |
| PRE_RUN_SCRIPT | shell script to execute before running type | `./prerun.sh` | no |

## Types

Expand Down Expand Up @@ -82,6 +83,8 @@ jobs:

### Helmfile lock

**TYPE: `helmfile`**

With this type you can update [helmfile](https://github.com/roboll/helmfile) lock files.
Under the hood this type runs [helmfile deps](https://github.com/roboll/helmfile#deps) command.

Expand Down
36 changes: 35 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fs = require('fs').promises;

const { Repository } = require('./github');
const handlers = require('./handlers');

const DEFAULT_SHELL = '/bin/bash';
const PRE_RUN_SCRIPT_PERMISSIONS = 0o744;

/**
* Raw configuration.
*/
Expand All @@ -12,13 +19,16 @@ const RAW_CONFIG = {
REPOSITORY: process.env.REPOSITORY || process.env.GITHUB_REPOSITORY,
USERNAME: process.env.USERNAME || null,
PASSWORD: process.env.PASSWORD || null,
PRE_RUN_SCRIPT: process.env.PRE_RUN_SCRIPT || null,
};

const CONFIG = {};

// TODO: might be prudent to put helper functions from here to i.e. utils.js

/**
* This function processes RAW_CONFIG object. Reasoning is that environment
* variables, that populates th Config can only contain string, but sometimes
* variables, that populates this Config can only contain string, but sometimes
* we need other types. It also checks, if RAW_CONFIG object contains all
* necessary properties and those properties are valid.
*/
Expand All @@ -36,6 +46,21 @@ const processConfig = () => {
}
};

/**
* Execute custom shell script before calling processor
* @param {string} script - path to script
*/
const preRunScript = async (script) => {
console.log(`changing permissions of ${script} to 744`);
fs.chmod(script, PRE_RUN_SCRIPT_PERMISSIONS);
console.log(`Executing ${script}`);
const { stdout, stderr } = await exec(
script,
{ shell: DEFAULT_SHELL },
);
console.log(stdout, stderr);
};

/**
* Clone repository and checkout the feature branch
* @param {string} branchName - feature branch name
Expand Down Expand Up @@ -72,6 +97,15 @@ const PROCESSOR_TYPES = {
process.exit(1);
}

if (CONFIG.PRE_RUN_SCRIPT) {
try {
await preRunScript(CONFIG.PRE_RUN_SCRIPT);
} catch (err) {
console.error(`Running ${CONFIG.PRE_RUN_SCRIPT} failed.`, err);
process.exit(1);
}
}

try {
const repository = await initializeRepo(
`${CONFIG.BRANCH_PREFIX}/${CONFIG.BRANCH_NAME}`,
Expand Down

0 comments on commit a0c5442

Please sign in to comment.