Skip to content

Commit

Permalink
Cleared merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-mcconnell committed Mar 3, 2020
2 parents dcce0fa + 5840b5b commit bf9013f
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 94 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
on: [push]

jobs:
hello_world_job:
Test_Job:
runs-on: ubuntu-latest
name: A job to say hello
name: Test job
steps:
<<<<<<< HEAD
# To use this repository's private action, you must check out the repository
=======
>>>>>>> origin/feature/bootstrap
- name: Checkout
uses: actions/checkout@v2
- name: Test this action
uses: ./ # Uses an action in the root directory
uses: ./
with:
access-token: ${{ secrets.CR_TOKEN }}
source-charts-folder: 'test-charts'
destination-repo: ${{ secrets.CR_TOKEN }}
destination-repo: dave-mcconnell/helm-charts
destination-charts-folder: 'charts'
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,3 @@ Defaults to `v3`.
Create a repository with the format `<YOUR/ORG USERNAME>.github.io`, push your
helm sources to a branch other than `master` and add this GitHub Action to
your workflow! 🚀😃

## Related Links

[github-access-token]: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line
[github-action-input]: https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idstepswith
[github-pages-domain-docs]: https://help.github.com/en/articles/using-a-custom-domain-with-github-pages
[github-repo-secret]: https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
description: 'The folder to copy packaged helm charts into'
required: false
default: 'charts'
helm-version:
description: 'The version of Helm being used - either v2 or v3'
required: false
default: 'v3'
runs:
using: 'node12'
main: 'index.js'
196 changes: 113 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ const ioUtil = require('@actions/io/lib/io-util');
const { readdirSync } = require('fs');
const path = require('path');

const getDirectories = fileName =>
readdirSync(fileName, {
withFileTypes: true,
})
.filter(dirent => dirent.isDirectory())
.filter(dirent => !(/(^|\/)\.[^\/\.]/g).test(dirent))
.map(dirent => dirent.name);

