How does jenkins declarative pipeline define arrays - kubernetes

docs
I want to define volumes and envVars, but I get an error
pipeline {
agent {
kubernetes {
idleMinutes 5
workspaceVolume nfsWorkspaceVolume(readOnly: false, serverAddress: '127.0.0.1', serverPath: '/data/nfs-data/kubernetes/jenkins/agent')
containerTemplate {
name 'maven'
image 'maven:3.3.9-jdk-8-alpine'
ttyEnabled true
command 'cat'
envVars envVar( key: 'ENV', value: 'test')
}
}
}
}
stages {
stage('Clone') {
steps {
script {
sh 'pwd'
}
}
}
}
}
Running a pipeline script gives following error:
java.lang.UnsupportedOperationException: no known implementation of interface java.util.List is using symbol ‘envVar’
at org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:570)
at org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable.instantiate(UninstantiatedDescribable.java:207)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:466)
at org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:429)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:331)
Caused: java.lang.IllegalArgumentException: Could not instantiate {image=maven:3.3.9-jdk-8-alpine, ttyEnabled=true, name=maven, envVars=#envVar(key=127.0.0.1,value=/data/nfs-data/kubernetes/jenkins/agent), command=cat} for org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:474)
at org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:429)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:331)
What should I do??

Related

Run two Jenkins alongside agents

we have a multibranch pipeline that triggers sup-build
the syntax for this like
pipeline {
stages{
stage('main') {
agent { label "k8s" }
sh 'echo hello.'
}
}
}
I need to add another alongside agent that runs the selenium grid - this agent needs to be active the whole time that the pipeline is active and needs to be deleted after
pipeline {
stages{
stage('main') {
agent { label "k8s" }
sh 'echo hello.'
}
stage('SG') {
agent { label "k8s" }
sh 'run selenuim-grid'
}
}
}
Try with below code
Pipeline {
Agent none
Stages {
stage(‘main’)
{
Agent { labe; “k8s”}
Steps {
Sh ‘echo hello.’
}
}
Stage (‘SG’)
{
Agent {
Kubernetes
{
Label ‘selenium-grid’
Cloud ‘kubernetes’
containerTemplate {
Name ‘selenium’
Image ‘selenium/standalone-chrome’
ttyEnabled true
Command ‘cat’
}}}
Steps
Sh ‘run selenium-grid’
}}}
‘SG’ stage has been added to run selenium grid. Once this stage is completed, the pod and container will be automatically deleted.
Please check Jenkins official page 1 and Pipeline for further reference.

Jenkins dynamic choice parameter to read a ansible host file in github

I have an ansible host file that is stored in GitHub and was wondering if there is a way to list out all the host in jenkins with choice parameters? Right now every time I update the host file in Github I would have to manually go into each Jenkins job and update the choice parameter manually. Thanks!
I'm assuming your host file has content something similar to below.
[client-app]
client-app-preprod-01.aws-xxxx
client-app-preprod-02.aws
client-app-preprod-03.aws
client-app-preprod-04.aws
[server-app]
server-app-preprod-01.aws
server-app-preprod-02.aws
server-app-preprod-03.aws
server-app-preprod-04.aws
Option 01
You can do something like the one below. Here you can first checkout the repo and then ask for the user input. I have implemented the function getHostList() to parse the host file to filter the host entries.
pipeline {
agent any
stages {
stage('Build') {
steps {
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
script {
def selectedHost = input message: 'Please select the host', ok: 'Next',
parameters: [
choice(name: 'PRODUCT', choices: getHostList("client-app","ansible/host/location"), description: 'Please select the host')]
echo "Host:::: $selectedHost"
}
}
}
}
}
def getHostList(def appName, def filePath) {
def hosts = []
def content = readFile(file: filePath)
def startCollect = false
for(def line : content.split('\n')) {
if(line.contains("["+ appName +"]")){ // This is a starting point of host entries
startCollect = true
continue
} else if(startCollect) {
if(!line.allWhitespace && !line.contains('[')){
hosts.add(line.trim())
} else {
break
}
}
}
return hosts
}
Option 2
If you want to do this without checking out the source and with Job Parameters. You can do something like the one below using the Active Choice Parameter plugin. If your repository is private, you need to figure out a way to generate an access token to access the Raw GitHub link.
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select the Host',
name: 'Host',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script:
'return [\'Could not get Host\']'
],
script: [
classpath: [],
sandbox: false,
script:
'''
def appName = "client-app"
def content = new URL ("https://raw.githubusercontent.com/xxx/sample/main/testdir/hosts").getText()
def hosts = []
def startCollect = false
for(def line : content.split("\\n")) {
if(line.contains("["+ appName +"]")){ // This is a starting point of host entries
startCollect = true
continue
} else if(startCollect) {
if(!line.allWhitespace && !line.contains("[")){
hosts.add(line.trim())
} else {
break
}
}
}
return hosts
'''
]
]
]
])
])
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
echo "Host:::: ${params.Host}"
}
}
}
}
}
Update
When you are calling a private repo, you need to send a Basic Auth header with the access token. So use the following groovy script instead.
def accessToken = "ACCESS_TOKEN".bytes.encodeBase64().toString()
def get = new URL("https://raw.githubusercontent.com/xxxx/something/hosts").openConnection();
get.setRequestProperty("authorization", "Basic " + accessToken)
def content = get.getInputStream().getText()

