From e2e458c92f34e631be45157b60963644a4d0635c Mon Sep 17 00:00:00 2001 From: peterdeme Date: Mon, 25 Apr 2022 10:47:04 +0200 Subject: [PATCH] feat(release): update release flow --- .github/workflows/ci.yml | 25 ++++++------- .github/workflows/initiate_release.yml | 47 ++++++++++++++++++++++++ .github/workflows/release.yml | 49 ++++++++++++++++++++++++++ .github/workflows/reviewdog.yml | 7 ++-- .versionrc.js | 16 +++++++++ scripts/get_changelog_diff.js | 26 ++++++++++++++ 6 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/initiate_release.yml create mode 100644 .github/workflows/release.yml create mode 100644 .versionrc.js create mode 100644 scripts/get_changelog_diff.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff7efcb..a470793 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,7 @@ name: build on: push: branches: - - 'main' + - "main" pull_request: jobs: @@ -11,24 +11,25 @@ jobs: strategy: fail-fast: false matrix: - python: [3.6, 3.7, 3.8, 3.9, '3.10'] + python: [3.6, 3.7, 3.8, 3.9, "3.10"] django: - - 'django>=4.0.0, <4.1' - - 'django>=3.2.0, <3.3' - - 'django>=3.1.0, <3.2' - - 'django>=3.0.0, <3.1' - - 'django>=2.2.0, <3.0' - - 'django>=2.1.0, <2.2.0' - - 'django>=2.0.0, <2.1.0' + - "django>=4.0.0, <4.1" + - "django>=3.2.0, <3.3" + - "django>=3.1.0, <3.2" + - "django>=3.0.0, <3.1" + - "django>=2.2.0, <3.0" + - "django>=2.1.0, <2.2.0" + - "django>=2.0.0, <2.1.0" exclude: - python: 3.6 - django: 'django>=4.0.0, <4.1' + django: "django>=4.0.0, <4.1" - python: 3.7 - django: 'django>=4.0.0, <4.1' + django: "django>=4.0.0, <4.1" - python: "3.10" - django: 'django>=2.0.0, <2.1.0' + django: "django>=2.0.0, <2.1.0" steps: - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 with: python-version: ${{ matrix.python }} diff --git a/.github/workflows/initiate_release.yml b/.github/workflows/initiate_release.yml new file mode 100644 index 0000000..5fd3be4 --- /dev/null +++ b/.github/workflows/initiate_release.yml @@ -0,0 +1,47 @@ +name: Create release PR + +on: + workflow_dispatch: + inputs: + version: + description: "The new version number. Example: 1.40.1" + required: true + +jobs: + init_release: + name: 🚀 Create release PR + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # gives the changelog generator access to all previous commits + + - name: Update CHANGELOG.md, setup.py and push release branch + env: + VERSION: ${{ github.event.inputs.version }} + run: | + npx --yes standard-version@9.3.2 --release-as "$VERSION" --skip.tag --skip.commit + git config --global user.name 'github-actions' + git config --global user.email 'release@getstream.io' + git checkout -q -b "release-$VERSION" + git commit -am "chore(release): $VERSION" + git push -q -u origin "release-$VERSION" + + - name: Get changelog diff + uses: actions/github-script@v6 + with: + script: | + const get_change_log_diff = require('./scripts/get_changelog_diff.js') + core.exportVariable('CHANGELOG', get_change_log_diff()) + + - name: Open pull request + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh pr create \ + -t "chore(release): ${{ github.event.inputs.version }}" \ + -b "# :rocket: ${{ github.event.inputs.version }} + Make sure to use squash & merge when merging! + Once this is merged, another job will kick off automatically and publish the package. + # :memo: Changelog + ${{ env.CHANGELOG }}" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..24f067c --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,49 @@ +name: Release + +on: + pull_request: + types: [closed] + branches: + - master + - main + +jobs: + Release: + name: 🚀 Release + if: github.event.pull_request.merged && startsWith(github.head_ref, 'release-') + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - uses: actions/github-script@v6 + with: + script: | + const get_change_log_diff = require('./scripts/get_changelog_diff.js') + core.exportVariable('CHANGELOG', get_change_log_diff()) + + // Getting the release version from the PR source branch + // Source branch looks like this: release-1.0.0 + const version = context.payload.pull_request.head.ref.split('-')[1] + core.exportVariable('VERSION', version) + + - uses: actions/setup-python@v3 + with: + python-version: "3.10" + + - name: Publish to PyPi + env: + TWINE_USERNAME: "__token__" + TWINE_PASSWORD: "${{ secrets.PYPI_TOKEN }}" + run: | + pip install -q twine==3.7.1 wheel==0.37.1 + python setup.py sdist bdist_wheel + twine upload --non-interactive dist/* + + - name: Create release on GitHub + uses: ncipollo/release-action@v1 + with: + body: ${{ env.CHANGELOG }} + tag: ${{ env.VERSION }} + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/reviewdog.yml b/.github/workflows/reviewdog.yml index 9736573..d98ff17 100644 --- a/.github/workflows/reviewdog.yml +++ b/.github/workflows/reviewdog.yml @@ -10,19 +10,16 @@ jobs: reviewdog: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: reviewdog/action-setup@v1 with: reviewdog_version: latest - - uses: actions/setup-python@v2 + - uses: actions/setup-python@v3 with: python-version: "3.10" - - name: Add pip bin to PATH - run: echo "/home/runner/.local/bin" >> $GITHUB_PATH - - name: Install deps run: pip install ".[ci]" diff --git a/.versionrc.js b/.versionrc.js new file mode 100644 index 0000000..68d8cd4 --- /dev/null +++ b/.versionrc.js @@ -0,0 +1,16 @@ +const setupPyUpdater = { + VERSION_REGEX: /version = "(.+)"/, + + readVersion: function (contents) { + const version = this.VERSION_REGEX.exec(contents)[1]; + return version; + }, + + writeVersion: function (contents, version) { + return contents.replace(this.VERSION_REGEX.exec(contents)[0], `version = "${version}"`); + } +} + +module.exports = { + bumpFiles: [{ filename: './setup.py', updater: setupPyUpdater }], +} diff --git a/scripts/get_changelog_diff.js b/scripts/get_changelog_diff.js new file mode 100644 index 0000000..317e286 --- /dev/null +++ b/scripts/get_changelog_diff.js @@ -0,0 +1,26 @@ +/* +Here we're trying to parse the latest changes from CHANGELOG.md file. +The changelog looks like this: + +## 0.0.3 +- Something #3 +## 0.0.2 +- Something #2 +## 0.0.1 +- Something #1 + +In this case we're trying to extract "- Something #3" since that's the latest change. +*/ +module.exports = () => { + const fs = require('fs'); + + changelog = fs.readFileSync('CHANGELOG.md', 'utf8'); + releases = changelog.match(/## [?[0-9](.+)/g); + + current_release = changelog.indexOf(releases[0]); + previous_release = changelog.indexOf(releases[1]); + + latest_changes = changelog.substr(current_release, previous_release - current_release); + + return latest_changes; +};