iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
Mobile Development

一天一腳步,30天學會 Android Compose UI!系列 第 23

Day 23 Git → Gitlab-CI 超簡單

會講這個題目其實有點微妙,但是都講完了測試,卻不講 CI 我覺得好像少了一點什麼。

不知道 Android App 工程師,到底要不要自己維護 CI呢?我覺得需要。

所以我就寫了一篇自己簡單搞定自己的 CI 的文章。

不過維護 CI 之前要先學會基礎的 Git 使用,

Git 教學這方面的文網路上也有許多很好的學習資源,

如下:


假設你已經學習會了 Git,那我們怎麼樣一天內搞定 CI呢?

我自己有接觸過 Gitalb, Bitbucket, Github,其實大多數的 CI 都差不多,

我自己覺得 Gitlab 是最熟悉的所以我就講 Gitlab,其他的有機會再介紹囉~

然後因為我同時也在 GDG Taipei 社群服務,稍微推廣一下 GCP 的服務,哈哈!

因為真的很好用啊!不信等等看,那就開始講解囉~


首先我們將一個本地確定可以執行通過的專案,上傳到Gitlab上,如下圖:

Untitled

Untitled

然後點畫面上的 new file

選擇 .gitlab-ci.yml 的 Android

Untitled

Untitled

就會出現Gitlab 預設的 Android CI yaml setting了,通常來說這個 settting 就可以執行完 CI !

Untitled

可以點到 CI/CD 這個頁面,就能看到CI正在執行了!

執行好後的畫面如下!

Untitled

怎麼沒成功QQ

沒關係,我們先回頭來看 究竟 gitlab-CI的 yaml檔裡面是講些什麼~

image: openjdk:8-jdk

variables:

  #(ANDROID COMPILE SDK版本號碼)
  ANDROID_COMPILE_SDK: "29"

  #(ANDROID BUILD TOOLS版本號碼)
  ANDROID_BUILD_TOOLS: "29.0.3"

  #(SDK 版號)
  #ANDROID_SDK_TOOLS: "6514223"

# Packages installation before running script
before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1

  # Setup path as android_home for moving/exporting the downloaded sdk into it
  - export ANDROID_HOME="${PWD}/android-home"
  # Create a new directory at specified location
  - install -d $ANDROID_HOME
  # Here we are installing androidSDK tools from official source,
  # (the key thing here is the url from where you are downloading these sdk tool for command line, so please do note this url pattern there and here as well)
  # after that unzipping those tools and
  # then running a series of SDK manager commands to install necessary android SDK packages that'll allow the app to build
  - wget --output-document=$ANDROID_HOME/cmdline-tools.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
  # move to the archive at ANDROID_HOME
  - pushd $ANDROID_HOME
  - unzip -d cmdline-tools cmdline-tools.zip
  - popd
  - export PATH=$PATH:${ANDROID_HOME}/cmdline-tools/tools/bin/

  # Nothing fancy here, just checking sdkManager version
  - sdkmanager --version

  # use yes to accept all licenses
  - yes | sdkmanager --sdk_root=${ANDROID_HOME} --licenses || true
  - sdkmanager --sdk_root=${ANDROID_HOME} "platforms;android-${ANDROID_COMPILE_SDK}"
  - sdkmanager --sdk_root=${ANDROID_HOME} "platform-tools"
  - sdkmanager --sdk_root=${ANDROID_HOME} "build-tools;${ANDROID_BUILD_TOOLS}"

  # Not necessary, but just for surity
  - chmod +x ./gradlew

#上面這一段都是在描述如何在Git runner 下載指定的 Android SDK 跟對應的 compile 工具

# 執行 Check Linting 
# Basic android and gradle stuff
# Check linting
lintDebug:
  interruptible: true
  stage: build
  script:
    - ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint

# Compile Android APK
# Make Project
assembleDebug:
  interruptible: true
  stage: build
  script:
    - ./gradlew assembleDebug
  artifacts: // 執行完後將這個資料夾的檔案上傳至Gitlab
    paths:
      - app/build/outputs/

## 執行 android test
# Run all tests, if any fails, interrupt the pipeline(fail it)
debugTests:
  interruptible: true
  stage: test
  script:
    - ./gradlew -Pci --console=plain :app:testDebug

看完以後,再點進去這裡面看一下為什麼執行失敗。

Untitled

Untitled

原來是現在 Android project 的要求 Java 要用11以上了~,那就改一下吧!

點擊 File 打開.gitlab-ci.yaml檔

Untitled

Untitled

將8-jdk改成11-jdk,然後就等他再次執行

Untitled

登愣!完成。

如果今天這個也會了,那我們就明天見囉:)

值得一提的是目前Gitlab 的免費額度是一個月可以執行 400分鐘,
有超過這個需求的話,就要自行安裝Git-runner或是購買!

https://about.gitlab.com/pricing/

本文同步發表在 Medium 上:文章連結


上一篇
Day 22 Android Unit Test
下一篇
Day 24 Compose UI Test
系列文
一天一腳步,30天學會 Android Compose UI!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言