方式一:
需要安装插件:Build Name and Description Setter

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
                buildDescription "构建人:admin"
            }
        }
    }
}

在这里插入图片描述
方式二:
安装插件:build-user-vars-plugin

pipeline {
    agent any
    stages {
        stage('test') {
            steps {
                wrap([$class: 'BuildUser']) {
                    script {
                        BUILD_USER = "${env.BUILD_USER}"
                        currentBuild.description = " 构建人 : ${BUILD_USER}"
                    }           
                }
                
            }
        }
    }
}

在这里插入图片描述
方式三:
安装插件 pipeline-utility-steps(Pipeline Utility Steps)
Pipeline:Basic Steps

#!/usr/bin/env groovy
import groovy.json.JsonOutput

String gitRepositryURL = 'https://gitlab/dtkgo/dtk-go-goods-center.git'
String dockerRegistry = 'registry.b.com2'
String dockerRegistryURL = 'https://registry.b.com2'
String dockerRegistryNameSpace = 'dataoke-test2'

String kubeManifestsRepo = '/home/jenkins/repo/dtk-kubernetes-test/app'
String[] dockerFiles = ["Dockerfile.test"]
Map dockerImages = [:]
boolean notify = false
//获取dtk-go-goods-center
String projectName = gitRepositryURL.replaceFirst(/^.*\/([^\/]+?).git$/, '$1') 
//获取jenkins名字
env.JOB_BASE_NAME = env.JOB_NAME[4..-1]
//转义符
env.JOB_K8S_NAME = env.JOB_BASE_NAME.replaceAll('_', "-") 

// def specificCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
// print(specificCause)
// def causes = currentBuild.getBuildCauses()
// print(causes)


// print(JOB_BASE_NAME)
// print(projectName)
// print(JOB_K8S_NAME)


pipeline {
	agent any
       //定义流水线运行时的配置选项,流水线提供了许多选项,
	options {
		buildDiscarder logRotator(
			artifactDaysToKeepStr: '', 
			artifactNumToKeepStr: '', 
			daysToKeepStr: '10', 
			numToKeepStr: '10'
			)
	}
      //参数化
	parameters {
        
        choice( 
        	name: 'PENV',
        	choices: [
        		'dev1',
        		'dev2',
        		'test1',
        		'test2', 
        		'test3', 
        		'test4',
        		'test5',
                        'test6',
                        'test7',
        		'huise',
                        'huise3'
        	], 
        	description: '选择发布环境,默认发布至dev1测试环境'
        	)
           //git分支信息
        gitParameter(
        	name: 'GIT_BRANCH', 
            type: 'PT_BRANCH_TAG',
            branchFilter: 'origin/(.*)',
            defaultValue: 'master',
            selectedValue: 'DEFAULT',
            sortMode: 'DESCENDING_SMART',
            quickFilterEnabled: true, 
            description: 'Select your branch or tag.'
        	)
    }
    
   stages { 
       
      stage('test') {
        steps {
            script {
                    //定义函数,返回构建信息
                    def specificCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
                    print(specificCause)
          			def causes = currentBuild.getBuildCauses()
          			print(causes)
					buildUser = causes[0]['userName']
					print(buildUser)
					buildUserId = causes[0]['userId']
					print(buildUserId)
					//读取yaml文件
					userList = readYaml(file:'/var/jenkins_home/users.yaml')
					print(userList.env.dev.join(','))
					print(userList.user.allow)
					echo "111111"
				// 	if (!(PENV in userList.env.dev ) && !(buildUser in userList.user.allow)) {
				// 		error(message: "开发只能发布环境到${userList.env.dev.join(',')}")			
				// 	}
				// 	sh (script: '#!/bin/sh +x\n' + "check.py -u ${buildUser} -e ${PENV}")
				 //显示构建信息
					currentBuild.description = "k8s环境: ${PENV} 构建人:${buildUser} 分支: ${GIT_BRANCH}"
        }
     }
    }
}
}

users.yaml

---
user:
  allow:
    - a
    - b
    - c
    - d
    - e
    - zhangsan
    - lisi
  deny:

group:
  allow:
    - admin
  deny:

env:
  dev:
   - dev1
   - dev2
  huise:
   - huise
   - huise1
   - huise2
   - huise3

在这里插入图片描述

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