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

How to sign APK in gitlab CI and send release to slack

我想构建一个 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 文件在 Java 11 中弃用)

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

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

SLACK_CHANNEL_ACCESS_TOKEN = Slack App 生成的访问令牌

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

8.Copy 您现有的 Keystore 文件到存储库。 (如果您的项目是私有的,请执行此操作。) 7.Change 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/