Skip to content

Commit

Permalink
Use Docker for running Helm
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Aug 1, 2021
1 parent c7fc7ff commit d79de3f
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ async function main() {
const tmp = require('tmp');
const {waitFile} = require('wait-file');

console.log("\033[36mPWD: " + process.cwd() + "\033[0m");

tmp.setGracefulCleanup();

const execShFile = tmp.fileSync({
Expand All @@ -13,6 +15,15 @@ async function main() {
postfix: '.sh',
discardDescriptor: true,
});
const dockerKubeConfigDir = process.cwd() + '/docker-kube-config-' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 13);
fs.mkdirSync(dockerKubeConfigDir, {
mode: 0o777,
});
const dockerKubeConfig = dockerKubeConfigDir + '/config';
const helmCacheDir = process.cwd() + '/helm-cache-' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 13);
fs.mkdirSync(helmCacheDir, {
mode: 0o744,
});

const kubeConfigLocation = homedir + '/.kube/config';
const kubeConfigExists = fs.existsSync(kubeConfigLocation);
Expand All @@ -25,15 +36,37 @@ async function main() {
});
fs.appendFileSync(
kubeConfigLocation,
"\r\n\r\n" + process.env.INPUT_KUBECONFIG,
"\r\n\r\n" + process.env.INPUT_KUBECONFIG + "\r\n\r\n",
{
mode: 0o600,
mode: 0o644,
}
);
}

fs.writeFileSync(
dockerKubeConfig,
fs.readFileSync(kubeConfigLocation),
{
mode: 0o777,
}
);
console.log("\033[36mPreparing helm execution\033[0m");
fs.appendFileSync(execShFile.name, process.env.INPUT_EXEC);
fs.appendFileSync(
execShFile.name,
'#!/usr/bin/env bash\n' +
' \n' +
'kubectl () {\n' +
' docker run --network host --rm -v ' + process.cwd() + ':' + process.cwd() + ' -w ' + process.cwd() + ' -v ' + dockerKubeConfigDir + ':' + dockerKubeConfigDir + ' -e KUBECONFIG=' + dockerKubeConfig + ' rancher/kubectl:v1.20.2 "$@"\n' +
'}\n' +
'helm () {\n' +
' docker run --network host --rm -v ' + process.cwd() + ':' + process.cwd() + ' -w ' + process.cwd() + ' -v ' + dockerKubeConfigDir + ':' + dockerKubeConfigDir + ' -e KUBECONFIG=' + dockerKubeConfig + ' -v ' + helmCacheDir + 'cache:/root/.cache/helm -v ' + helmCacheDir + 'config:/root/.config/helm -v ' + helmCacheDir + 'local:/root/.local/share/helm alpine/helm:3.6.3 "$@"\n' +
'}\n' +
' \n' +
'docker pull -q rancher/kubectl:v1.20.2 > /dev/null 2>&1\n' +
'docker pull -q alpine/helm:3.6.3 > /dev/null 2>&1\n' +
' \n' +
process.env.INPUT_EXEC
);

await waitFile({
resources: [
Expand Down Expand Up @@ -61,11 +94,11 @@ async function main() {
});
console.log('::set-output name=helm_output::' + result.split('%').join('%25').split('\n').join('%0A').split('\r').join('%0D'));
} catch (error) {
console.error(error);
process.exit(1);
} finally {
console.log("\033[36mCleaning up: \033[0m");
fs.unlinkSync(execShFile.name);
fs.unlinkSync(dockerKubeConfig);
console.log("\033[36m - exec ✅ \033[0m");
if (!kubeConfigExists) {
fs.unlinkSync(kubeConfigLocation);
Expand Down

0 comments on commit d79de3f

Please sign in to comment.