chore: Create releases when deploying new version #47
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: CD | |
| on: | |
| pull_request: | |
| types: [closed] | |
| jobs: | |
| label-version-bump: | |
| name: Bump version based on PR label | |
| runs-on: ubuntu-latest | |
| if: | | |
| github.event.pull_request.merged | |
| && ( | |
| contains(github.event.pull_request.labels.*.name, 'bump patch') | |
| || contains(github.event.pull_request.labels.*.name, 'bump minor') | |
| || contains(github.event.pull_request.labels.*.name, 'bump major') | |
| ) | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.pull_request.base.ref }} | |
| token: ${{ secrets.POSTHOG_BOT_PAT }} | |
| fetch-depth: 0 | |
| - name: Set up Elixir | |
| uses: erlef/setup-beam@v1 | |
| with: | |
| elixir-version: 1.18.3 | |
| otp-version: 27.3 | |
| - name: Detect version bump type | |
| id: bump-type | |
| run: | | |
| BUMP_TYPE=null | |
| if [[ $BUMP_PATCH_PRESENT == 'true' ]]; then | |
| BUMP_TYPE=patch | |
| fi | |
| if [[ $BUMP_MINOR_PRESENT == 'true' ]]; then | |
| BUMP_TYPE=minor | |
| fi | |
| if [[ $BUMP_MAJOR_PRESENT == 'true' ]]; then | |
| BUMP_TYPE=major | |
| fi | |
| echo "bump-type=$BUMP_TYPE" >> "$GITHUB_OUTPUT" | |
| env: | |
| BUMP_PATCH_PRESENT: ${{ contains(github.event.pull_request.labels.*.name, 'bump patch') }} | |
| BUMP_MINOR_PRESENT: ${{ contains(github.event.pull_request.labels.*.name, 'bump minor') }} | |
| BUMP_MAJOR_PRESENT: ${{ contains(github.event.pull_request.labels.*.name, 'bump major') }} | |
| - name: Determine new version | |
| id: versions | |
| if: steps.bump-type.outputs.bump-type != 'null' | |
| run: | | |
| OLD_VERSION=$(grep '@version "' mix.exs | grep -o '"[0-9]\+\.[0-9]\+\.[0-9]\+"' | tr -d '"') | |
| # Function to bump version using cut | |
| bump_version() { | |
| local version=$1 | |
| local type=$2 | |
| local major=$(echo "$version" | cut -d. -f1) | |
| local minor=$(echo "$version" | cut -d. -f2) | |
| local patch=$(echo "$version" | cut -d. -f3) | |
| case $type in | |
| major) | |
| echo "$((major + 1)).0.0" | |
| ;; | |
| minor) | |
| echo "${major}.$((minor + 1)).0" | |
| ;; | |
| patch) | |
| echo "${major}.${minor}.$((patch + 1))" | |
| ;; | |
| esac | |
| } | |
| NEW_VERSION=$(bump_version "$OLD_VERSION" "${{ steps.bump-type.outputs.bump-type }}") | |
| echo "old-version=$OLD_VERSION" >> "$GITHUB_OUTPUT" | |
| echo "new-version=$NEW_VERSION" >> "$GITHUB_OUTPUT" | |
| - name: Update version in mix.exs | |
| if: steps.bump-type.outputs.bump-type != 'null' | |
| run: | | |
| sed -i "s/@version \"${{ steps.versions.outputs.old-version }}\"/@version \"${{ steps.versions.outputs.new-version }}\"/" mix.exs | |
| - name: Update CHANGELOG.md | |
| run: | | |
| CHANGELOG_HEADING='## ${{ steps.versions.outputs.new-version }} - '$(date --iso-8601) | |
| CHANGELOG_POINTS=$(git log v${{ steps.versions.outputs.old-version }}..${{ github.event.pull_request.base.ref }} --pretty=format:%s --grep='^.*\d*)$' | sed -e 's/^/- /') | |
| mv CHANGELOG.md CHANGELOG.old.md | |
| echo -e "$CHANGELOG_HEADING\n\n$CHANGELOG_POINTS\n\n$(cat CHANGELOG.old.md)" > CHANGELOG.md | |
| rm CHANGELOG.old.md | |
| - name: Commit bump | |
| if: steps.bump-type.outputs.bump-type != 'null' | |
| uses: EndBug/add-and-commit@v7 | |
| with: | |
| branch: ${{ github.event.pull_request.base.ref }} | |
| tag: "v${{ steps.versions.outputs.new-version }}" | |
| message: "chore: Bump version to v${{ steps.versions.outputs.new-version }}" | |
| github_token: ${{ secrets.POSTHOG_BOT_PAT }} | |
| - name: Create release on GitHub | |
| if: steps.bump-type.outputs.bump-type != 'null' | |
| run: | | |
| gh release create "v${{ steps.versions.outputs.new-version }}" --generate-notes | |
| - name: Publish to Hex.pm | |
| if: steps.bump-type.outputs.bump-type != 'null' | |
| run: | | |
| mix local.hex --force | |
| mix local.rebar --force | |
| mix deps.get | |
| mix hex.publish --yes | |
| env: | |
| HEX_API_KEY: ${{ secrets.HEX_API_KEY }} |