GitHub Release for Android using Actions

GitHub Release for Android using Actions

Releasing an Android app on GitHub as part of an open-source project can be a time-consuming and repetitive task. However, by using GitHub Actions to set up CI/CD pipelines, you can simplify the process and automate many of the steps involved.

GitHub Actions is a powerful tool that enables you to automate tasks and workflows for your GitHub repositories. You can create custom workflows using pre-built actions, or write your own using any programming language.

To release an Android app on GitHub using GitHub Actions, you can follow these simple steps:


Create Workflow Files

To create workflow files, first, create a .github folder at the root of your project. Inside this folder, create another folder named workflows, where you can create build and release-related files.

The first goal is to build and sign the app and then upload the build artifacts. To achieve this, create a build.yml file that uses actions like sign-android-release, and upload-artifact.

name: Android Build
	

	on:
	  workflow_dispatch:
	jobs:
	  build:
	    runs-on: ubuntu-latest
	    steps:
	      # 1
	      - name: Checkout code
	        uses: actions/checkout@v2
	      # 2
	      - name: Generate Release APK
	        run: ./gradlew assembleFossRelease
	      # 3
	      - name: Sign APK
	        uses: r0adkll/sign-android-release@v1
	        # ID used to access action output
	        id: sign_app
	        with:
	          releaseDirectory: app/build/outputs/apk/foss/release
	          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
	          alias: ${{ secrets.ALIAS }}
	          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
	          keyPassword: ${{ secrets.KEY_PASSWORD }}
	      # 4
	      - uses: actions/upload-artifact@master
	        with:
	          name: release.apk
	          path: ${{steps.sign_app.outputs.signedReleaseFile}}
	      # 5
	      - uses: actions/upload-artifact@master
	        with:
	          name: mapping.txt
	          path: app/build/outputs/mapping/release/mapping.txt        

Next, create a publish.yml file that is responsible for releasing the built artifacts on GitHub Release. The release tag is generated in a date format, but you can provide your own label if you prefer. This flow checks for build flow success and uses the available artifact for release.

name: Github Publish
	
	on:
	  workflow_dispatch:
	jobs:
	  build:
	    runs-on: ubuntu-latest
	    steps:
	      # 1
	      - uses: dawidd6/action-download-artifact@v2
	        with:
	          github_token: ${{secrets.GITHUB_TOKEN}}
	          workflow: build.yml
	          workflow_conclusion: success
	          branch: floss
	          name: release.apk
	          path: downloads
	          if_no_artifact_found: fail
	      # 2
	      - name: Generate release tag
	        id: tag
	        run: |
	          echo "::set-output name=release_tag::APP_$(date +"%Y.%m.%d_%H-%M")"
	      # 3
	      - name: Release
	        uses: softprops/action-gh-release@v1
	        env:
	          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
	        with:
	          tag_name: ${{ steps.tag.outputs.release_tag }}
	          files: downloads/release-signed.apk        

If the generated APK file by your Gradle is different from release.apk, update the last line downloads/release-signed.apk with downloads/filename_signed.apk. These workflow files will run on manual workflow dispatch, but you can choose different triggers like push or pull requests.


Keystore file Encode

Before you can sign the release app, you need to have a keystore file, which should be securely stored and encoded before use in GitHub secrets. I recommend using OpenSSL to encode the file, as other tools may result in issues while decoding. This step is important to ensure that the keystore file remains safe and protected while being used in your GitHub Actions workflow.

openssl base64 < signing_keystore.jks | tr -d '\n' | tee signing_keystore_base64.txt        

For window OS you have to install OpenSSL.


Add Github secrets

to the settings tab of your repository and click on the "secrets and variables" tab in the left menu section. you will see a section as shown in this image:

No alt text provided for this image

Add all the required secrets mentioned in your files: SIGNING_KEY, ALIAS, KEY_STORE_PASSWORD, and KEY_PASSWORD.

No alt text provided for this image

Using Actions

Now that your workflow files are set up and the secrets are added, you're ready to use GitHub Actions to build and release your Android app. To get started, push all the code to your repository, making sure to push it to the main or master branch for the first time. This is important because actions won't be visible in other branches until they are merged into the main branch.

Inside the "Actions" tab, you can find two actions: "Android Build" and "GitHub Release". Run the Android Build first. If the result is a success then you can proceed with the GitHub Release action.


No alt text provided for this image

If everything works as expected, a release tag will be added to your repository, and you'll be able to see the release in the "Releases" section of your repository, as shown in the image.

No alt text provided for this image


Conclusion

Using GitHub Actions to build and release your Android app is a great way to automate the process and save time. By following the steps outlined in this guide, even new developers can easily set up their own CI/CD workflow and streamline the app delivery process.

Helpful links


If you're looking for a real-life example of how to implement the workflow discussed in this guide, check out Sensify, an open-source project built using Jetpack Compose.


要查看或添加评论,请登录

社区洞察

其他会员也浏览了