jenkinsci / lockable-resources-plugin

Lock resources against concurrent use
https://plugins.jenkins.io/lockable-resources
MIT License
86 stars 183 forks source link

Lock the whole pipeline base on variable , and excute stage on specific agent #675

Closed oldunclez closed 2 months ago

oldunclez commented 2 months ago

I have a jenkins pipeline job which is trigger by the gitlab webhook. My goal is to :

  1. if the builds for this job with the same value of variable project_branch , these builds should be queued.
  2. if the builds for this job with the different value of variable project_branch , these builds should be excuted concurrently.

The code is as following , but agent { label 'YTLdhost' } in stage2 does not work, it is excuted on agent { label 'ubt' }, why ?

Thank you

pipeline {

    environment {
        gitlabBranch = "${env.gitlabBranch ?: env.branch}"
        gitlabSourceRepoSshUrl = "${env.gitlabSourceRepoSshUrl ?: env.gurl}"
        project = "${gitlabSourceRepoName ? gitlabSourceRepoName : (gitlabSourceRepoSshUrl.tokenize('/').last().replace('.git', ''))}"
        project_branch = "${project}_${gitlabBranch}"
    }
    agent { label 'ubt' }
    stages {
        stage('refresh') {
            steps {
                lock(resource: "${env.project_branch}") {
                    script {
                        stage('stage1')
                        sh '''
                            sleep 20
                        '''
                        stage('stage2') {
                            agent { label 'YTLdhost' }
                            sh '''
                                #!/bin/bash
                                set +x
                                hostname
                            '''
                        }
                    }
                }
            }
        }
    }
}
oldunclez commented 2 months ago

if using the following code , agent { label 'YTLdhost' } works., but lock(resource: "${env.project_branch}") not work , I.E. all the builds will be queued even the value of variable project_branch is different

pipeline {
    environment {
        gitlabBranch = "${env.gitlabBranch ?: env.branch}"
        gitlabSourceRepoSshUrl = "${env.gitlabSourceRepoSshUrl ?: env.gurl}"
        project = "${gitlabSourceRepoName ? gitlabSourceRepoName : (gitlabSourceRepoSshUrl.tokenize('/').last().replace('.git', ''))}"
        project_branch = "${project}_${gitlabBranch}"
    }
    agent { label 'ubt' }
    options {
        lock(resource: "${env.project_branch}")
    }
    stages {
        stage('stage1') {
            steps {
                script {
                    sleep 20
                }
            }
        }
        stage('stage2') {
            agent {  label 'YTLdhost' }
            steps {
                sh '''
                    #!/bin/bash
                    set +x
                    hostname
                '''
            }
        }
    }
}
oldunclez commented 2 months ago

According to

https://community.jenkins.io/t/how-to-use-environment-variables-to-lock-a-resource-in-the-options-block/13067/5

I use node("YTLdhost") instead of agent { label 'YTLdhost' } in stage2 , it works

pipeline {

    environment {
        gitlabBranch = "${env.gitlabBranch ?: env.branch}"
        gitlabSourceRepoSshUrl = "${env.gitlabSourceRepoSshUrl ?: env.gurl}"
        project = "${gitlabSourceRepoName ? gitlabSourceRepoName : (gitlabSourceRepoSshUrl.tokenize('/').last().replace('.git', ''))}"
        project_branch = "${project}_${gitlabBranch}"
    }
    agent { label 'ubt' }
    stages {
        stage('refresh') {
            steps {
                lock(resource: "${env.project_branch}") {
                    script {
                        stage('stage1')
                        sh '''
                            sleep 20
                        '''
                        stage('stage2') {
                           node("YTLdhost") {
                                sh '''
                                    #!/bin/bash
                                    set +x
                                    hostname
                                    sleep 30
                                '''
                           }
                        }
                    }
                }
            }
        }
    }
}