From 7d7f574e1d8ba69de16952a9802b0efb0edbbd62 Mon Sep 17 00:00:00 2001 From: Kiran Varma Date: Thu, 28 Nov 2024 15:06:57 +0000 Subject: [PATCH] test --- .../workflows/update-version-create-tag.yml | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 .github/workflows/update-version-create-tag.yml diff --git a/.github/workflows/update-version-create-tag.yml b/.github/workflows/update-version-create-tag.yml new file mode 100644 index 000000000..3ac2fe120 --- /dev/null +++ b/.github/workflows/update-version-create-tag.yml @@ -0,0 +1,78 @@ +name: Update Version and Create Tag Based on Commit Messages + +on: + push: + branches: [test] + +jobs: + update-version-and-tag: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Determine Next Version + id: determine-next-version + uses: actions/github-script@v6 + with: + script: | + const core = require('@actions/core'); + const { exec } = require('child_process'); + + // Regex to match commit messages for major, minor, and patch versions + const majorRegex = /major/; + const minorRegex = /feat/; + const patchRegex = /fix/; + + // Get the last commit + exec('git log -1 --pretty=format:"%s"', (error, stdout, stderr) => { + if (error) { + core.setFailed(`Error getting commit messages: ${error.message}`); + return; + } + + const commits = stdout.trim().split('\n'); + let nextVersion = 'patch'; // Default to patch version + + commits.forEach(commit => { + if (majorRegex.test(commit)) { + nextVersion = 'major'; + return; + } else if (minorRegex.test(commit)) { + nextVersion = 'minor'; + return; + } + }); + + exec(`npm version ${nextVersion}`, (error, stdout, stderr) => { + if (error) { + core.setFailed(`Error determining next version: ${error.message}`); + return; + } + core.setOutput('next-version', stdout.trim().split('\n')[0]); + }); + }); + + - name: Commit Changes + run: git commit -am "Bump version to ${{ steps.determine-next-version.outputs.next-version }}" + + - name: Push Changes + run: git push origin main + + - name: Create Tag + uses: actions/github-script@v6 + with: + script: | + const core = require('@actions/core'); + const { exec } = require('child_process'); + + const nextVersion = core.getInput('next-version'); + + exec(`git tag v${nextVersion}`, (error, stdout, stderr) => { + if (error) { + core.setFailed(`Error creating tag: ${error.message}`); + return; + } + }); + + - name: Push Tag + run: git push origin --tags