jenkins pipline样例
jenkins pipline样例
String name="${checkout_sha}" node { // 拉取代码 stage('git checkout') { checkout([$class: 'GitSCM', branches: [[name: '*/main']],doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'cb9e3ed8-7c1d-48ff-9268-0a07f875c0f5', url: 'http://192.168.169.137/solomon/tom-test.git']]]) } // 项目打包到镜像并推送到镜像仓库 stage('Build and push Image') { sh ''' REPOSITORY=192.168.169.133/library/tomcat:${Branch} echo ' FROM 192.168.169.133/library/tomcat:v2.0 MAINTAINER maomao RUN rm -rf /usr/local/tomcat/webapps/* ' > Dockerfile docker build -t $REPOSITORY . docker login -u admin -p Harbor12345 192.168.169.133 docker push $REPOSITORY ''' } stage('test vars'){ echo "${name}" } }
声明式pipline
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
pipeline { agent any environment{ TOKEN = credentials('TG_TOKEN') CHAT_ID = credentials('TG_CHAT_ID') COMMIT_ID = sh( returnStdout: true, script:'git show-ref | grep uat | cut -c 1-40') AWS_DEFAULT_REGION = "ap-east-1" AWS_S3_BUCKET = "s3://codepipeline-target-notify-65776118" TEMPLATE_CODE_BUILD = "codebuild_auth" } tools { maven 'Maven 3.8.8' jdk 'jdk-11.0.19' } stages { stage ('0 - Initialize') { steps { sh ''' echo "PATH = ${PATH}" echo "M2_HOME = ${M2_HOME}" ''' } } stage ('1 - Checkout Code') { steps { script { checkout scmGit(branches: [[name: '*/uat']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab-ssh-key', url: 'ssh://git@192.168.17.11:58321/java/smons-oms.git']]) } } } stage ('2 - Compile Code') { when { expression { return (COMMIT_ID != null) } } steps { script { try { sh ''' echo "job_name = ${JOB_NAME}" echo "workspace = ${WORKSPACE}" echo "Commit_Id = ${COMMIT_ID}" ''' sh 'pwd' sh 'mvn clean package -Puat -Dmaven.test.skip=true' // archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true currentBuild.result="SUCCESS" } catch (e) { currentBuild.result="FAILURE" throw e } finally { } } } post { success { sh 'echo "Sports-OMS Compile Code is SUCCESS !" ' } } } stage('3 - CP Buildspec and Jar to S3') { when { expression { return (currentBuild.result == null || currentBuild.result == 'SUCCESS') } } steps { script { sh 'mkdir ${TEMPLATE_CODE_BUILD}' sh 'cp oms-auth/target/oms-auth.jar ${TEMPLATE_CODE_BUILD}/oms-auth.jar' sh 'cp build/Dockerfile.oms-auth ${TEMPLATE_CODE_BUILD}/Dockerfile' sh 'cp build/bin/docker-startup.oms-auth.sh ${TEMPLATE_CODE_BUILD}/docker-startup.sh' sh 'cp build/buildspec.oms-auth.yml ${TEMPLATE_CODE_BUILD}/buildspec.yml' sh 'echo ${COMMIT_ID} > ${TEMPLATE_CODE_BUILD}/tag.commit' sh 'zip -rj tag_oms_auth.zip ${TEMPLATE_CODE_BUILD} ' sh 'aws s3 cp tag_oms_auth.zip $AWS_S3_BUCKET/tag_oms_auth.zip' sh 'rm -rf ${TEMPLATE_CODE_BUILD}' } } post { success { sh 'curl --location "https://api.telegram.org/bot${TOKEN}/sendMessage" --form "text=Jenkins Prod-OMS-Auth [Commit SHA : ${COMMIT_ID}]\nRun Dockerfile is SUCCESS !" --form "chat_id=${CHAT_ID}"' } } } stage('4 - Trigger Oms-auth CodePipeline') { when { expression { return (currentBuild.result == null || currentBuild.result == 'SUCCESS') } } steps { //build job: "cd-oms-auth", wait: true sh 'echo "cd_oms_auth"' } } } }
脚本式pipline
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// harbor镜像仓库地址和密码信息 def registry = "harbor.solomon.com" // jenkins中定义的连接harbor的用户名密码凭据 def registry_se = "d2318c77-8a32-4618-bced-d6cabb0454b4" // gitlab地址和密码信息 def gitlab_url = "http://192.168.17.18/java/eladmin.git" // jenkins中定义git连接gitlab的密码凭据 def gitlab_se = "6e58036d-21ad-42b4-a8d2-50fb700f92c7" // jenkins中k8s-api 连线k8s集群的凭据信息 // jenkins中Config File provice插件定义的kubeconfig的内容 def k8s_auth = "782d66a2-d207-4398-b823-786b999db338" // k8s连接harbor的证书(kubectl命令离开k8s集群时需要,例如目前的jenkins环境) def secret_name = "registry-pull-secret" // harbor仓库的项目前缀信息 def prefix = "library" // 部署应用的服务名称 def app_name = "access-ultra-vires" // 部署服务所在的命名空间、副本数、容器暴露的端口、svc的端口 def ns = "default" def rc = 2 def cport = 8008 def cluport = 8008 // 构建编译的环境参数 def env = "default" node(){ stage("1.pull git code"){ checkout([$class: 'GitSCM', branches: [[name: '*/dev']], userRemoteConfigs: [[credentialsId: "${gitlab_se}", url: "${gitlab_url}"]]]) script { build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() TM = sh(script: 'date +%Y%m%d%H%M') } image_name_build = "${registry}/${prefix}/${app_name}:${BUILD_NUMBER}" image_name_latest = "${registry}/${prefix}/${app_name}:latest" } stage("2.build code"){ sh "mvn clean package -P${env} -DskipTests" } stage("3.build docker and push to harbor") { withCredentials([usernamePassword(credentialsId: "${registry_se}", passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) { sh "docker login -u ${dockerHubUser} -p ${dockerHubPassword} ${registry}" sh "docker build -t ${image_name_build} -f Dockerfile . --no-cache" sh "docker tag ${image_name_build} ${image_name_latest}" sh "docker push ${image_name_build}" sh "docker push ${image_name_latest}" } } stage("4.deploy to k8s") { // sh "/usr/bin/ossutil cp -uf oss://prod-***/api/blue/templete-v1.yaml templete-v1.yaml" sh "cp templete-v1.yaml ${app_name}-dep.yaml" sh "sed -i 's#SVC_NAME#${app_name}#g' ${app_name}-dep.yaml" sh "sed -i 's#NS_NAME#${ns}#g' ${app_name}-dep.yaml" sh "sed -i 's#RC_NUM#${rc}#g' ${app_name}-dep.yaml" sh "sed -i 's#IMAGE_URL#${image_name_latest}#g' ${app_name}-dep.yaml" sh "sed -i 's#CON_PORT#${cport}#g' ${app_name}-dep.yaml" sh "sed -i 's#CLU_PORT#${cluport}#g' ${app_name}-dep.yaml" sh "kubectl apply -f ${app_name}-dep.yaml" echo "deploy success" } }
generic webhook trigger中的钩子
https://www.cnblogs.com/zhuhaofeng/p/16615581.html
定义命令片段
def yarn_home = "/var/yarn_home"; // 此处省略代码... stage('构建') { nodejs("nodejs"){// 括号里面的名字在jenkins》系统管理》全局工具配置中设定的NodeJS的别名 sh("node -v && npm -v") sh("${yarn_home}/bin/yarn install") sh("${yarn_home}/bin/yarn build") } }