I am getting a vmSize error when trying to deploy a batch service pool using bicep

I have the following Bicep code:
resource pool 'Microsoft.Batch/batchAccounts/pools#2021-06-01' = {
name: '${bs.name}/run-python'
properties: {
scaleSettings: {
fixedScale: {
nodeDeallocationOption: 'TaskCompletion'
targetDedicatedNodes: 1
}
}
deploymentConfiguration: {
cloudServiceConfiguration: {
osFamily: '6'
}
}
vmSize: 'standard_A1_v2'
startTask: {
commandLine: 'cmd /c "pip install azure-storage-blob pandas"'
userIdentity: {
autoUser: {
elevationLevel: 'NonAdmin'
scope: 'Pool'
}
}
waitForSuccess: true
}
}
dependsOn: [
bs
]
}
Where I try to create a pool for my batch service, but when I try to deploy it, I get the following error:
{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"PropertyName","message":"vmSize"}]}
So I think the problem is the vmSize, but there I cannot find concrete example on what the value should be.
CloudServiceConfiguration Pools are deprecated. Please retry using VirtualMachineConfiguration.

Conditional environment variables in Jenkinsfile

My Jenkinsfiles use the environment{} directive. I've been trying to set a condition where I invoke different variables depending on the GIT branch being builded.
I've tried something like e. g. :
switch(branch_name) {
case 'dev' :
Var1 = x;
case 'master' :
Var1 = y;
}
That would allow me skip using different Jenkinsfiles for each branch on a repo. But groovy syntax seems not to work inside this environment{} directive.
Is there a way tackling it? Or would you suggest another approach for handling different global variables for each branch?
You can use when declarative in your declarative pipeline. More info here
pipeline {
agent any
stages {
stage('Example Build') {
steps {
echo 'Hello World'
}
}
stage('Deploy to PROD') {
when {
branch 'production'
environment name: 'DEPLOY_TO', value: 'production'
}
steps {
echo 'Deploying'
}
}
stage('Deploy to DEV') {
when {
branch 'dev'
environment name: 'DEPLOY_TO', value: 'dev'
}
steps {
echo 'Deploying'
}
}
}
}

jenkins pipeline - both of groovy and powershell variables in powershell command

I trying to define powershell variable inside jenkins pipeline with the WORKSPACE variable in value. Is there a way to do this?
stage ('BUILD') {
steps {
powershell ("""Write-Output ${WORKSPACE}\\One\\Two\\""")
}
}
Output:
[Pipeline] powershell
18:18:15 C:\buildenv\Jenkins\workspace\project\One\Two\
[Pipeline] }
[Pipeline] // stage
That's nice. Now i need to wrap it into variable:
stage ('BUILD') {
steps {
powershell ("""$name02=${WORKSPACE}\\One\\Two\\""")
}
}
Output now:
groovy.lang.MissingPropertyException: No such property: name02 for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:50)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:134)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:679)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:414)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:412)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:678)
name02=''
pipeline {
agent any
stages{
stage ('BUILD') {
steps {
script{
name2 = powershell label: '', returnStdout: true, script: "return \"${Workspace}\\One\\Two\\\""
}
}
}
stage ('Print') {
steps {
echo "name2 = ${name2}"
}
}
}
}
pipeline {
agent any
stages{
stage ('BUILD') {
steps {
script{
powershell """
\$5name2 = "${Workspace}\\One\\Two\\"
Write-Output "name2 = \$5name2"
"""
}
}
}
}
}
def result = powershell returnStdout: true, script: '''
$result = ${WORKSPACE}\\One\\Two\\
return $result
'''