完整的多分支流水线jenkinsfile

一套完整的多分支流水线jenkinsfile,使用时只需要修改每个项目的gitlab token,可以做到多个项目通用

pipeline {
    agent any
    #构建完成后结果通知到gitlab
    post {
      failure {
        updateGitlabCommitStatus name: 'build', state: 'failed'
        updateGitlabCommitStatus name: 'deploy', state: 'failed'
      }
      success {
        updateGitlabCommitStatus name: 'build', state: 'success'
        updateGitlabCommitStatus name: 'deploy', state: 'success'
      }
    }
    options {
      gitLabConnection('gitlab-example')
      gitlabBuilds(builds: ['build', 'deploy'])
      #需要先安装插件Build Discarder,清除历史构建build记录
      #artifactDaysToKeepStr: 发布包保留天数
      #artifactNumToKeepStr: 发布包最大保留构建次数
      buildDiscarder(logRotator(numToKeepStr: '8', artifactNumToKeepStr: '8'))
    }
    #分支触发插件,需要先安装Generic Webhook Trigger插件
    triggers {
    GenericTrigger (
            causeString: 'Triggered by $ref',
            genericVariables: [[key: 'ref', value: '$.ref']],
            printContributedVariables: true,
            printPostContent: true,
            regexpFilterText: '$ref',
            regexpFilterExpression: 'refs/heads/' + BRANCH_NAME,
            #gitlab-api获取webhook token
            token: '*******'
    )
    }
    
    environment {
        NODE_IMG = 'node:12.22.1'
        MAVEN_IMG = 'maven:3.6.3-jdk-8'
        DEPLOY_IMG = 'centos:ssh'
        #harbor仓库地址
        REGISTRY = '********'
        ITEM = """${sh(
                returnStdout: true,
                script: "echo $JOB_NAME | awk -F / '{print \$1}'"
            ).trim()}"""
    }


    stages {       
        stage('build') {
            agent{
                node{
                    label 'master'
                }
            }
            steps{
                sh script:'''
                  #!/bin/bash
                  pwd
                  ls  
                '''
                #构建镜像过程,可以参阅Jenkins片段生成器
                script{
                    docker.withRegistry("$REGISTRY",'9d6108b4-10c4-422b-adf9-27571b9a4526') {
                        def image = docker.build("devops/$JOB_NAME","./devops/docker")
                        image.push()
                    }
                }
            }
        }
        stage('deploy') {
            steps{
                #ssh到远程主机上执行,可用Jenkins语法片段生成器生成
                script{
                    def remote = [:]
                    remote.name = "******"
                    remote.host = "******"
                    remote.allowAnyHosts = true
                    #Jenkins默认是jenkins用户登陆到主机,必须先到主机上创建Jenkins用户                   
                    node {
                        withCredentials([usernamePassword(credentialsId: '******', passwordVariable: 'password', usernameVariable: 'userName')]) {
                            remote.user = userName
                            remote.password = password
                            #发布是使用的docker swarm发布
                            stage("deploy") {                                
                                sshCommand remote: remote, command: "pwd;mkdir -p $JOB_NAME;ls -l;"
                                sshPut remote: remote, from: 'devops/docker/docker-compose.yml', into: "$JOB_NAME"
                                sshCommand remote: remote, command: "export ITEM=$ITEM;export JOB_NAME=$JOB_NAME;docker stack deploy -c $JOB_NAME/docker-compose.yml --with-registry-auth --prune --resolve-image always $ITEM;"
                    
                            }
                        }
                    }
                }
            }
        }
    }
}

你可能感兴趣的:(jenkins,devops,运维)