5

I have to create a release by using the create-release action provided by GitHub actions. The default config parameters in the build.gradle file like the following;

defaultConfig {
    applicationId "com.example.myapp"
    minSdk 21
    targetSdk 31
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

and the action in the GitHub Actions yml file like the following;

- name: Create Release
    id: create_release
    uses: actions/create-release@v1
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    with:
      tag_name: <I need gradle version name here! It is 1.0>
      release_name: Release <I need gradle version name here! It is 1.0>
      body: |
        Release body
      draft: false
      prerelease: false

How can I get the gradle versionName in the GitHub Actions yml file to create a GitHub release?

SayMyName
  • 461
  • 5
  • 17
  • Does this answer your question? [Read versionName from build.gradle in bash](https://stackoverflow.com/questions/35475432/read-versionname-from-build-gradle-in-bash) – Grzegorz Krukowski Feb 12 '22 at 12:33

2 Answers2

13

A path i took used gradle tasks.

In your module build.gradle, where version name is a simple ext property for gradle that you can set as a variable that can be used through out the gradle file.

task printVersionName {
  println project.ext.version_name
}

In your github actions .yaml file add these steps.

- name : Retrieve Version
  run: |
   echo "::set-output name=VERSION_NAME::$(${{github.workspace}}/gradlew -q printVersionName)"
  id: android_version

Following step is used to save it to a environmental variable that can be used through out the workflow file.

- name: Get version
  run: |
    echo "version_name=${{steps.android_version.outputs.VERSION_NAME}}" >> $GITHUB_ENV

You can then reference the value through out your workflow file with

${{env.version_name}}
Danuofr
  • 1,661
  • 19
  • 25
  • 1
    Correct Answer, This worked for me. I just logged In StackOverFlow to upvote this answer :) – DkPathak Aug 12 '22 at 04:42
  • It worked, but set-output is deprecated now, the suggested way is to use: echo "VERSION_NAME=$(${{github.workspace}}/gradlew -q printVersionName)" >> $GITHUB_OUTPUT and but it doesn't seem to work, can anyone help? – Deepak Joshi Dec 29 '22 at 20:16
  • Getting error: Unable to process file command 'output' successfully, Invalid format 'Iterating variant: app-debug' – Deepak Joshi Dec 29 '22 at 20:18
-1

Actually you can merge Retrieve and Get together as one.

# Set VERSION_INFORMATION
- run: echo "VERSION_INFORMATION=$(${{github.workspace}}/gradlew -q printVersionInformation)" >> $GITHUB_ENV

For your build.gradle(:app), we can use.

task printVersionInformation {
     println "v" + defaultConfig.versionName + " (" + defaultConfig.versionCode + ")"
}

Then, in your output. It remains the same.

${{ env.VERSION_INFORMATION }}

Then, you should able to get the output of.

v1.0.0 (0)
Morgan Koh
  • 2,297
  • 24
  • 24