Gitlab CI/CD(一)

70 阅读2分钟

CI/CD 的说明

CI/CD is a continuous method of software development, where you continuously build, test, deploy, and monitor iterative code changes.

实践记录

  1. 创建Project gitlabcicdstudy
  2. 在项目根目录添加.gitlab-ci.yml文件
  3. 安装Gitlab-runner到本地机器(如果使用gitlab.com,则不需要进行安装)
  • 下载gitlab-runner

Intel

sudo curl --output /usr/local/bin/gitlab-runner "https://s3.dualstack.us-east-1.amazonaws.com/gitlab-runner-downloads/latest/binaries/gitlab-runner-darwin-amd64"

Apple Silicon

sudo curl --output /usr/local/bin/gitlab-runner "https://s3.dualstack.us-east-1.amazonaws.com/gitlab-runner-downloads/latest/binaries/gitlab-runner-darwin-arm64"
  • 授予执行权限
sudo chmod +x /usr/local/bin/gitlab-runner
  • 安装gitlab-runner 作为一个服务,并启动
gitlab-runner install
gitlab-runner start 
  • 注册runner到项目,选择「shell」executor
gitlab-runner register
  1. Enter the GitLab instance URL (for example, gitlab.com/):
  2. Enter the registration token:

image.png 3. Enter a description for the runner: CICDStudy Project Build Script(自定义的一段文字)

  1. Enter tags for the runner (comma-separated): macos(自定义的标签,不会影响构建行为,只是在构建脚本里任务可以派发给指定标签的runner 进行执行)
  2. Enter optional maintenance note for the runner: macos for build Android Project(不影响构建行为,只是当前runner 的描述)

完成以上输入后会看到

image.png 6. Enter an executor: docker-autoscaler, shell, parallels, docker, kubernetes, instance, custom, ssh, virtualbox, docker-windows, docker+machine: shell

  1. 完成后,配置保存目录(/Users/lizengqiang.vendor/.gitlab-runner/config.toml)

image.png

内容如下:

image.png

  • 注册完成后,可以在项目的CI/CD 看到一个Runner Online

image.png

编写gitlab-ci.yml

# .gitlab-ci.yml
image: cimg/android:2025.04.1

stages:
  - build

#before_script:
#  # 打印确认环境变量
#  - echo "ANDROID_HOME=$ANDROID_HOME"
#  - echo "PATH=$PATH"
#
#  # 接受所有 SDK licenses
#  - yes | sdkmanager --licenses
#
#  # 安装必须的 SDK & Build Tools(根据项目 targetSdkVersion 修改)
#  - sdkmanager "platform-tools" "platforms;android-34" "build-tools;34.0.0"

build:
  stage: build
  script:
    # 使用 Gradle Wrapper 构建 APK
    - ./gradlew clean assembleRelease --stacktrace
  tags:
    - macos

  # 构建产物上传
  artifacts:
    paths:
      - app/build/outputs/apk/
    expire_in: 1 week

注意项

上面这种方式没有考虑SDK是否存在,需要在本地的.bash_profile 里声明ANDROID_HOME 变量,.zshrc 中声明 gitlab-runner (默认使用的shell 是bash)找不到, 在gitlab-ci.yml 中指定PATH 变量,会导致mkdir 命令都找不到