跳过正文
  1. 云原生AI技术/

Devops之CI三种触发构建

·762 字·2 分钟·
Kubernetes Ci Build Auto
古德
作者
古德
目录

1.自动化构建
#

  • 通过在需要自动化的步骤,指定rules对应commit分支即可,自动触发该步骤
stages:
  - build_docker

build_docker:
  stage: build_docker
  image:
    name: ${KANIKO_IMAGE}
  script: |
    echo "自动化构建"    
  rules:
  - if: '$CI_COMMIT_REF_NAME == "master"'

2. 半自动化构建(参考友人方案)
#

  • 设定好需要执行的commit分支以及触发半自动化构建的变量及值
    • 该变量RUN_PIPELINE初始化值为false
variables:
  RUN_PIPELINE: 'false'

stages:
  - build_docker

build_docker:
  stage: build_docker
  image:
    name: ${KANIKO_IMAGE}
  script: |
    echo "自动化构建"    
  rules:
  - if: '$CI_COMMIT_REF_NAME == "dev" && $RUN_PIPELINE == "true"'
  • 在项目根目录下创建git-push-ci.sh文件
#!/bin/bash

# 检查当前分支是否为 dev
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" != "dev" ]; then
    echo "Error: This script should only be run on the dev branch."
    exit 1
fi

# 设置 CI 变量并推送
git push origin dev -o ci.variable="RUN_PIPELINE=true"

echo "Pushed to dev branch with pipeline trigger."
  • 在全局用户git配置末尾添加
    • windows默认路径:C:\Users\Administrator\.gitconfig
[alias]
  push-ci = "!sh ./git-push-ci.sh"
  • 提交代码时,使用git push-ci代替git push,即可自动触发CICD流程
  • 提交代码时,仍使用git push,则不触发CICD流程

3. 手动构建
#

  • 通过在需要手动执行的步骤,指定rules对应参数
stages:
  - build_docker

build_docker:
  stage: build_docker
  image:
    name: ${KANIKO_IMAGE}
  script: |
    echo "手动构建"    
  rules:
  - if: '$EXECUTE_MODE == "manual"' 
  • 在gitlab界面,手动点Run pipeline按钮进去
    image.png
  • 选择需要执行的分支,输入上一步定义变量及值
    image.png
  • 即可手动执行CICD流程

4. 推送代码时通过命令行传递参数
#

  • 在项目根目录下创建git-push-ci.sh文件
#!/bin/bash

# 检查当前分支是否为 dev
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" != "dev" ]; then
    echo "Error: This script should only be run on the dev branch."
    exit 1
fi

# 设置 CI 变量并推送
git push origin dev -o ci.variable="RUN_PIPELINE=true" -o ci.variable="DEFINE_NAME=$1"

echo "Pushed to dev branch with pipeline trigger."
  • 在全局用户git配置末尾添加
    • windows默认路径:C:\Users\Administrator\.gitconfig
    • $1: 传参占位符,可定义多个
[alias]
  push-ci = "!sh ./git-push-ci.sh $1"
  • 本地推送代码时,添加参数
    • 则变量DEFINE_NAME的值被赋予t1传递到CICD的构建当中去
git push-ci t1

相关文章

Devops之gitlab-ci部署
·2456 字·5 分钟
Kubernetes Ci Build Gitlab-Runner
通过kaniko在容器中构建镜像
·1171 字·3 分钟
Kubernetes Docker Build Kaniko
Devops之ArgoCD部署
·2118 字·5 分钟
Kubernetes Cd Devops Argocd
批量删除卸载docker遗留的大量挂载路径
·177 字·1 分钟
Kubernetes Docker Mount Path
Harbor镜像仓库迁移
·205 字·1 分钟
Kubernetes Harbor Backup Image
kserve安装使用
·1825 字·4 分钟
Kubernetes Cloud Machinelearning Ai