如何在 gitlab CI 中签署 APK 并将发布发送到 slack

时间:2021-02-23 09:34:04

标签: continuous-integration gitlab

我想构建一个 android 签名的 APK 并通过 slack 通道接收发布 APK。尝试了下面的脚本,但由于我的应用程序是用 JDK 8 编写的,它不起作用。

这是我使用的脚本。

image: jangrewe/gitlab-ci-android

cache:
  key: ${CI_PROJECT_ID}
  paths:
    - .gradle/

before_script:
  - export GRADLE_USER_HOME=$(pwd)/.gradle
  - chmod +x ./gradlew

stages:
  - build

assembleDebug:
  stage: build
  only:
    - development
    - tags
  script:
    - ./gradlew assembleDebug
    - |
      curl \
        -F token="${SLACK_CHANNEL_ACCESS_TOKEN}" \
        -F channels="${SLACK_CHANNEL_ID}" \
        -F initial_comment="Hello team! Here is the latest APK" \
        -F "file=@$(find app/build/outputs/apk/debug -name 'MyApp*')" \
        https://slack.com/api/files.upload
  artifacts:
    paths:
      - app/build/outputs/apk/debug
view raw

但是显示了一些未找到的 Java 类。 (Java 11 中已弃用的 Java 文件)

1 个答案:

答案 0 :(得分:0)

首先,您需要设置 slack 身份验证密钥。

  1. 在 Slack 中创建应用
  2. 转到身份验证部分并生成身份验证密钥。
  3. 获取您想要接收消息的频道 ID。
  4. 在您的 slack 线程中提及您的应用名称并将应用添加到频道。
  5. 在您的 GitLab ci 设置变量中设置这些键。

SLACK_CHANNEL_ACCESS_TOKEN = Slack 应用生成的访问令牌

SLACK_CHANNEL_ID = 频道 ID(检查频道 ID 的 URL 最后部分)

8. 将您现有的 Keystore 文件复制到存储库。 (如果您的项目是私有的,请执行此操作。) 7.将GitLab脚本的内容改为如下代码。

请务必更改证书密码、密钥密码和别名。

image: openjdk:8-jdk

variables:

  # ANDROID_COMPILE_SDK is the version of Android you're compiling with.
  # It should match compileSdkVersion.
  ANDROID_COMPILE_SDK: "29"

  # ANDROID_BUILD_TOOLS is the version of the Android build tools you are using.
  # It should match buildToolsVersion.
  ANDROID_BUILD_TOOLS: "29.0.3"

  # It's what version of the command line tools we're going to download from the official site.
  # Official Site-> https://developer.android.com/studio/index.html
  # There, look down below at the cli tools only, sdk tools package is of format:
  #        commandlinetools-os_type-ANDROID_SDK_TOOLS_latest.zip
  # when the script was last modified for latest compileSdkVersion, it was which is written down below
  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

# Make Project
assembleDebug:
  interruptible: true
  stage: build
  only:
    - tags
  script:
    - ls
    - last_v=$(git describe --abbrev=0 2>/dev/null || echo '')
    - tag_message=$(git tag -l -n9 $last_v)
    - echo $last_v
    - echo $tag_message
    - ./gradlew assembleRelease
      -Pandroid.injected.signing.store.file=$(pwd)/Certificate.jks
      -Pandroid.injected.signing.store.password=123456
      -Pandroid.injected.signing.key.alias=key0
      -Pandroid.injected.signing.key.password=123456
    - |
      curl \
        -F token="${SLACK_CHANNEL_ACCESS_TOKEN}" \
        -F channels="${SLACK_CHANNEL_ID}" \
        -F initial_comment="$tag_message" \
        -F "file=@$(find app/build/outputs/apk/release -name 'app*')" \
        https://slack.com/api/files.upload

  artifacts:
    paths:
      - app/build/outputs/
相关问题