How to get version name from Android Gradle file in GitHub Actions?

Viewed 857

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?

1 Answers

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}}
Related