跳转至正文
CI/CD

使用 Gitlab 实现自动化 build 与 release

使用 GitLab 免费构建专属的 CI/CD pipeline,每次 push 到 main 分支即可自动部署应用。

使用 Gitlab 实现自动化 build 与 release

本教程以 GitLab CI 为核心,但稍作调整即可适配任何其他 CI/CD 平台。

前言

请确保已先将应用添加到 Capgo,本教程仅侧重于 upload 阶段

Commit 规范

首先,你需要开始遵循 commit 规范 约定式提交 (conventional commits)` 这将帮助工具链理解如何升级版本号,学会它只需 5 分钟。

约定式提交 (Conventional commits)

基于 Tag 的 GitLab CI

接着,你需要创建第一个 GitLab 项目,以实现自动构建并生成 Tag。

在此路径下创建文件: .github/workflows/bump_version.yml

内容如下:

name: Bump version

on:
  push:
    branches:
      - main

jobs:
  bump-version:
    if: "!startsWith(github.event.head_commit.message, 'chore(release):')"
    runs-on: ubuntu-latest
    name: "Bump version and create changelog with standard version"
    steps:
      - name: Check out
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          filter: blob:none
          token: '${{ secrets.PERSONAL_ACCESS_TOKEN }}'
      - name: Git config
        run: |
          git config --local user.name "github-actions[bot]"
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
      - name: Create bump and changelog
        run: npx capacitor-standard-version
      - name: Push to origin
        run: |
          CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
          remote_repo="https://${GITHUB_ACTOR}:${{ secrets.PERSONAL_ACCESS_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git"
          git pull $remote_repo $CURRENT_BRANCH
          git push $remote_repo HEAD:$CURRENT_BRANCH --follow-tags --tags

这将为 main 分支的每次 commit 发布一个 tag,并在其中为 main 分支的每次 commit 添加 changelog 条目: CHANGELOG.md.

如果你没有这个文件,别担心,它会自动为你创建。

要实现此功能,请创建一个 个人访问令牌 (PERSONAL ACCESS TOKEN) 并将其作为以下内容添加到您的 GitLab CI/CD 变量中: PERSONAL_ACCESS_TOKEN.

这是为了让 CI 能够提交 changelog。

创建 token 时,请将过期时间设置为 never 并将 scope 设置为 repo.

最后,为了让工具识别版本保存位置,您需要创建文件 .cz.toml 并将其放在仓库根目录下。

并在其中添加以下内容:

[tool.commitizen]
name = "cz_conventional_commits"
tag_format = "$major.$minor.$patch$prerelease"
version = "0.11.5"
version_files = [
    "package.json:version",
    ".cz.toml"
]

请确保此文件中的版本号与您的 package.json 文件。

这仅在第一次时需要,之后工具会自动保持更新。

现在你可以提交这两个文件,并在 GitHub 中看到你的第一个 tag 了!

GitHub 的构建 action

在此路径下创建文件: .github/workflows/build.yml

内容如下:

name: Build source code and send to Capgo

on:
  push:
    tags:
      - '*'
      
jobs:
  deploy:
    runs-on: ubuntu-latest
    name: "Build code and release"
    steps:
      - name: Check out
        uses: actions/checkout@v6
      - name: Install dependencies
        id: install_code
        run: npm i
      - name: Build
        id: build_code
        run: npm run build
        env: # Remove both lines  if you don't need it
          FIREBASE_CONFIG: ${{ secrets.FIREBASE_CONFIG }} # Example of env var coming from a secret
      - name: Create Release
        id: create_release
        run: npx @capgo/cli@latest bundle upload -a ${{ secrets.CAPGO_TOKEN }} -c production

这会在发送到 Capgo 之前,先安装并构建你的依赖。

如果你的构建命令不同,可以在 build_code 步骤中修改。

要让其生效,你需要获取用于 Capgo 的 API key,并将其添加到 你的 GitHub 仓库的 secret 中, 命名为 CAPGO_TOKEN.

现在你可以提交这两个文件,并在 GitHub 中看到你的第一个 tag 了!

添加该 commit 后,会为 production channel 生成新的 build。

你应该在 build 步骤中添加测试,以确保你的 code 运行正常。

前往你的 Capgo dashboard 查看刚刚生成的 build,你现在已经拥有了自己的 CI/CD 系统。

如果你希望所有用户在有更新时都能收到,请前往你的 channel 并将其设置为 public.

继续阅读:使用 GitLab 自动构建与发布

如果你正在使用 使用 Gitlab 实现自动化 build 与 release 规划 CI/CD 自动化,并将其与 使用 Capgo 实现 CI/CD 用于 Capgo CI/CD 中的产品工作流, Capgo Native Build 用于 Capgo Native Builds 中的产品工作流, Capgo 集成 用于 Capgo 集成中的产品工作流, CI/CD 集成 了解 CI/CD 集成的实现细节,以及 GitHub Actions 集成 有关 GitHub Actions 集成的实现细节。

为 Capacitor 应用提供 Live Updates

当 Web 层出现线上 Bug 时,通过 Capgo 发布修复,而不用等 App Store 审核好几天。用户会在后台收到更新,原生变更则仍走正常审核流程。

博客最新动态

Capgo 为你提供打造真正专业的移动应用所需的最有价值的洞察。