In Jenkins, how do builds know who requested them?

后端 未结 6 1100
南旧
南旧 2021-01-04 01:09

I need to pass the username of the requestor of a build down to the script that is actually doing the work. Looking at the console output for a particular build, the first

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 01:25

    import os
    import jenkinsapi.build
    import jenkinsapi.jenkins
    
    #Required Environment variables example:
    #JENKINS_URL=http://jenkinsserver/
    #JOB_NAME=TEST_GT
    #BUILD_NUMBER=8
    
    jenkins_inst = None
    
    def get_jenkins_inst():
        if jenkins_inst == None:
            jenkins_url = os.environ['JENKINS_URL']
            print("Connect to jenkins " + jenkins_url)
            jenkins_inst = jenkinsapi.jenkins.Jenkins(jenkins_url)
        return jenkins_inst
    
    def get_jenkins_job():
        jenkins_inst = get_jenkins_inst()
    
        jenkins_job_name = os.environ['JOB_NAME']
        print("Get jenkins job " + jenkins_job_name)
        return jenkins_inst.get_job(jenkins_job_name)
    
    def get_jenkins_user():
        jenkins_job = get_jenkins_job()
    
        jenkins_buildno = int(os.environ['BUILD_NUMBER'])
        print("Get jenkins job build " + str(jenkins_buildno))
        cur_build = jenkins_job.get_build(jenkins_buildno)
    
        return cur_build.get_actions()['causes'][0]['userId']
    

提交回复
热议问题