如何获取大禹流水线连接配置和创建定制大禹流水线

本文介绍如何获取大禹流水线连接配置,以及如何创建基于代码部署和镜像部署的大禹流水线。

获取大禹流水线连接配置

  1. 登录大禹-GTS工作台
  2. 交付项目列表页面,单击目标交付项目。1
  3. 获取AccessKey IDAccessKey Secret。在目标交付项目的左侧导航栏中,参考下图操作。1
    1. 选择管理 > 设置
    2. 项目配置页面,单击OpenAPI
    3. 单击创建AccessKey,单击复制,将AccessKey IDAccessKey Secret保存至本地。1
      说明 查看Secret对话框关闭后,您无法再次查询Secret。如果您遗失该AccessKey,可以创建新的AccessKey替代。
  4. 获取大禹项目ID大禹用户ID1
    上图中:
    • businessId对应大禹项目ID
    • 用户ID对应大禹用户ID
    当您在新建流水线连接时,关于大禹项目ID大禹用户ID信息您可以通过该步骤获取。而AccessKey IDAccessKey Secret信息您可以通过步骤3获取。1

创建大禹流水线

  1. 在目标交付项目的左侧导航栏中,选择研运 > 应用1
  2. 单击1图标。
  3. 创建研发项目面板,填写名称标识描述(选填),完成后单击确定1
  4. 在新建研发项目的基本信息页签,单击新增应用1
  5. 完成新建应用的相关配置。1
    1. 选择目标应用。
    2. 选择目标应用类型。
    3. 设置目标应用详细信息:名称标识描述(选填)、仓库,以及选择应用是否为空应用
    4. 完成后单击确定
  6. 新建定制流水线。
    1. 在目标应用的应用概览页签,单击定制流水线1
    2. 在目标应用的定制流水线页签,单击新建定制流水线1
    3. 新建流水线对话框中,设置流水线名称所属研发项目流水线模板所属应用分支模式分支触发方式,完成后,单击保存1
  7. 在目标流水线ID右侧操作列下,选择更多 > 编辑1
  8. 编辑流水线页面,您可以参考以下步骤完成流水线编辑操作。1
    说明 代码准备(代码准备)节点不支持编辑,代码克隆(准备阶段)节点不支持编辑和删除。
    • 新增自定义脚本节点。单击1图标,在选择任务类型对话框中,单击工具页签后,单击自定义脚本1
      新建阶段面板,完成阶段名称自定义脚本设置后,单击确定1

      创建基于代码部署的大禹流水线。

      1. 新增第一个自定义脚本节点:Java代码克隆,具体脚本示例如下。
        echo "========================== ${STAGE_NAME} =========================="
        
        updateStatus(){
          pipelineId=$(echo ${JOB_NAME} | awk -F"-" '{print $(NF-1)}')
          rm -rf ${JOB_NAME}-${BuildId}
          result=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/updateDeploymentStatusWebhook?x-bw-us-auth-way=jwt&access_token=${BW_AUTH_TOKEN}" \
                        -H "Content-Type: application/json" \
                        -X POST -d "{\"task\": {\"pipelineId\": \"${pipelineId}\",\"buildNumber\": \"${BuildId}\",\"statusCode\": \"FAIL\"},
                        \"globalParams\": [{\"key\": \"BW_TENANT_ID\", \"value\": \"${BW_TENANT_ID}\"},{\"key\": \"BW_USER_ID\", \"value\": \"${BW_USER_ID}\"},
                        {\"key\": \"BW_DEPLOYMENT_CODE\", \"value\": \"${BW_DEPLOYMENT_CODE}\"}]}")
        
          if [ $(echo "${result}" | grep -c '"code":"OK"') = 0 ] ; then
            echo "更新流水线状态 [  失败  ] - $result"
            exit 1
          else
            echo "更新流水线状态 [  成功  ]"
          fi
        }
        
        mkdir ${JOB_NAME}-${BuildId}
        cd ${JOB_NAME}-${BuildId}
        echo "代码仓库:${BW_GIT_REPO_HTTP_URL}"
        url=$(echo "${BW_GIT_REPO_HTTP_URL}" | awk -F '//' '{print $2}')
        git_url=$(echo http://${BW_GIT_REPO_USERNAME}:${BW_GIT_REPO_PASSWORD}@${url})
        
        if [ ! -z "${BW_GIT_REPO_TAG}" ]; then
          echo "构建Tag:${BW_GIT_REPO_TAG}"
          git clone -c advice.detachedHead=false --depth 1 --single-branch --branch ${BW_GIT_REPO_TAG} ${git_url} .
        elif [ ! -z "${BW_GIT_REPO_COMMIT_ID}" ]; then
          echo "构建commitId:${BW_GIT_REPO_COMMIT_ID}"
          git clone ${git_url} . && git reset --hard ${BW_GIT_REPO_COMMIT_ID}
        elif [ ! -z "${BW_GIT_REPO_BRANCH}" ]; then
          echo "构建分支:${BW_GIT_REPO_BRANCH}"
          git clone --depth 1 --single-branch --branch ${BW_GIT_REPO_BRANCH} ${git_url} .
        fi
        
        if [ "$?" != 0 ]; then
          echo "克隆代码失败,请检查代码仓库地址、用户名、密码是否正确"
          updateStatus
          exit 1
        fi
      2. 新增一个自定义脚本节点:Java代码构建,具体脚本示例如下。
        echo "========================== ${STAGE_NAME} =========================="
        
        updateStatus(){
          pipelineId=$(echo ${JOB_NAME} | awk -F"-" '{print $(NF-1)}')
          rm -rf ${JOB_NAME}-${BuildId}
          result=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/updateDeploymentStatusWebhook?x-bw-us-auth-way=jwt&access_token=${BW_AUTH_TOKEN}" \
                        -H "Content-Type: application/json" \
                        -X POST -d "{\"task\": {\"pipelineId\": \"${pipelineId}\",\"buildNumber\": \"${BuildId}\",\"statusCode\": \"FAIL\"},
                        \"globalParams\": [{\"key\": \"BW_TENANT_ID\", \"value\": \"${BW_TENANT_ID}\"},{\"key\": \"BW_USER_ID\", \"value\": \"${BW_USER_ID}\"},
                        {\"key\": \"BW_DEPLOYMENT_CODE\", \"value\": \"${BW_DEPLOYMENT_CODE}\"}]}")
        
          if [ $(echo "${result}" | grep -c '"code":"OK"') = 0 ] ; then
            echo "更新流水线状态 [  失败  ] - $result"
            exit 1
          else
            echo "更新流水线状态 [  成功  ]"
          fi
        }
        
        cd ${JOB_NAME}-${BuildId}
        if [ -z "${BW_BUILD_MODULE_PATH}" ];then
          MODULE_DIR=$(ls | grep -e "-start")
        else
          MODULE_DIR=${BW_BUILD_MODULE_PATH}
        fi
        
        if [ ! -d "${MODULE_DIR}" ];then
          echo "---------------------------------------------------------------"
          echo " MODULE_PATH 不存在,请检查Module名称是否正确。"
          updateStatus
          exit 1
        fi
        
        if [ "${BW_APPLICATION_TYPE}" = "SourceCode" ]; then
          MVN_PLUGIN_PARAM="-DtenantId=${BW_TENANT_ID} -DprojectCode=${BW_PROJECT_CODE} -DapplicationCode=${BW_APPLICATION_CODE} -DlogicFlowServer=${BW_LOGIC_FLOW_SERVER}"
        fi
        
        if [ ! -f "${BW_BUILD_SETTINGS_PATH}" ]; then
          echo "构建命令:mvn ${MVN_PLUGIN_PARAM} -Duser.timezone=GMT+08 -f pom.xml -U clean package -Dmaven.test.skip=true"
          mvn ${MVN_PLUGIN_PARAM} -Duser.timezone=GMT+08 -f pom.xml -U clean package -Dmaven.test.skip=true
        else
          echo "构建命令:mvn ${MVN_PLUGIN_PARAM} -Duser.timezone=GMT+08 -f pom.xml -s ${BW_BUILD_SETTINGS_PATH} -U clean package -Dmaven.test.skip=true"
          mvn ${MVN_PLUGIN_PARAM} -Duser.timezone=GMT+08 -f pom.xml -s ${BW_BUILD_SETTINGS_PATH} -U clean package -Dmaven.test.skip=true
        fi
        
        if [ $(ls -l ${MODULE_DIR}/target/*.jar | wc -l) -eq 1 ];then
          JAR_PACKAGE=$(ls ${MODULE_DIR}/target/*.jar | awk -F '/' '{print $NF}')
          cp ${MODULE_DIR}/target/${JAR_PACKAGE} ${BW_BUILD_DOCKERFILE_PATH}/${JAR_PACKAGE}
        else
          echo "---------------------------------------------------------------"
          echo "代码编译失败,请检查错误日志,可能的原因:"
          echo "1. 工程根目录下没有settings.xml文件,编译过程中拉取依赖的jar包失败。"
          echo "2. ${MODULE_DIR}/target目录下没有编译后的jar包。"
          updateStatus
          exit 1
        fi
      3. 新增一个自定义脚本节点:镜像构建,具体脚本示例如下。
        echo "========================== ${STAGE_NAME} =========================="
        
        updateStatus(){
          pipelineId=$(echo ${JOB_NAME} | awk -F"-" '{print $(NF-1)}')
          rm -rf ${JOB_NAME}-${BuildId}
          result=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/updateDeploymentStatusWebhook?x-bw-us-auth-way=jwt&access_token=${BW_AUTH_TOKEN}" \
                        -H "Content-Type: application/json" \
                        -X POST -d "{\"task\": {\"pipelineId\": \"${pipelineId}\",\"buildNumber\": \"${BuildId}\",\"statusCode\": \"FAIL\"},
                        \"globalParams\": [{\"key\": \"BW_TENANT_ID\", \"value\": \"${BW_TENANT_ID}\"},{\"key\": \"BW_USER_ID\", \"value\": \"${BW_USER_ID}\"},
                        {\"key\": \"BW_DEPLOYMENT_CODE\", \"value\": \"${BW_DEPLOYMENT_CODE}\"}]}")
        
          if [ $(echo "${result}" | grep -c '"code":"OK"') = 0 ] ; then
            echo "更新流水线状态 [  失败  ] - $result"
            exit 1
          else
            echo "更新流水线状态 [  成功  ]"
          fi
        }
        
        cd ${JOB_NAME}-${BuildId}
        JAR_PACKAGE=$(ls ${BW_BUILD_DOCKERFILE_PATH}/*.jar | awk -F '/' '{print $NF}')
        chmod a+r ${BW_BUILD_DOCKERFILE_PATH}/*.jar
        BW_TARGET_REGISTRY_URL=$(echo ${BW_TARGET_IMAGE_URL} | awk -F '/' '{print $1}')
        
        docker login --username=${BW_TARGET_REGISTRY_USERNAME} --password=${BW_TARGET_REGISTRY_PASSWORD} ${BW_TARGET_REGISTRY_URL}
        if [ "$?" != 0 ]; then
          echo "${BW_TARGET_REGISTRY_URL} 登录失败,请检查镜像仓库地址、用户名、密码是否正确。"
          updateStatus
          exit 1
        fi
        
        docker build --pull --build-arg JAR_PACKAGE=${JAR_PACKAGE} -t ${BW_TARGET_IMAGE_URL} ${BW_BUILD_DOCKERFILE_PATH}/
        
        docker push ${BW_TARGET_IMAGE_URL}
        if [ "$?" != 0 ]; then
          echo "推送镜像失败,请检查镜像仓库地址、用户名、密码是否正确。"
          updateStatus
          exit 1
        fi
        
        docker rmi ${BW_TARGET_IMAGE_URL}
      4. 新增一个自定义脚本节点:应用部署,具体脚本示例如下。
        echo "========================== ${STAGE_NAME} =========================="
        
        updateStatus(){
          pipelineId=$(echo ${JOB_NAME} | awk -F"-" '{print $(NF-1)}')
          rm -rf ${JOB_NAME}-${BuildId}
          result=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/updateDeploymentStatusWebhook?x-bw-us-auth-way=jwt&access_token=${BW_AUTH_TOKEN}" \
                        -H "Content-Type: application/json" \
                        -X POST -d "{\"task\": {\"pipelineId\": \"${pipelineId}\",\"buildNumber\": \"${BuildId}\",\"statusCode\": \"FAIL\"},
                        \"globalParams\": [{\"key\": \"BW_TENANT_ID\", \"value\": \"${BW_TENANT_ID}\"},{\"key\": \"BW_USER_ID\", \"value\": \"${BW_USER_ID}\"},
                        {\"key\": \"BW_DEPLOYMENT_CODE\", \"value\": \"${BW_DEPLOYMENT_CODE}\"}]}")
        
          if [ $(echo "${result}" | grep -c '"code":"OK"') = 0 ] ; then
            echo "更新流水线状态 [  失败  ] - $result"
            exit 1
          else
            echo "更新流水线状态 [  成功  ]"
          fi
        }
        
        code=$(curl -I -m 10 -o /dev/null -s -w %{http_code} ${BW_DOMAIN_NAME})
        echo ${code}
        if [ "${code}" != 200 ];then
          echo "访问 ${BW_DOMAIN_NAME} 地址异常"
          updateStatus
          exit 1
        fi
        
        result=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/deployInstance" \
                      -H "Content-Type: application/json" \
                      -H "x-bw-us-auth-way: jwt" \
                      -H "Authorization: Bearer ${BW_AUTH_TOKEN}" \
                      -H "x-bw-tenant-id: ${BW_TENANT_ID}" \
                      -H "x-bw-user-id: ${BW_USER_ID}" \
                      -H "x-bw-user-name: ${BW_USER_NAME}" \
                      -X POST -d "{\"deploymentCode\": \"${BW_DEPLOYMENT_CODE}\"}")
        if [ $(echo "${result}" | grep -c '"code":"OK"') = 0 ] ; then
          echo "触发应用部署 [  失败  ] - ${result}"
          updateStatus
          exit 1
        else
          echo "触发应用部署 [  成功  ]"
        fi
        
        # 检查实例状态
        while :; do
          sleep 5
          status=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/getInstanceDeployStatusFromPipeline" \
                        -H "Content-Type: application/json" \
                        -H "x-bw-us-auth-way: jwt" \
                        -H "Authorization: Bearer ${BW_AUTH_TOKEN}" \
                        -H "x-bw-tenant-id: ${BW_TENANT_ID}" \
                        -H "x-bw-user-id: ${BW_USER_ID}" \
                        -H "x-bw-user-name: ${BW_USER_NAME}" \
                        -X POST -d "{\"deploymentCode\": \"${BW_DEPLOYMENT_CODE}\"}")
          # echo $result
          if [ $(echo ${status} | grep '"code":"OK"' | grep 'Running' | wc -l) = 1 ] ; then
            echo "检查部署状态 [ 部署中 ]"
          elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Cancelled' | wc -l) = 1 ] ; then
            echo "检查部署状态 [  取消  ]"
            updateStatus
            exit 1
          elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Failed' | wc -l) = 1 ] ; then
            echo "检查部署状态 [  失败  ]"
            updateStatus
            exit 1
          elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Succeeded' | wc -l) = 1 ] ; then
            echo "检查部署状态 [  成功  ]"
            rm -rf ${JOB_NAME}-${BuildId}
            exit 0
          elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Abnormal' | wc -l) = 1 ] ; then
            echo "检查部署状态 [  异常  ]"
            rm -rf ${JOB_NAME}-${BuildId}
            exit 1
          else
            echo "检查部署状态 [  失败  ]"
            updateStatus
            exit 1
          fi
        done
      编辑流水线页面,您可以查看到新建的节点。1

      创建基于镜像部署的大禹流水线

      新增一个自定义脚本节点:应用部署,具体脚本示例如下。
      echo "========================== ${STAGE_NAME} =========================="
      
      updateStatus(){
        pipelineId=$(echo ${JOB_NAME} | awk -F"-" '{print $(NF-1)}')
        result=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/updateDeploymentStatusWebhook?x-bw-us-auth-way=jwt&access_token=${BW_AUTH_TOKEN}" \
                      -H "Content-Type: application/json" \
                      -X POST -d "{\"task\": {\"pipelineId\": \"${pipelineId}\",\"buildNumber\": \"${BuildId}\",\"statusCode\": \"FAIL\"},
                      \"globalParams\": [{\"key\": \"BW_TENANT_ID\", \"value\": \"${BW_TENANT_ID}\"},{\"key\": \"BW_USER_ID\", \"value\": \"${BW_USER_ID}\"},
                      {\"key\": \"BW_DEPLOYMENT_CODE\", \"value\": \"${BW_DEPLOYMENT_CODE}\"}]}")
      
        if [ $(echo "${result}" | grep -c '"code":"OK"') = 0 ] ; then
          echo "更新流水线状态 [  失败  ] - $result"
          exit 1
        else
          echo "更新流水线状态 [  成功  ]"
        fi
      }
      
      code=$(curl -I -m 10 -o /dev/null -s -w %{http_code} ${BW_DOMAIN_NAME})
      echo ${code}
      if [ "${code}" != 200 ];then
        echo "访问 ${BW_DOMAIN_NAME} 地址异常"
        updateStatus
        exit 1
      fi
      
      result=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/deployInstance" \
                    -H "Content-Type: application/json" \
                    -H "x-bw-us-auth-way: jwt" \
                    -H "Authorization: Bearer ${BW_AUTH_TOKEN}" \
                    -H "x-bw-tenant-id: ${BW_TENANT_ID}" \
                    -H "x-bw-user-id: ${BW_USER_ID}" \
                    -H "x-bw-user-name: ${BW_USER_NAME}" \
                    -X POST -d "{\"deploymentCode\": \"${BW_DEPLOYMENT_CODE}\"}")
      if [ $(echo "${result}" | grep -c '"code":"OK"') = 0 ] ; then
        echo "触发应用部署 [  失败  ] - ${result}"
        updateStatus
        exit 1
      else
        echo "触发应用部署 [  成功  ]"
      fi
      
      # 检查实例状态
      while :; do
        sleep 5
        status=$(curl -s "${BW_DOMAIN_NAME}/devBackend/v2/deployment/getInstanceDeployStatusFromPipeline" \
                      -H "Content-Type: application/json" \
                      -H "x-bw-us-auth-way: jwt" \
                      -H "Authorization: Bearer ${BW_AUTH_TOKEN}" \
                      -H "x-bw-tenant-id: ${BW_TENANT_ID}" \
                      -H "x-bw-user-id: ${BW_USER_ID}" \
                      -H "x-bw-user-name: ${BW_USER_NAME}" \
                      -X POST -d "{\"deploymentCode\": \"${BW_DEPLOYMENT_CODE}\"}")
        # echo $result
        if [ $(echo ${status} | grep '"code":"OK"' | grep 'Running' | wc -l) = 1 ] ; then
          echo "检查部署状态 [ 部署中 ]"
        elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Cancelled' | wc -l) = 1 ] ; then
          echo "检查部署状态 [  取消  ]"
          updateStatus
          exit 1
        elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Failed' | wc -l) = 1 ] ; then
          echo "检查部署状态 [  失败  ]"
          updateStatus
          exit 1
        elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Succeeded' | wc -l) = 1 ] ; then
          echo "检查部署状态 [  成功  ]"
          exit 0
        elif [ $(echo ${status} | grep '"code":"OK"' | grep 'Abnormal' | wc -l) = 1 ] ; then
          echo "检查部署状态 [  异常  ]"
          exit 1
        else
          echo "检查部署状态 [  失败  ]"
          updateStatus
          exit 1
        fi
      done
      编辑流水线页面,您可以查看到新建的节点。1
    • 删除节点。单击1图标。