GitHub action: Next version proposal action
Tags: github, githubactions, typescript
Published: 2023-07-17 10:28:23

Github action which recommends a name for the next version based on your git tag and pull request labels.

## Usages

### Inputs

| Name                | Required | Description                                                              |
| :------------------ | :------: | :----------------------------------------------------------------------- |
| github_token        |    ✅    | GitHub Personal Access Token. It requires REPO scope.                    |
| pr                  |    ✅    | Pull request number. Input just number. e.g.) 100                        |
| major_labels        |    ✅    | A comma-separated list of label names to increment the major version by. |
| minor_labels        |    ✅    | A comma-separated list of label names to increment the minor version by. |
| patch_labels        |    ✅    | A comma-separated list of label names to increment the patch version by. |
| next_version_prefix |          | Next version prefix                                                      |

### Outputs

| Name               | Description                               |
| :----------------- | :---------------------------------------- |
| latest_version     | Latest version of git tag                 |
| next_version       | Recommended next version name             |
| next_version_major | Major version of Recommended next version |
| next_version_minor | Minor version of Recommended next version |
| next_version_patch | Patch version of Recommended next version |

- next_version is `1.0.0` if latest version could not find.
- latest_version is latest git tag name of git tags SEMVER[^semver] formatted.

### Example

We recommend using this action in the `pull_request` event, because this action requires PR ref.

You can recognize the PR is closed as completed with condition of `github.event.pull_request.merged == true`.

```yaml
name: 'create-tag'

on:
  pull_request:
    branches:
      - main
    types:
      - closed

jobs:
  next_version:
    runs-on: ubuntu-latest
    if: github.event.pull_request.merged == true # It represents PR is closed as completed
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Get next version
        uses: bbonkr/next-version-proposal-action@v1
        id: next_version_proposal
        with:
          github_token: ${{ github.token }}
          pr: ${{ github.event.pull_request.number }}
          major_labels: 'major,next'
          minor_labels: 'enhancement,feature'
          patch_labels: 'bug,documentation,chore,dependencies'
          next_version_prefix: 'v'

      - name: logging
        run: |
          echo "latest_version=${{ steps.next_version_proposal.outputs.latest_version }}"
          echo "next_version=${{ steps.next_version_proposal.outputs.next_version }}"
          echo "next_version_major=${{ steps.next_version_proposal.outputs.next_version_major }}"
          echo "next_version_minor=${{ steps.next_version_proposal.outputs.next_version_minor }}"
          echo "next_version_patch=${{ steps.next_version_proposal.outputs.next_version_patch }}"
```

## References

- https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
- https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
- https://docs.github.com/en/actions/learn-github-actions/expressions#functions
- https://docs.github.com/en/actions/using-workflows/using-github-cli-in-workflows
- https://docs.github.com/ko/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests
- https://docs.github.com/ko/rest/git/refs?apiVersion=2022-11-28#list-matching-references

## Closing

You can see code from [GitHub: bbonkr/next-version-proposal-action](https://github.com/bbonkr/next-version-proposal-action)

You can find GitHub Action from [GitHub Marketplace: next-version-proposal-action](https://github.com/marketplace/actions/next-version-proposal-action)

I hope this is a convenient solution for your version management.

[^semver]: https://semver.org/