async function run() {

try {
const sourceBranch = github.context.ref
const accessToken = core.getInput('access-token');
const sourceChartsDir = core.getInput('source-charts-folder') | 'charts';

const sourceRepo = `${github.context.repo.owner}/${github.context.repo.repo}`;
const sourceBranch = github.context.ref.replace('refs/heads/', '')
const sourceChartsDir = core.getInput('source-charts-folder') ? core.getInput('source-charts-folder') : 'charts';

const destinationRepo = core.getInput('destination-repo');
const destinationBranch = core.getInput('destination-branch') | 'master'
const destinationChartsDir = core.getInput('destination-charts-folder') | 'charts';
const destinationBranch = core.getInput('destination-branch') ? core.getInput('destination-branch') : 'master'
const destinationChartsDir = core.getInput('destination-charts-folder') ?core.getInput('destination-charts-folder') : 'charts';

let useHelm3 = true;
if (!core.getInput('helm-version')) {
useHelm3 = true
}
else useHelm3 = core.getInput('helm-version') === 'v3' ? true : false;

console.log('Running Push Helm Chart job with:')
console.log('Source Branch:' + sourceBranch)
console.log('Source Charts Directory:' + sourceChartsDir)
console.log('Destination Repo:' + destinationRepo)
console.log('Destination Branch:' + destinationBranch)
console.log('Destination Charts Directory:' + destinationChartsDir)

if (!accessToken) {
core.setFailed(
Expand All @@ -38,87 +47,108 @@ async function run() {
return;
}

const sourceRepo = `${github.context.repo.owner}/${github.context.repo.repo}`;
const sourceRepoURL = `https://${accessToken}@github.com/${sourceRepo}.git`;
const destinationRepoURL = `https://${accessToken}@github.com/${destinationRepo}.git`;

// clone source repo
console.log(`Deploying to repo: ${sourceRepo} and branch: ${sourceBranch}`);
await exec.exec(`git clone`, ['-b', sourceBranch, sourceRepoURL, 'sourceRepo'], {
cwd: './',
});

// git config
await exec.exec(`git config user.name`, [github.context.actor], {
cwd: './',
});
await exec.exec(
`git config user.email`,
[`${github.context.actor}@users.noreply.github.com`],
{ cwd: './' }
);

// package helm charts
const chartDirectories = getDirectories(path.resolve(`./sourceRepo/${sourceChartsDir}`));

console.log('Charts dir content');
await exec.exec(`ls`, ['-I ".*"'], { cwd: `./sourceRepo/${sourceChartsDir}` });
for (const chartDirname of chartDirectories) {
console.log(`Resolving helm chart dependency in directory ${chartDirname}`);
await exec.exec(
`helm dependency update`,
[],
{ cwd: `./sourceRepo/${sourceChartsDir}/${chartDirname}` }
);

console.log(`Packaging helm chart in directory ${chartDirname}`);
await exec.exec(
`helm package`,
[chartDirname, '--destination', '../output'],
{ cwd: `./sourceRepo/${sourceChartsDir}` }
);
if (useHelm3) {
await InstallHelm3Latest();
}

console.log('Packaged all helm charts.');
await ConfigureGit()
await CloneGitRepo(sourceRepo, sourceBranch, accessToken, 'sourceRepo')
await CloneGitRepo(destinationRepo, destinationBranch, accessToken, 'destinationRepo')

const cnameExists = await ioUtil.exists('./CNAME');
if (cnameExists) {
console.log('Copying CNAME over.');
await io.cp('./CNAME', './output/CNAME', { force: true });
console.log('Finished copying CNAME.');
}
await PackageHelmCharts(`./sourceRepo/${sourceChartsDir}`, `../../destinationRepo/${destinationChartsDir}`)
await GenerateIndex()
await AddCommitPushToGitRepo(`./destinationRepo`, `${github.context.sha}`, destinationBranch)

} catch (error) {
core.setFailed(error.message);
}
}

const getDirectories = fileName =>
readdirSync(fileName, {
withFileTypes: true,
})
.filter(dirent => dirent.isDirectory())
.filter(dirent => !(/(^|\/)\.[^\/\.]/g).test(dirent))
.map(dirent => dirent.name);

const InstallHelm3Latest = async () => {
await exec.exec(`curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3`, [], { cwd: `./` });
await exec.exec(`chmod 700 get_helm.sh`, [], { cwd: `./` });
await exec.exec(`./get_helm.sh`, [], { cwd: `./` });

await exec.exec(`helm version`, [], { cwd: `./` }
)
}

const ConfigureGit = async () => {

await exec.exec(`git config --global user.name`, [github.context.actor], {
cwd: './',
});

await exec.exec(
`git config --global user.email`,
[`${github.context.actor}@users.noreply.github.com`],
{ cwd: './' }
);
}

const CloneGitRepo = async (repoName, branchName, accessToken, cloneDirectory) => {

const repoURL = `https://${accessToken}@github.com/${repoName}.git`;
await exec.exec(`git clone`, ['-b', branchName, repoURL, cloneDirectory], {
cwd: './',
});

}

// clone destination repo
await exec.exec(`git clone`, ['-b', destinationBranch, destinationRepoURL, 'destinationRepo'], {
cwd: './',
});
const PackageHelmCharts = async (chartsDir, destinationChartsDir) => {

// move published chart
await exec.exec(`mv`, ['./sourceRepo/*.tgz', `./DestinationRepo/${destinationChartsDir}`], {
cwd: './',
});
const chartDirectories = getDirectories(path.resolve(chartsDir));

// push to
await exec.exec(`git add`, ['.'], { cwd: './DestinationRepo' });
console.log('Charts dir content');
await exec.exec(`ls`, ['-I ".*"'], { cwd: chartsDir });
for (const chartDirname of chartDirectories) {

console.log(`Resolving helm chart dependency in directory ${chartDirname}`);
await exec.exec(
`git commit`,
['-m', `Deployed via Helm Publish Action for ${github.context.sha}`],
{ cwd: './output' }
`helm dependency update`,
[],
{ cwd: `${chartsDir}/${chartDirname}` }
);

console.log(`Packaging helm chart in directory ${chartDirname}`);
await exec.exec(
`helm package`,
[chartDirname, '--destination', destinationChartsDir],
{ cwd: chartsDir }
);
await exec.exec(`git push`, ['-u', 'origin', `${destinationBranch}`], {
cwd: './output',
});
console.log('Finished deploying your site.');
}
console.log('Packaged all helm charts.');
}

console.log('Enjoy! ✨');
// generate index
console.log(`Building index.yaml`);
await exec.exec(`helm repo index`, `./output`);
const GenerateIndex = async () => {

console.log(`Successfully build index.yaml.`);
} catch (error) {
core.setFailed(error.message);
}
// generate index
console.log(`Building index.yaml`);
await exec.exec(`helm repo index`, `./destinationRepo`);
console.log(`Successfully generated index.yaml.`);
}

const AddCommitPushToGitRepo = async (workingDir, gitSha, branch) => {
await exec.exec(`git status`, [], { cwd: workingDir });
await exec.exec(`git add`, ['.'], { cwd: workingDir });
await exec.exec(`git status`, [], { cwd: workingDir });
await exec.exec(
`git commit`,
['-m', `Deployed via Helm Publish Action for ${gitSha}`],
{ cwd: workingDir }
);
await exec.exec(`git push`, ['-u', 'origin', `${branch}`],
{ cwd: workingDir }
);
console.log(`Pushed to ${workingDir}`);
}

run();

0 comments on commit bf9013f

Please sign in to comment.