BrDevops单代码库多应用

有些情况一个代码库可能有多个应用,本例将指引如何在BrDevops中使用该代码库。

一、新建应用

本例中创建一个应用,应用名为all-in-one-demo在该应用中会有两个子应用app1app2

图片

二、代码结构

分别在根目录创建两个文件夹app1,app2,并在app1和app2中创建各自的chart。目录结构如下

图片

三、编辑CI文件

先创建一个.gitlab-ci.yml文件,因为有多个子应用,所以设定两个stage分别运行。

image: registry-vpc.cn-zhangjiakou.aliyuncs.com/vinkdong/tinyci:docker-19

stages:
  - build-app1
  - build-app2

build-app1:
  stage: build-app1
  script: 
    - chart_build

build-app2:
  stage: build-app2
  script: 
    - chart_build

.auto_devops: &auto_devops |
  curl -o .auto_devops.sh \
      "${CHOERODON_URL}/devops/ci?token=${Token}"
  if [ $? -ne 0 ];then
    cat .auto_devops.sh
    exit 1
  fi
  source .auto_devops.sh

before_script:
  - *auto_devops

修改app1构建步骤

build-app1:
  stage: build-app1 
  script: 
    - cd app1   # 先进入app1的文件夹中
    - export SUB_APP_NAME=app1 # 设定应用名称为app1 
    - docker build --build-arg commit=${CI_COMMIT_TAG} -t ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME} .  # 构建镜像,镜像版本后添加子应用名称后缀 
    - docker push ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME} # 镜像推
    - chart_build  # 构建镜像
  only:  # 设定只在 app1有修改时触发
    changes:
      - app1/**/*

默认的chart_build不会添加子应用的标识,修改** .auto_devops **手工编写一个覆盖默认方法。

.auto_devops: &auto_devops |
  curl -o .auto_devops.sh \
      "${CHOERODON_URL}/devops/ci?token=${Token}"
  if [ $? -ne 0 ];then
    cat .auto_devops.sh
    exit 1
  fi
  source .auto_devops.sh

  function chart_build(){
      CHART_PATH=`find . -maxdepth 3 -name Chart.yaml`
      helm package ${CHART_PATH%/*} --version ${CI_COMMIT_TAG} --app-version ${CI_COMMIT_TAG}-${SUB_APP_NAME}
      # 通过Choerodon API上传chart包
      curl -X POST \
          -F "token=${Token}" \
          -F "version=${CI_COMMIT_TAG}-${SUB_APP_NAME}" \
          -F "file=@${PROJECT_NAME}-${CI_COMMIT_TAG}-${SUB_APP_NAME}.tgz" \
          -F "commit=${CI_COMMIT_SHA}" \
          -F "image=${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME}" \
          "${CHOERODON_URL}/devops/ci"
      # 判断本次上传是否出错
      if [ $? -ne 0 ]; then
          echo "upload chart error"
          exit 1
      fi
  }

同样的修改app2

build-app2:
  stage: build-app2
  script: 
    - cd app2
    - export SUB_APP_NAME=app2
    - docker build --build-arg commit=${CI_COMMIT_TAG} -t ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME} . .
    - docker push ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME}
    - chart_build
  only:
    changes:
      - app2/**/*

最终的.gitlab-ci.yml文件如下:

image: registry-vpc.cn-zhangjiakou.aliyuncs.com/vinkdong/tinyci:docker-19


stages:
  - build-app1
  - build-app2


build-app1:
  stage: build-app1
  script: 
    - cd app1
    - export SUB_APP_NAME=app1
    - docker build --build-arg commit=${CI_COMMIT_TAG} -t ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME} .
    - docker push ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME}
    - chart_build
  only:
    changes:
      - app1/**/*


