­

Artifactory & GitLab CI持续集成实践

  • 2020 年 2 月 19 日
  • 筆記

GitLab CI支持创建多个构建,并评估每次代码提交是否通过测试和以及对您产品的影响。在构建过程中,会生成大量二进制文件,如果不能正确的大规模管理这些文件,就会导致二进制文件管理混乱。为了克服这个问题,Artifactory被无缝地集成到GitLab CI构建过程中,以便更好的发布和管理这些二进制文件,并通过JFrog CLI, GitLab CI缓存、发布您的依赖包、制品包和构建信息到Artifactory。

这篇文章描述了如何将 GitLab CI 与 Artifactory 集成在一起,不仅可以解析和部署二进制文件,还可以从 Artifactory 的 Build Integration 功能中获取更多帮助。

将 Artifactory 与 GitLab CI 集成后,您可以存储和查看以下信息:

· 构建信息和发布的模块

· 使用的依赖

· 环境变量

· 许可证摘要

· 链接到您的 Jira issue

· 构建之间的差异

一、 环境配置

· 安装Gitlab Runner并配置Gitlab (此处不再赘述)

· 准备一个示例项目

https://gitlab.com/guoyunzong/maven-example.git

· Artifactory 中创建仓库(2 local,1 remote,1 virtual):maven-dev-local、maven-pro-local、maven-remote、maven-virtual

· 在项目目录下编写配置文件 (maven.conf)

version: 1  type: maven  resolver:   snapshotRepo: maven-virtual   releaseRepo: maven-virtual   serverID: Default-Server  deployer:   snapshotRepo: maven-virtual   releaseRepo: maven-virtual   serverID: Default-Server  在项目目录下编写配置文件 (jira-cli.conf)  version: 1  issues:   serverID: Default-Server   trackerName: JIRA   regexp: (.+-[0-9]+)s-s(.+)   keyGroupIndex: 1   summaryGroupIndex: 2   trackerUrl: http://my-jira.com/issues   aggregate: true   aggregationStatus: RELEASED  · 在gitlab中配置artifactory的环境变量,Settings—CI/CD--Variables ,如:  ARTIFACTORY_URL  http://192.168.230.32:8081/artifactory  ARTIFACTORY_USER admin  ARTIFACTORY_PASS password  MAVEN_REPO_KEY  maven-virtual

二、编写 Gitlab CI 脚本并执行构建

· 在项目目录下编写脚本(.gitlab-ci.yml)

image: docker:git  services:  - docker:dind  stages:  - build  build:   image: maven:3.5.4-jdk-8-alpine   stage: build   script:   # Install      - apk add git   # Set the M2_HOME environment variable      - export M2_HOME=/usr/share/maven   # Download 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   # Mvn clean install      - ./jfrog rt mvn "clean install" maven.conf --build-name=gitlabci-maven-artifactory --build-number=$CI_JOB_ID   # Collect the environment variables      - ./jfrog rt bce gitlabci-maven-artifactory $CI_JOB_ID   # Add jira issue      - ./jfrog rt bag gitlabci-maven-artifactory $CI_JOB_ID --config jira-cli.conf   # Add sonar(optional)      - ./jfrog rt sp "maven-dev-local/org/jfrog/test/multi3/3.7-SNAPSHOT/*.war" "qulity.gate.sonarUrl=http://192.168.230.156:9000/dashboard/index/gitlabci-maven-artifactory"   # Add properties(optional)      - ./jfrog rt sp "maven-dev-local/org/jfrog/test/multi3/3.7-SNAPSHOT/*.war" "deploy.tool=ansible"      - ./jfrog rt sp "maven-dev-local/org/jfrog/test/multi3/3.7-SNAPSHOT/*.war" "ip=127.0.0.1"   # Pass the build information to Artifactory         - ./jfrog rt bp gitlabci-maven-artifactory $CI_JOB_ID   # Promote      - ./jfrog rt bpr gitlabci-maven-artifactory $CI_JOB_ID maven-pro-local   # Xray scan(optional)      - ./jfrog rt bs gitlabci-maven-artifactory $CI_JOB_ID --fail=false   # Download(optional)      - ./jfrog rt dl maven-dev-local/org/jfrog/test/multi3/3.7-SNAPSHOT/multi3-3.7-20191213.050538-8.war all-my-frogs/   when: manual  · 提交代码,输入git commit message,格式如下  HAP-1007 - This is a sample issue

· 执行构建(可配置手动或自动执行)

CI/CD–Pipelines

· Job中查看构建输出

· artifactory中的issue信息(可点击 HAP-1007 链接至 Jira 地址)