jenkins-常用插件

阅读量: zyh 2020-04-22 11:12:11
Categories: > Tags:

gitlab

https://plugins.jenkins.io/gitlab-plugin/

这个插件会在 jenkins 系统配置中添加额外的选项框:Gitlab,通过设定这个选项框后,你可以在 Jenkinsfile 中调用这个选项,从而将 jenkins 工程步骤的结果通过 gitlab api 回传给 gitlab,gitlab 将这个结果在 ci/cd 流水线界面体现出来。

💥配置Gitlab选项框的时候,需要一个gitlab的用户的API token,且这个用户需要是gitlab工程的Maintainer用户。如果权限不对,会导致jenkins工程结果无法反馈给gitlab.

示例:

pipeline{
    agent {
        node {
            label "agent01"
        }
    }
    options {                          // 调用 jenkins 系统配置中配置好的 Gitlab
        gitLabConnection('gitlab')
    }
    environment {
	    dockerImage = ""              
    }
    stages {
        stage("set custom env"){
            steps{
                script{
                    imageName = env.GIT_URL.split("/|\\.")[-2]
                    imageTag = env.BRANCH_NAME + "-" + env.GIT_COMMIT.take(8)
                    if (env.BRANCH_NAME == 'master') {
                        registryCredential = "registry-apps_acr-rw" 
	                    registryUrl = "https://registry-vpc.cn-zhangjiakou.aliyuncs.com/"
	                    registryNamespace = env.GIT_URL.split("/|\\.")[-3]
                    } else {
                        registryCredential = "registry-jenkins_harbor-rw"                       
	                    registryUrl = "https://harbor.xxx.com:10443" 
	                    registryNamespace = env.GIT_URL.split("/|\\.")[-3]
                    }
                }
            }
        }
        stage("code: pull") {
            steps {
                cleanWs()
                script {
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'running'    // 回传阶段结果给 gitlab
                    try {
                        checkout scm
                    } catch(Exception ex) {
                        updateGitlabCommitStatus name: env.STAGE_NAME, state: 'failed'
                        throw ex;
                    }
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'success'
                }
            }
        }
        stage("code: test") {
            steps {
                script {
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'running'
                    try {
                        echo "单元测试"
                    } catch(Exception ex) {
                        updateGitlabCommitStatus name: env.STAGE_NAME, state: 'failed'
                        throw ex;
                    }
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'success'
                }
            }
        }
        stage('docker: build') {
			steps {
                script {
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'running'
                    try {
                        dockerImage = docker.build( registryNamespace + "/" + imageName + ":" + imageTag, "docker")  
                    } catch(Exception ex) {
                        updateGitlabCommitStatus name: env.STAGE_NAME, state: 'failed'
                        throw ex;
                    }
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'success'
                }
			}
		}
        stage('docker: push') {
			steps {
                script {
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'running'
                    try {
                        docker.withRegistry( registryUrl, registryCredential ) {
                            dockerImage.push()
                        }
                    } catch(Exception ex) {
                        updateGitlabCommitStatus name: env.STAGE_NAME, state: 'failed'
                        throw ex;
                    }
                    updateGitlabCommitStatus name: env.STAGE_NAME, state: 'success'
                }
			}
		}
    }
    post {
        success{
            updateGitlabCommitStatus name: env.STAGE_NAME, state: 'success'
            //成功清理工作空间,失败保留现场
            cleanWs()
        }
        failure{
            updateGitlabCommitStatus name: env.STAGE_NAME, state: 'failed'
            //成功清理工作空间,失败保留现场
            cleanWs()
        }
    }
}

插件本身内置变量

https://plugins.jenkins.io/gitlab-plugin/#plugin-content-defined-variables

💥多分支工程无法使用

gitlabBranch
gitlabSourceBranch
gitlabActionType
gitlabUserName
gitlabUserUsername
gitlabUserEmail
gitlabSourceRepoHomepage
gitlabSourceRepoName
gitlabSourceNamespace
gitlabSourceRepoURL
gitlabSourceRepoSshUrl
gitlabSourceRepoHttpUrl
gitlabMergeRequestTitle
gitlabMergeRequestDescription
gitlabMergeRequestId
gitlabMergeRequestIid
gitlabMergeRequestState
gitlabMergedByUser
gitlabMergeRequestAssignee
gitlabMergeRequestLastCommit
gitlabMergeRequestTargetProjectId
gitlabTargetBranch
gitlabTargetRepoName
gitlabTargetNamespace
gitlabTargetRepoSshUrl
gitlabTargetRepoHttpUrl
gitlabBefore
gitlabAfter
gitlabTriggerPhrase

gitlab-branch-source(多分支流水线)

https://plugins.jenkins.io/gitlab-branch-source/

这个插件,旨在多分支流水线的分支源中提供额外的gitlab选项,从而实现 jenkins 和 gitlab 之间的继承。从设置上,比gitlab插件或许要简单些。

✨这个插件有一个小缺点,就是jenkins是被动的以某个时间间隔监听gitlab的事件,所以gitlab事件触发后,jenkins这边可能会在十几秒以内某个点才会开启工程。当然,这只是观察到的现象,并不一定准确。

首先,插件会在 jenkins 系统配置中添加额外的选项框:GitLab,需要在这里设置 gitlab 服务链接信息。

💥注意这个和gitlab插件添加的选项卡名称及其相似。。。

image-20220413114803485

jenkins多分支工程里配置

添加分支源:GitLab项目:

行为列表:

image-20220413115205901

image-20220413115340464

image-20220413115405051