极狐GitLab CI/CD 如何构建镜像并推送到 azure 镜像仓库?

极狐GitLab 是 GitLab 在中国的发行版,专门面向中国程序员和企业提供企业级一体化 DevOps 平台,用来帮助用户实现需求管理、源代码托管、CI/CD、安全合规,而且所有的操作都是在一个平台上进行,省事省心省钱。可以一键安装极狐GitLab,详情可以参考极狐GitLab 下载安装官网

GitLab 中文版学习资料

azure 有自己的容器镜像仓库服务 acr(azure container registry)。用户可以构建容器镜像之后推送到 acr 中。当然,还可以直接借助极狐GitLab CI/CD 实现容器镜像构建和推送的自动化。下面演示如何使用极狐GitLab CI/CD 构建容器镜像并自动推送到 acr 中。

创建 ACR 服务

首先,需要在 azure 上创建一个 acr 服务。创建成功之后,可以看到对应的信息:

镜像仓库的登录地址为jihugitlab.azurecr.cn。点击页面上的 Push an Image 可以看到右侧的登录方法:

复制代码
az acr login --name JiHuGitLab
Uppercase characters are detected in the registry name. When using its server url in docker commands, to avoid authentication errors, use all lowercase.
Login Succeeded

之后就可以使用 docker pull、push等命令了:

复制代码
docker pull mcr.microsoft.com/mcr/hello-world
docker tag mcr.microsoft.com/mcr/hello-world jihugitlab.azurecr.cn/samples/hello-world

如果要使用 docker login的方式登录 acr,需要在 Access Key 中找到对应的用户名和密码:

同样可以登录成功:

复制代码
docker login jihugitlab.azurecr.cn
Username: JiHuGitLab
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

和极狐GitLab CI/CD 集成

一般来讲,在极狐GitLab CI/CD 中构建容器镜像的 .gitlab-ci.yml文件内容如下:

复制代码
build:
  image: docker:latest
  stage: build
  services:
    - docker:20.10.7-dind
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE:1.0.0 .
    - docker push $CI_REGISTRY_IMAGE:1.0.0

只需要将环境变量 CI_REGISTRY_USERCI_REGISTRY_PASSWORDCI_REGISTRY修改为 acr 中的数值即可。也就是将这些变量都以环境变量的形式存储到极狐GitLab CI/CD 变量中(项目 --> 设置 --> CI/CD --> 变量):

因此,.gitlab-ci.yml文件中的内容如下:

复制代码
build:
  image: docker:latest
  stage: build
  services:
    - docker:20.10.7-dind
  tags:
    - gitlab
  script:
    - docker login -u "$ACR_REGISTRY_USER" -p "$ACR_REGISTRY_PASSWORD" $ACR_REGISTRY
    - docker build -t $ACR_REGISTRY_IMAGE:1.0.0 .
    - docker push $ACR_REGISTRY_IMAGE:1.0.0

出发 CI/CD 流水线之后,可以查看构建结果:

镜像构建成功,并且推送到了 acr 中,可以在 acr 中查看。在 acr 的 repositories 中看到推送成功的镜像:

高阶体验,用 CI/CD Component 简化构建过程

为了提高 CI/CD 流水线的复用性、易用性,极狐GitLab 从 16.0 引入了 component 功能,16.1 引入了 catalog 功能。对于这两个功能的详细介绍可以参考过往的技术文章极狐GitLab CI/CD Component & Catalog 功能揭秘

对于上面的流程,可以采用直接引用 component 的方式来简化 CI/CD 流水线的构建。在极狐GitLab SaaS(JihuLab.com)上点击搜索或转到 --> 探索 --> CI/CD 目录,可以看到已经发布的 CI/CD Component:

第一个 docker-image-build就是构建容器镜像,并将其推送到镜像仓库的 component:

Component 的实际内容如下:

复制代码
spec:
  inputs:
    stage:
      default: test
    image:
      default: docker:20.10.7-dind
    image_tag:
      default: 1.0.0
    tags:
      default: jh-gitlab


---
component-job-build-image:
  image: $[[ inputs.image ]]
  stage: $[[ inputs.stage ]]
  tags:
    - $[[ inputs.tags ]]
  script:
    - docker login -u "$ACR_REGISTRY_USER" -p "$ACR_REGISTRY_PASSWORD" $ACR_REGISTRY
    - docker build -t $ACR_REGISTRY_IMAGE:$[[ inputs.image_tag ]] .
    - docker push $ACR_REGISTRY_IMAGE:$[[ inputs.image_tag ]]

在项目中的引用方式为:

复制代码
include:  
  - component: jihulab.com/jh-xiaomage-devops/docker-image-build/docker-image-build@5.0.0
    inputs:      
      stage: build      
      image: docker:20.10.7-dind      
      tags: gitlab
      image_tag: 2.0.0

然后触发流水线,直接查看构建结果:

然后在 acr 上查看 tag 为 2.0.0 的镜像是否被推送过去:

2.0.0的镜像已经存在于 acr 中了,说明引用 component 将镜像构建并推送成功。

相关推荐
ClouGence18 小时前
开源数据库管理工具 CloudDM 4.2.0 发布,新增 GoldenDB、KingbaseES 等数据源
数据库·dba·devops
l1t2 天前
DeepSeek总结的DuckDB在 CI 中为 release 和 glibc CLI 构建启用 LTO - #24225
开发语言·数据库·ci/cd·duckdb
天天喝旺仔2 天前
Docker 镜像瘦身实战:多阶段构建把体积缩小 90%
运维·后端·ci/cd·docker·云原生·容器·性能优化
福大大架构师每日一题2 天前
RAGFlow v0.27.1 发布:新增 Azure DevOps、You.com 搜索,修复海量 Bug,性能大幅提升!
bug·azure·devops
研发小能3 天前
需求黑洞、业务与 IT 脱节、外包失控:企业需求管理平台如何破局?
devops
小小测试开发3 天前
RAG评测指标实战:忠实度、上下文精确率/召回率从原理到CI落地
android·人工智能·spring boot·ci/cd
leeyi3 天前
ACP 协议 + devops 可视化:Agent 与编辑器的两条桥(第99篇-E85)
aigc·agent·devops
m0_525724724 天前
端到端 GitOps + 金丝雀流程测评报告
云原生·自动化·k8s·devops
明月_清风4 天前
GitHub Actions 从入门到实战:一文搞懂 CI/CD 自动化
后端·ci/cd·github