Merge pull request #38 from axivo/fix/workflow-jobs #13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Charts Release | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - .github/workflows/release.yml | |
| - .github/cr-config.yml | |
| - .github/repository.md.gotmpl | |
| - apps/** | |
| - library/** | |
| workflow_dispatch: | |
| jobs: | |
| release: | |
| name: Package and Publish | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Configure repository | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const runGit = async (args) => (await exec.getExecOutput('git', args)).stdout.trim(); | |
| try { | |
| await Promise.all([ | |
| runGit(['config', 'user.name', 'github-actions[bot]']), | |
| runGit(['config', 'user.email', '41898282+github-actions[bot]@users.noreply.github.com']) | |
| ]); | |
| } catch (error) { | |
| core.setFailed(error.message); | |
| } | |
| - name: Install Helm | |
| uses: azure/setup-helm@v3 | |
| with: | |
| version: v3.12.0 | |
| - name: Prepare gh-pages branch | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| const runGit = async (args) => (await exec.getExecOutput('git', args)).stdout.trim(); | |
| try { | |
| const branchExists = (await runGit(['ls-remote', '--heads', 'origin', 'gh-pages'])).length > 0; | |
| if (!branchExists) { | |
| core.info('Creating gh-pages branch with charts directory.'); | |
| await runGit(['checkout', '--orphan', 'gh-pages']); | |
| await runGit(['rm', '-rf', '.']); | |
| fs.mkdirSync('.', { recursive: true }); | |
| core.info('Creating placeholder file in charts directory.'); | |
| const placeholderContent = '# AXIVO Helm Charts\n\nThis repository contains Helm charts published by chart-releaser.\n'; | |
| fs.writeFileSync('README.md', placeholderContent); | |
| await runGit(['add', 'README.md']); | |
| await runGit(['commit', '-m', 'chore(github-action): initialize gh-pages branch with charts directory']); | |
| await runGit(['push', 'origin', 'gh-pages']); | |
| await runGit(['checkout', 'main']); | |
| } | |
| } catch (error) { | |
| core.setFailed(error.message); | |
| } | |
| - name: Run chart-releaser for apps | |
| uses: helm/chart-releaser-action@v1 | |
| id: release-apps | |
| env: | |
| CR_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| charts_dir: apps | |
| config: .github/cr-config.yml | |
| - name: Run chart-releaser for library | |
| uses: helm/chart-releaser-action@v1 | |
| id: release-library | |
| env: | |
| CR_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| charts_dir: library | |
| config: .github/cr-config.yml | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Install js-yaml package | |
| run: npm install js-yaml | |
| - name: Update index.md on gh-pages | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| const yaml = require(process.cwd() + '/node_modules/js-yaml'); | |
| const runGit = async (args) => (await exec.getExecOutput('git', args)).stdout.trim(); | |
| try { | |
| await runGit(['fetch', 'origin', 'gh-pages']); | |
| await runGit(['checkout', '-f', 'gh-pages']); | |
| const indexPath = 'index.yaml'; | |
| if (!fs.existsSync(indexPath)) { | |
| core.warning(indexPath + ' not found, skipping index.md update.'); | |
| await runGit(['checkout', 'main']); | |
| return; | |
| } | |
| await runGit(['checkout', 'main', '.github/repository.md.gotmpl']); | |
| const template = fs.readFileSync('.github/repository.md.gotmpl', 'utf8'); | |
| const index = yaml.load(fs.readFileSync(indexPath, 'utf8')); | |
| if (!index || !index.entries) { | |
| core.warning('Invalid or empty index.yaml file, skipping index.md update.'); | |
| await runGit(['checkout', 'main']); | |
| return; | |
| } | |
| const chartTable = Object.entries(index.entries) | |
| .sort(([a], [b]) => a.localeCompare(b)) | |
| .map(([name, versions]) => { | |
| if (!versions || !versions.length) { | |
| return null; | |
| } | |
| return `| \`${name}\` | ${versions[0].version} | ${versions[0].description || ''} |`; | |
| }) | |
| .filter(Boolean) | |
| .join('\n'); | |
| const templatePattern = '{{ range .Charts }}| `{{ .Name }}` | {{ .Version }} | {{ .Description }} |\n{{ end }}'; | |
| const templateContent = template.replace(templatePattern, chartTable); | |
| fs.writeFileSync('index.md', templateContent); | |
| await runGit(['add', 'index.md']); | |
| const files = (await runGit(['diff', '--staged', '--name-only'])).split('\n').filter(Boolean); | |
| if (files.length > 0) { | |
| const placeholderPath = '.placeholder'; | |
| if (fs.existsSync(placeholderPath)) { | |
| fs.unlinkSync(placeholderPath); | |
| await runGit(['add', placeholderPath]); | |
| } | |
| await runGit(['commit', '-m', 'chore(github-action): update chart releases documentation']); | |
| await runGit(['push', 'origin', 'gh-pages']); | |
| } else { | |
| core.info('No changes to index.md detected.'); | |
| } | |
| await runGit(['checkout', 'main']); | |
| core.info('Successfully processed index.md for gh-pages branch.'); | |
| } catch (error) { | |
| core.error(error.message); | |
| try { | |
| await runGit(['checkout', 'main']); | |
| } catch (checkoutError) { | |
| core.error(checkoutError.message); | |
| } | |
| } |