build-app2:
  stage: build-app2
  script: 
    - cd app2
    - export SUB_APP_NAME=app2
    - docker build --build-arg commit=${CI_COMMIT_TAG} -t ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME} . .
    - docker push ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME}
    - chart_build
  only:
    changes:
      - app2/**/*


.auto_devops: &auto_devops |
  curl -o .auto_devops.sh \
      "${CHOERODON_URL}/devops/ci?token=${Token}"
  if [ $? -ne 0 ];then
    cat .auto_devops.sh
    exit 1
  fi
  source .auto_devops.sh


  function chart_build(){
      CHART_PATH=`find . -maxdepth 3 -name Chart.yaml`
      helm package ${CHART_PATH%/*} --version ${CI_COMMIT_TAG} --app-version ${CI_COMMIT_TAG}-${SUB_APP_NAME}
      echo "ok";
      
      
      # 通过Choerodon API上传chart包
      curl -X POST \
          -F "token=${Token}" \
          -F "version=${CI_COMMIT_TAG}-${SUB_APP_NAME}" \
          -F "file=@${PROJECT_NAME}-${CI_COMMIT_TAG}-${SUB_APP_NAME}.tgz" \
          -F "commit=${CI_COMMIT_SHA}" \
          -F "image=${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME}" \
          "${CHOERODON_URL}/devops/ci"
      # 判断本次上传是否出错
      if [ $? -ne 0 ]; then
          echo "upload chart error"
          exit 1
      fi
  }

before_script:
  - *auto_devops

可以简化为:

image: registry-vpc.cn-zhangjiakou.aliyuncs.com/vinkdong/tinyci:docker-19


stages:
  - build-app1
  - build-app2


build-app1:
  stage: build-app1
  script: 
    - cd app1
    - export SUB_APP_NAME=app1
    - docker_build
    - chart_build
  only:
    changes:
      - app1/**/*

build-app2:
  stage: build-app2
  script: 
    - cd app2
    - export SUB_APP_NAME=app2
    - docker_build
    - chart_build
  only:
    changes:
      - app2/**/*

.auto_devops: &auto_devops |
  curl -o .auto_devops.sh \
      "${CHOERODON_URL}/devops/ci?token=${Token}"
  if [ $? -ne 0 ];then
    cat .auto_devops.sh
    exit 1
  fi
  source .auto_devops.sh


  function docker_build(){
    docker build --build-arg commit=${CI_COMMIT_TAG} -t ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME} .
    docker push ${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME}
  }


  function chart_build(){
      CHART_PATH=`find . -maxdepth 3 -name Chart.yaml`
      helm package ${CHART_PATH%/*} --version ${CI_COMMIT_TAG} --app-version ${CI_COMMIT_TAG}-${SUB_APP_NAME}
      echo "ok";
      
      # 通过Choerodon API上传chart包
      curl -X POST \
          -F "token=${Token}" \
          -F "version=${CI_COMMIT_TAG}-${SUB_APP_NAME}" \
          -F "file=@${PROJECT_NAME}-${CI_COMMIT_TAG}-${SUB_APP_NAME}.tgz" \
          -F "commit=${CI_COMMIT_SHA}" \
          -F "image=${DOCKER_REGISTRY}/${GROUP_NAME}/${PROJECT_NAME}:${CI_COMMIT_TAG}-${SUB_APP_NAME}" \
          "${CHOERODON_URL}/devops/ci"
      # 判断本次上传是否出错
      if [ $? -ne 0 ]; then
          echo "upload chart error"
          exit 1
      fi
  }


before_script:
  - *auto_devops

四、提交代码,运行CI

图片

五、部署应用

打开部署界面,选择刚刚新建的应用,可以看到有两个应用版本,app1和app2

图片

选择app1部署,为了便于区分服务,实例名称添加子应用名称 app1。

图片

点击部署之后,等待部署完成

图片

容器里面测试一下:

图片

示例代码:https://github.com/vinkdong/all-in-one-demo

运行正常,本文完。

VinkDong

open to open