From 49e23764205487d132cf84cf7ad9ee7c11ca53d1 Mon Sep 17 00:00:00 2001 From: ravali-rimmalapudi <83863595+ravali-rimmalapudi@users.noreply.github.com> Date: Fri, 10 Sep 2021 20:19:32 +0530 Subject: [PATCH] feat: Added the GitHub actions for cli (#284) * feat: Added the GitHub actions for cli * Fixed the issue of use and run * Refactoring the code and addresed the review comments * Added condition to return if changes are already in changelog * Seperated semantic release related configuration to another file and getting the tag from semantic release * Added the workflow inputs * Update release.yml * Update release.yml Co-authored-by: lakshmiravali --- .github/scripts/commit-api-spec-change-log.sh | 31 +++++++ .github/scripts/update-change-log.js | 32 +++++++ .github/workflows/release.yml | 83 +++++++++++++++---- .releaserc.json | 79 ++++++++++++++++++ package.json | 4 + 5 files changed, 212 insertions(+), 17 deletions(-) create mode 100644 .github/scripts/commit-api-spec-change-log.sh create mode 100644 .github/scripts/update-change-log.js create mode 100644 .releaserc.json diff --git a/.github/scripts/commit-api-spec-change-log.sh b/.github/scripts/commit-api-spec-change-log.sh new file mode 100644 index 000000000..d33fb1336 --- /dev/null +++ b/.github/scripts/commit-api-spec-change-log.sh @@ -0,0 +1,31 @@ +#!/bin/sh +echo "Running update changelog script" +changeLog="$1" +versionType="$2" +echo "$changeLog" +node .github/scripts/update-change-log.js "$changeLog" +echo "Git configurations" +git config --global user.email "team_interfaces+github@twilio.com" +git config --global user.name "twilio-dx" +git add -A +if [ -n "$(git status --porcelain)" ]; then + branch=$(git branch --show-current) + echo "Current branch: $branch" + echo "There are changes to commit."; + commitMessage='' + if [ "$versionType" == 0 ] || [ "$versionType" == 1 ] + then + commitMessage='feat: Updated api definitions changelog in CHANGES.md' + elif [ "$versionType" == 2 ] + then + commitMessage='fix: Updated api definitions changelog in CHANGES.md' + else + echo "Invalid versionType: $versionType"; + exit + fi + echo "Commit message:$commitMessage" + git commit -m "$commitMessage" + git push origin "$branch" +else + echo "No changes to commit"; +fi diff --git a/.github/scripts/update-change-log.js b/.github/scripts/update-change-log.js new file mode 100644 index 000000000..35327b304 --- /dev/null +++ b/.github/scripts/update-change-log.js @@ -0,0 +1,32 @@ +/* eslint-disable no-console */ +const fs = require('fs'); + +const cliChangelogFilename = 'CHANGES.md'; + +const updateChangeLog = async () => { + try { + console.log('Updating the CHANGES.md'); + const changes = process.argv[2]; + if (changes) { + const data = fs.readFileSync(cliChangelogFilename); + if (data.toString().includes(changes)) { + console.log(`Provided changes are already in cli changelog : ${changes}`); + return; + } + const fd = fs.openSync(cliChangelogFilename, 'w+'); + const insert = Buffer.from(changes); + fs.writeSync(fd, insert, 0, insert.length, 0); + fs.writeSync(fd, data, 0, data.length, insert.length); + fs.close(fd, (err) => { + if (err) throw err; + }); + } else { + console.log('There are no changes provided'); + } + } catch (error) { + console.log(`Error while updating the changelog: ${error}`); + } +}; +(async () => { + await updateChangeLog(); +})(); diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 87e6a1f85..160eb1f9e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,20 +1,69 @@ -name: Cli-core Release +name: Cli Release on: workflow_dispatch: + inputs: + change-log: + description: 'Open API Changelog.' + required: true + version-type: + description: 'Version to upgrade, Major: 0, Minor:1, Patch: 2' + required: true + jobs: - test: - runs-on: ubuntu-latest - strategy: - matrix: - node-version: [14.x] - steps: - - name: Checkout cli-core repo - uses: actions/checkout@v2 - - run: npm install - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - name: Run tests - run: npm test + test: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [14.x] + steps: + - name: Checkout cli-core repo + uses: actions/checkout@v2 + - run: npm install + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - name: Run tests + run: npm test + update-api-definitions-changelog: + runs-on: ubuntu-latest + needs: [test] + steps: + - name: Checkout cli + uses: actions/checkout@v2 + - name: Update Changelog + run: | + bash .github/scripts/commit-api-spec-change-log.sh "${{ github.event.inputs.change-log }}" ${{ github.event.inputs.version-type }} + release: + runs-on: ubuntu-latest + needs: [update-api-definitions-changelog] + outputs: + tag-name: ${{ steps.semantic-release.outputs.TAG_NAME }} + steps: + - name: Checkout cli + uses: actions/checkout@v2 + - name: Run git and npm update + run: | + git pull + npm install + - name: Semantic Release runs + id: semantic-release + run: npx semantic-release -t \${version} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + update-release: + runs-on: ubuntu-latest + needs: [ release ] + steps: + - name: Checkout cli + uses: actions/checkout@v2 + - name: Update release + if: ${{needs.release.outputs.tag-name != ''}} + uses: tubone24/update_release@v1.2.0 + env: + GITHUB_TOKEN: ${{ github.token }} + TAG_NAME: ${{needs.release.outputs.tag-name}} + with: + is_append_body: true + body: ${{ github.event.inputs.change-log }} diff --git a/.releaserc.json b/.releaserc.json new file mode 100644 index 000000000..bb71dde47 --- /dev/null +++ b/.releaserc.json @@ -0,0 +1,79 @@ +{ + "branches": [ + "main", + { + "name": "github_action_changes", + "prerelease": "rc" + } + ], + "plugins": [ + [ + "@semantic-release/commit-analyzer", + { + "preset": "angular", + "releaseRules": [{ + "type": "chore", + "release": "patch" + }] + } + ], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits", + "presetConfig": { + "types": [{ + "type": "feat", + "section": "Library - Features" + }, + { + "type": "fix", + "section": "Library - Fixes" + }, + { + "type": "chore", + "section": "Library - Chores" + }, + { + "type": "test", + "section": "Library - Test" + }, + { + "type": "docs", + "section": "Library - Docs" + } + ] + } + } + ], + [ + "@semantic-release/changelog", + { + "changelogFile": "CHANGES.md" + } + ], + [ + "@semantic-release/npm", + { + "npmPublish": false + } + ], + "@semantic-release/github", + [ + "@semantic-release/git", + { + "assets": [ + "CHANGES.md", + "package.json" + ], + "message": "chore(release): set `package.json` to ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" + } + ], + [ + "@semantic-release/exec", + { + "successCmd": "echo '::set-output name=TAG_NAME::${nextRelease.version}'" + } + ] + ] +} diff --git a/package.json b/package.json index a7dcf476a..640da649d 100644 --- a/package.json +++ b/package.json @@ -54,9 +54,13 @@ "devDependencies": { "@oclif/dev-cli": "^1.22.2", "@oclif/test": "^1.2.6", + "@semantic-release/changelog": "^5.0.1", + "@semantic-release/exec": "^5.0.0", + "@semantic-release/git": "^9.0.0", "@twilio/cli-test": "^2.1.0", "aws-sdk": "^2.757.0", "chai": "^4.2.0", + "conventional-changelog-conventionalcommits": "^4.6.0", "eslint": "^7.5.0", "eslint-config-twilio": "~1.31.0", "eslint-config-twilio-mocha": "~1.31.0",