Skip to content
coooldoggy.dev

Github Action으로 APK 자동배포하기

Android, Github Action, CI/CD1 min read

Bitriste로 APK 자동 배포하기 에 이어서 Github의 신기능 Github Action으로 안드로이드 APK를 자동빌드하는 것을 만들어 보았다.

Github Action은 사용량에 따라 과금되는데 개인이 사용하는 정도는 무료라고 보면 된다 ,,,

YAML 스크립트로 작성해서 만드는 건데 Bitrise는 workflow가 좀더 유저친화적이고 이해하기 쉬웠다면 코드로만 이루어진 Github Action은 어렵게 느껴질 수 있다. 하지만, Guide에서 하라는 대로 하면 아주 잘된다.. 그리고 사람들이 만들어둔 다양한 tool을 MarketPlace에서 가져다 연동할 수 있다.

Github Action

https://github.com/features/actions

https://docs.github.com/en/free-pro-team@latest/actions

  • 소프트웨어 workflow를 자동화할 수 있도록 도와주는 도구
  • github Repository에 들어오는 이벤트를 Trigger로 사용 가능
  • github marketplace에 있는 여러 기능들 사용 가능
  • 500MB, 2000분/월 무료

Github Action으로 APK 빌드, Slack 메시지 전송하기

Github repository에서 Actions tab 선택

my alternate text

New Workflow 생성

my alternate text

Publish Java Package with Gradle로 workflow 시작

my alternate text

Workflow 이름, Trigger 설정

Trigger option
  • on: [push, pull_request]

  • branches (branch 이름)

1on:
2 # Trigger the workflow on push or pull request,
3 # but only for the main branch
4 push:
5 branches:
6 - main
7 pull_request:
8 branches:
9 - main

master에 push되었을 때 workflow 실행

1name: Deploy
2
3on:
4
5 push:
6
7 branches: master

Java 설치

1- name: Set up JDK 1.8
2 uses: actions/setup-java@v1
3 with:
4 java-version: 1.8
5 server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
6 settings-path: ${{ github.workspace }} # location for the settings.xml file

현재 날짜 구하기

1- name: Get current date
2 id: date
3 run: echo "::set-output name=date::$(date +'%Y-%m-%d')"

APK 생성

1- name: Build APK
2 run: bash ./gradlew assembleRelease --stacktrace

APK Upload

https://github.com/marketplace/actions/upload-a-build-artifact 사용

1- name: Upload APK
2 uses: actions/upload-artifact@v2
3 with:
4 name: app_${{ steps.date.outputs.date }}
5 path: app/release/app-release.apk

Slack Message 전송

https://github.com/marketplace/actions/action-slack 사용

1- name: action-slack
2 uses: 8398a7/action-slack@v3.8.0
3 with:
4 status: ${{job.status}}
5 author_name: 빌드 알림
6 env:
7 SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
1SLACK_WEBHOOK_URL 은 Secret에 등록해야함

Github Repository > Settings > Secrets에 등록

my alternate text

Full code

1# This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created
2
3# For more information see: https://github.com/actions/setup-java#publishing-using-gradle
4
5
6
7
8name: Deploy
9
10on:
11
12 push:
13
14 branches: master
15
16jobs:
17
18 build:
19
20 runs-on: ubuntu-latest
21
22
23
24
25 steps:
26
27 - uses: actions/checkout@v2
28
29 - name: Set up JDK 1.8
30
31 uses: actions/setup-java@v1
32
33 with:
34
35 java-version: 1.8
36
37 server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
38
39 settings-path: ${{ github.workspace }} # location for the settings.xml file
40
41
42
43# - name: Build with Gradle
44# run: ./gradlew build
45
46
47#Debug용 Key Hash
48# - name: get Key Hash
49
50# run: keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64
51
52
53
54 - name: Get current date
55 id: date
56 run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
57
58
59
60 - name: Build APK
61 run: bash ./gradlew assembleRelease --stacktrace
62
63
64
65 - name: Upload APK
66 uses: actions/upload-artifact@v2
67 with:
68 name: app_${{ steps.date.outputs.date }}
69 path: app/release/app-release.apk
70
71
72
73 - name: action-slack
74 uses: 8398a7/action-slack@v3.8.0
75 with:
76 status: ${{job.status}}
77 author_name: 빌드 알림
78 env:
79 SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required