Jenkins pipeline with multiple agents
1 min readJun 15, 2021
How to run multiple agents on a single jenkins pipeline
set agent to none
inside each stage define desired agent
see an example below
properties([
parameters([
string(name: 'GERRIT_REFSPEC'),
])
])
def get_yaml() {
node {
sh 'env'
echo GERRIT_PATCHSET_REVISION
echo "${GERRIT_PATCHSET_REVISION}"
return """
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: some-label-value
spec:
containers:
- name: simplekube
image: dhub.net/jenkins/simplekube:${GERRIT_PATCHSET_REVISION}
command:
- cat
tty: true
securityContext:
runAsUser: 0
"""
}
}
pipeline {
environment {
VIRTUAL_USERS = 40000
TEST_SCRIPT = '/scenarios/login.js'
YAML_FILE = get_yaml()
}
agent none
stages {
stage('Build docker image and push to dhub') {
agent { label 'docker' }
steps {
sh '''
git checkout ${GIT_REF}
image_with_tag="dhub.net/jenkins/simplekube:$(git rev-parse HEAD)"
docker build --pull -t simplekube:latest -t "$image_with_tag" --build-arg GIT_DESCRIBE=$(git rev-parse HEAD) ./jenkins_k8s/.
docker tag "$image_with_tag" "$image_with_tag"
docker push "$image_with_tag"
'''
}
}
stage('Performance Test') {
agent {
kubernetes {
yaml env.YAML_FILE
}
}
steps {
script {
def stages = [: ]
int pod_count = env.VIRTUAL_USERS.toInteger()/10000
for (int i = 0; i < pod_count; i++) {
stages[i] = {
// node('k6node') { // request for dedicate node for k6 tests (consistent load generation)
stage("Stage-${i}") {
container('simplekube') {
sh 'k6 version'
sh 'k6 run /scenarios/login.js'
}
}
// }
}
}
parallel stages
}
}
}
}
}