How to enable github actions access gradle properties?

Viewed 3900

My android project uses some keys in gradle properties ~/.gradle/gradle.properties. This is intentionally ignored by git. Is there a way to let github actions access these properties?

3 Answers

According to this page, there are three places one can have a gradle.properties file, and one of them includes the project's root folder.

If you needed gradle.properties in github actions, then create one in the root folder of your project and commit to git. The one in your home directory should remain there.


If it is really your desire not to commit any gradle.properties file to git, my first question would be Why?

Here is another way to do it using secrets.

Assuming you called the secret GRADLE_PROPERTIES, then you can do something like this in one of your steps:

steps:
  - uses: actions/checkout@v2
  - name: Restore gradle.properties
    env:
      GRADLE_PROPERTIES: ${{ secrets.GRADLE_PROPERTIES }}
    shell: bash
    run: |
      mkdir -p ~/.gradle/
      echo "::set-env name=GRADLE_USER_HOME::$HOME/.gradle"
      echo ${GRADLE_PROPERTIES} > ~/.gradle/gradle.properties

After this step runs, gradle will now use that file to configure itself, and so will your project.

2021 solution

We need to use $GITHUB_ENV environment variable to store GRADLE_USER_HOME because the old ::set-env is depecated.

Here is one sample workflow:

name: build
on: [ push ]
jobs:
  build-app:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the code
        uses: actions/checkout@v2
      - name: Restore gradle.properties
        env:
          GRADLE_PROPERTIES: ${{ secrets.GRADLE_PROPERTIES }}
        shell: bash
        run: |
          mkdir -p ~/.gradle/
          echo "GRADLE_USER_HOME=${HOME}/.gradle" >> $GITHUB_ENV
          echo "${GRADLE_PROPERTIES}" > ~/.gradle/gradle.properties
      - name: Build the app
        run: ./gradlew build
Related