GitLab CI 和 Artifactory: 各就各位、预备、构建!

GitLab CI and Artifactory

将 JFrog Artifactory 集成到您的 GitLab CI 工作流中,使 JFrog 的“快速发布 谁与争锋”愿景成为现实。
这篇博文描述了如何将 GitLab CI 与 Artifactory 集成,集成后不仅可以解析和部署二进制文件,还可以从 Artifactory 的
构建集成功能中获益。

发布和管理您的程序包

GitLab CI 支持创建多个构建,并评估每个提交如何通过测试并影响您的产品。 在构建过程中,会生成大量二进制文件,如果不能在这么大的规模下对其进行正确管理,可能会导致二进制文件损坏。 为了解决这个问题,可将 Artifactory 无缝集成到您的构建过程中,以便发布和管理这些二进制文件。 通过 JFrog CLI,GitLab CI 能够获取、缓存和发布您的依赖项和创建的程序包,并将构建信息发布到 Artifactory。

Artifactory 的构建集成功能可以自动将每个生成的程序包链接到其相关的构建、依赖项和信息,并收集一组您可能到目前为止还没有从中获益的新的元数据。 将 Artifactory 与 GitLab CI 集成后,您可以存储和查看如下信息:

  • 构建信息和已发布的模块
  • 使用的依赖项
  • 环境变量
  • 许可证摘要
  • 指向问题跟踪工具的链接
  • 不同构建之间的区别

数据存储在 Artifactory 数据库中,因此,您还可以使用 Artifactory 查询语言检索数据。

将 Artifactory 嵌入 GitLab CI 工作流

下面的分步示例演示了如何将 Artifactory 嵌入您的 GitLab CI 工作流。 在本例中,我们将使用 Maven。 GitHub 中提供了此分步示例和其他程序包类型示例。

  1. 在 GitLab 中新建一个项目。
  2. 将此 Maven 项目示例复制到您的 GitLab 仓库。
  3. 在 Maven 项目中,在“CI/CD 设置”>“机密变量”下,配置以下 Artifactory 凭据。
    ARTIFACTORY_URL: https://artifactory.mycompany.com/artifactory
    ARTIFACTORY_USER: admin
    ARTIFACTORY_PASS: password
    MAVEN_REPO_KEY: Artifactory 中目标制品库的名称
    GitLab and Artifactory
  4. 将以下 .gitlab-ci.yml 配置文件提交到您的项目的根目录。 此文件是可根据您的需要编辑的自定义模板。
    .gitlab-ci.yml 示例

    # This file is a template, and might need editing before it works on your project.
    image: maven:latest
    before_script:
      # Install JFrog CLI
      -  curl -fL https://getcli.jfrog.io | sh
      # Configure Artifactory instance with JFrog CLI
      - ./jfrog rt config --url=$ARTIFACTORY_URL --user=$ARTIFACTORY_USER --password=$ARTIFACTORY_PASS
      - ./jfrog rt c show
      # Set the M2_HOME environment variable
      - export M2_HOME=/usr/share/maven
      # Replace the repository name in the configuration.yml to the correct one.
      - sed -i 's,MAVEN_REPO_KEY,'"$MAVEN_REPO_KEY"',g' configuration.yml
    build:
      script:
        # Run the MVN command
        - ./jfrog rt mvn "clean install" configuration.yml --build-name=gitlabci-maven-artifactory --build-number=$CI_JOB_ID
        # Collect the environment variables
        - ./jfrog rt bce gitlabci-maven-artifactory $CI_JOB_ID
        # Pass the build information to Artifactory
        - ./jfrog rt bp gitlabci-maven-artifactory $CI_JOB_ID
      only:
        - master
    

    在提交配置文件后,将自动触发即时构建,并且您的 Maven 程序包和缓存的依赖项将发布到 Artifactory。

  5. 在 GitLab CI UI 中导航到相关作业,查看构建过程和日志。GitLab Job View
  6. 登录 Artifactory 并导航到制品库浏览器,查看您在 Artifactory 中发布的制品。
    GitLab and Artifactory
  7. 导航到构建浏览器,查看相关构建信息。
    GitLab and Artifactory
    就这样! 大功告成。