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
The ~/.gradle/gradle.properties is user-specific properties and should stay out of versioning.
If you have keys or any sensitive data you want to share with your Github Actions consider using Encrypted Secrets.
-
It is out of versioning but needed to build the project. Are there no similar ways for gradle properties? May 24, 2020 at 16:33
-
Multiple gradle properties files are merged for a build. You can put properties that are necessary for your CI inside the project gradle properties and version it (
gradle.propertiesin project root directory) but not the~/.gradle/gradle.properties. Please refer to docs.gradle.org/current/userguide/build_environment.html for more details about properties files merging. May 24, 2020 at 16:44 -
Dont want to commit these to the repository and hence did not put them at project level gradle properties. May 24, 2020 at 16:50
-
1Well in this case try writing the properties with an action that runs a shell script like
echo "your.gradle.property=value" >> gradle.propertiesMay 24, 2020 at 18:09 -
For some reason there is no ~/.gradle/gradle.properties file on my Macbook. May 12, 2021 at 17:31