jmongard / Git.SemVersioning.Gradle

Gradle plugin for automatically versioning a project using semantic versioning and conventional commits with change log support based on git commit messages.
https://plugins.gradle.org/plugin/com.github.jmongard.git-semver-plugin
Apache License 2.0
38 stars 4 forks source link

Use releaseVersion in Github Actions #6

Closed Arc-E-Tect closed 3 years ago

Arc-E-Tect commented 3 years ago

Hi,

I was wondering, how would you recommend using ./gradlew releaseVersion in a Github Actions worlflow?

I checked the workflow in your project, but it seems you are not using the plugin from the workflow to version the plugin itself.

I tried to define a workflow that would version automatically when a push to master is performed.

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
...
      - name: Retrieve current version number
        run: ./gradlew printVersion

      - name: Update release
        run: |
          ./gradlew releaseVersion

      - name: Push changes
        run: |
          git config --global user.name ${{env.GIT_COMMIT_AUTHOR_NAME}}
          git config --global user.email ${{env.GIT_COMMIT_AUTHOR_EMAIL}}
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
          git push origin --all --force
          git push origin --tags
          git tag
          git ls-remote --tags

      - name: Retrieve updated version number
        run: ./gradlew printVersion
...
  publish:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
...
      - name: Retrieve current version number
        run: |
          ./gradlew printVersion
...

The output shows that before the releaseVersion the output is:

> Task :printVersion
--------------------
Version: 0.0.25-SNAPSHOT+006.sha.a6f4a84

BUILD SUCCESSFUL in 920ms
1 actionable task: 1 executed

And after running the releaseVersion the output is:

> Task :printVersion
--------------------
Version: 0.0.25+sha.1e11c21

BUILD SUCCESSFUL in 983ms
1 actionable task: 1 executed

The commit message release: v0.0.23 is pushed to master, and the release tag is created. Still when I fetch the repository in another job or workflow the printVersion shows the snapshot version with the changes:

> Task :printVersion
--------------------
Version: 0.0.25-SNAPSHOT+006.sha.a6f4a84

BUILD SUCCESSFUL in 10s
1 actionable task: 1 executed

I was hoping that there is a recipe available for using the plugin from within a Github Actions workflow.

Thanks

jmongard commented 3 years ago

Hi,

Do you rely need to separate the creation of the release from the publishing of the release into separate jobs? The version in the first job is what you want after calling gradlew releaseVersion so you could just continue with the publishing part.

The second job will checkout the same commit as the first job and that is not the new release commit. You can skip creating a release commit and only create a release tag and it will work with two jobs.

I guess it is also possible to save the new commits SHA in a build artifact or cache and then use the ref option of the checkout action to check out the new commit from the second job but it will take some time to figure out exactly how.

Example of how to use the --no-commit option to skip creating a tag and make the separate jobs work:

name: Create release

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Retrieve current version number
        run: ./gradlew printVersion

      - name: Update release
        run: |
          ./gradlew releaseVersion --no-commit

      - name: Push the release tag
        run: |
          git push origin --tags

      - name: Retrieve updated version number
        run: ./gradlew printVersion

  publish:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Retrieve current version number to publish
        run: |
          ./gradlew printVersion
Arc-E-Tect commented 3 years ago

Thanks for the reply, it made me rethink the separate jobs option and I decided not to go that route. It does mean that I need to do some work in a separate workflow, but I think it is cleaner now.

Thanks again.