赞
踩
CODING 作为一站式云端开发平台,从诞生之初就生长在云端,充分利用腾讯云的能力为客户提供弹性可靠的服务。而且标准版本每月提供免费的使用时长 相当于白嫖了一波服务器。
coding devops高效的云上研发工作流,提供一站式开发协作工具,帮助研发团队快速落地敏捷开发与 DevOps 开发方式,实现研发效能升级.
● 实践敏捷开发打开即用的 Scrum 敏捷开发工作流,适用于团队项目管理与协作。
● 构建 DevOps 工作方式一站式 DevOps 工具,适用于研发团队快速搭建 DevOps ,实现可持续发布。
● 代码开发协同Git/SVN 代码仓库服务,免费、超大高可用仓库,适用于研发团队协作开发。
● 测试管理协作在线组织和管理测试用例及测试计划,将测试团队工作搬到云上。
准备一个用于CI/CD的项目。
并在需要打包的模块根目录中添加 Dockerfile
例如roca–shop-oauth模块
FROM openjdk:8-jdk-alpine
EXPOSE 8202
COPY /roca-shop-oauth/target/roca-shop-oauth-1.0.0.jar roca-shop-oauth.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Dfile.encoding=UTF-8", "-jar", "roca-shop-oauth.jar"]
准备一台服务器(我这里使用的是1核2G1M阿里云)
安装启动docker以及相关的容器,在服务器~目录下创建docker-compose文件和master.env文件
version: '3.5' services: roca-shop-gateway: container_name: roca-shop-gateway image: qingzhouyilian-docker.pkg.coding.net/shop/roca/roca-shop-gateway:master env_file: - ./master.env environment: - JAVA_TOOL_OPTIONS=-Xms256m -Xmx256m -Xmn128m ports: - "8005:8005" volumes: - /usr/local/magese/magese-api/logs:/usr/local/magese/magese-api/logs - /etc/timezone:/etc/timezone - /etc/localtime:/etc/localtime roca-system: container_name: roca-system image: qingzhouyilian-docker.pkg.coding.net/shop/roca/roca-system:master env_file: - ./master.env environment: - JAVA_TOOL_OPTIONS=-Xms256m -Xmx256m -Xmn128m ports: - "8006:8006" volumes: - /usr/local/magese/magese-api/logs:/usr/local/magese/magese-api/logs - /etc/timezone:/etc/timezone - /etc/localtime:/etc/localtime roca-shop-oauth: container_name: roca-shop-oauth image: qingzhouyilian-docker.pkg.coding.net/shop/roca/roca-shop-oauth:master env_file: - ./master.env environment: - JAVA_TOOL_OPTIONS=-Xms256m -Xmx256m -Xmn128m ports: - "8007:8007" volumes: - /usr/local/magese/magese-api/logs:/usr/local/magese/magese-api/logs - /etc/timezone:/etc/timezone - /etc/localtime:/etc/localtime
在coding上面创建项目
选择java+Spring+Docker模板
选择自己项目的位置选择对应的仓库,取消单元测试,编译构建暂时不做更改,镜像名字自己命名。
新建的项目是没有制品的 我们新建制品 然后命名
选择创建的制品
录入凭据
ssh-keygen -m PEM -t ed25519 -C "your.email@example.com"
cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
JvKE3WXugDp2YuswF4MAJq/o+R9kRKZzNfeM9A6NWjwjnIYP4yUVwqa/20MYmVpT
......
输入服务器地址 选择服务器的凭据
使用文本编辑器打开流程配置
修改编译步骤
stage('编译') {
steps {
sh 'mvn clean package -Dmaven.javadoc.skip=true -Dmaven.test.skip=true'
}
}
增加登录制品库步骤
stage('登录 CODING Docker 制品库') {
steps {
sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${CODING_DOCKER_REG_HOST}"
}
}
构建推送模块
stage('构建推送gateway 模块') {
steps {
sh """
docker build -t ${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} -f /root/workspace/${GATEWAY_IMAGE_NAME}/Dockerfile ${DOCKER_BUILD_CONTEXT}
docker images ${GATEWAY_IMAGE_NAME}
docker tag ${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} qingzhouyilian-docker.pkg.coding.net/shop/roca/${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION}
docker push qingzhouyilian-docker.pkg.coding.net/shop/roca/${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION}
"""
}
}
修改部署步骤
stage('部署') { steps { script { def remoteConfig = [:] remoteConfig.name = "my-remote-server" remoteConfig.host = "${REMOTE_HOST}" remoteConfig.port = "${REMOTE_SSH_PORT}".toInteger() remoteConfig.allowAnyHosts = true withCredentials([ sshUserPrivateKey( credentialsId: "${REMOTE_CRED}", keyFileVariable: "privateKeyFilePath" ) ]) { // SSH 登陆用户名 remoteConfig.user = "${REMOTE_USER_NAME}" // SSH 私钥文件地址 remoteConfig.identityFile = privateKeyFilePath sshCommand( remote: remoteConfig, command: "docker-compose -f ${DOCKER_COMPOSE_PATH} down --rmi all", sudo: true, ) sshCommand( remote: remoteConfig, command: "docker-compose -f ${DOCKER_COMPOSE_PATH} up -d", sudo: true, ) echo "Deploy success! Go to http://api.magese.com/doc.html for a preview." } } } }
完整的配置文件
pipeline { agent any stages { stage('检出') { steps { checkout([$class: 'GitSCM', branches: [[name: GIT_BUILD_REF]], userRemoteConfigs: [[ url: GIT_REPO_URL, credentialsId: CREDENTIALS_ID ]]]) } } stage('编译') { steps { sh 'mvn clean package -Dmaven.javadoc.skip=true -Dmaven.test.skip=true' } } stage('登录 CODING Docker 制品库') { steps { sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${CODING_DOCKER_REG_HOST}" } } stage('构建推送gateway 模块') { steps { sh """ docker build -t ${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} -f /root/workspace/${GATEWAY_IMAGE_NAME}/Dockerfile ${DOCKER_BUILD_CONTEXT} docker images ${GATEWAY_IMAGE_NAME} docker tag ${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} qingzhouyilian-docker.pkg.coding.net/shop/roca/${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} docker push qingzhouyilian-docker.pkg.coding.net/shop/roca/${GATEWAY_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} """ } } stage('构建推送ouath模块') { steps { sh """ docker build -t ${AUTH_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} -f /root/workspace/${AUTH_IMAGE_NAME}/Dockerfile ${DOCKER_BUILD_CONTEXT} docker images ${AUTH_IMAGE_NAME} docker tag ${AUTH_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} qingzhouyilian-docker.pkg.coding.net/shop/roca/${AUTH_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} docker push qingzhouyilian-docker.pkg.coding.net/shop/roca/${AUTH_IMAGE_NAME}:${DOCKER_IMAGE_VERSION} """ } } stage('构建推送system模块') { steps { sh """ docker build -t ${SYSTEM_IMAGE__NAME}:${DOCKER_IMAGE_VERSION} -f /root/workspace/${MODULES_IMAGE_NAME}/${SYSTEM_IMAGE__NAME}/Dockerfile ${DOCKER_BUILD_CONTEXT} docker images ${SYSTEM_IMAGE__NAME} docker tag ${SYSTEM_IMAGE__NAME}:${DOCKER_IMAGE_VERSION} qingzhouyilian-docker.pkg.coding.net/shop/roca/${SYSTEM_IMAGE__NAME}:${DOCKER_IMAGE_VERSION} docker push qingzhouyilian-docker.pkg.coding.net/shop/roca/${SYSTEM_IMAGE__NAME}:${DOCKER_IMAGE_VERSION} """ } } stage('部署') { steps { script { def remoteConfig = [:] remoteConfig.name = "my-remote-server" remoteConfig.host = "${REMOTE_HOST}" remoteConfig.port = "${REMOTE_SSH_PORT}".toInteger() remoteConfig.allowAnyHosts = true withCredentials([ sshUserPrivateKey( credentialsId: "${REMOTE_CRED}", keyFileVariable: "privateKeyFilePath" ) ]) { // SSH 登陆用户名 remoteConfig.user = "${REMOTE_USER_NAME}" // SSH 私钥文件地址 remoteConfig.identityFile = privateKeyFilePath sshCommand( remote: remoteConfig, command: "docker-compose -f ${DOCKER_COMPOSE_PATH} down --rmi all", sudo: true, ) sshCommand( remote: remoteConfig, command: "docker-compose -f ${DOCKER_COMPOSE_PATH} up -d", sudo: true, ) echo "Deploy success! Go to http://api.magese.com/doc.html for a preview." } } } } } environment { CODING_DOCKER_REG_HOST = "${CCI_CURRENT_TEAM}-docker.pkg.${CCI_CURRENT_DOMAIN}" CODING_DOCKER_IMAGE_NAME = "${PROJECT_NAME.toLowerCase()}/${DOCKER_REPO_NAME}/${DOCKER_IMAGE_NAME}" } }
脚本中用到的变量需要根据自己的情况修改保存
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。