Trigger a script on windows remote environment (VM) using jenkins - jenkins

I have 2 windows servers, one server(development) has Jenkins on it and both the servers have OPEN SSH installed in them.
I want to trigger the script(batch script) on my testing server through Jenkins. I tried writing the pipeline but it is showing an error.
I refered to https://github.com/jenkinsci/ssh-steps-plugin and wrote a pipeline as given below.
node {
def remote = [http://inb*****.com]
remote.name = 'inb***'
remote.host = 'http://inb*****.com'
remote.user = 'jenkinsSSH'
remote.password = '********34'
remote.allowAnyHosts = true
stage('Remote SSH') {
writeFile file: 'abc.bat', text: 'ls -lrt'
sshScript remote: remote, script: "abc.bat"
}
}
The output which I am getting is given below,
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 12: unexpected token: } # line 12, column 1.
}
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:144)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:100)
at hudson.model.Executor.run(Executor.java:433)
Finished: FAILURE
I have written a simple batch file - " echo hello " and saved it as abc.bat in the test server.
This is where I am stuck, If there is another method to run the script on the testing server through Jenkins then also please let me know it will be of great help.

You have an error in this line:
def remote = [http://inb*****.com]
It should be
def remote = [:]
to create an empty map.

Related

Import properties file from jenkins

I'm using Jenkins 2.289.2 on RHEL8.
I tried to read a property file from the pipeline.
def properties = readProperties file :'my.properties'
When I build the project I got the below error. I installed Plugin Utilities API plugin in Jenkins. Am I missing any steps?
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 35: Expected a step # line 35, column 9.
def properties = readProperties file :'my.properties''
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
If you are using a declarative pipeline this kind of statement must reside inside a script block. In general any scripted pipeline syntax or plain groovy DSL should be encapsulated inside a script block when used in a declarative pipeline.
You can read more about it in the Pipeline Syntax documentation.
To solve it try using something like:
pipeline {
agent any
stages {
stage('Hello') {
steps {
script {
def properties = readProperties file :'my.properties'
// Rest of code
}
}
}
}
}

Jenkinsfile 2 erros

this is my simple jenkinsfile:
pipeline {
agent any
stages {
stages ('Build') {
steps {
sh 'echo "Hello World" '
sh '''
echo "Multiline shell steps works too"
ls -lah
'''
}
}
}
}
But when i was executing this in jenkins i'm getting the below errors:
Branch indexing
Connecting to https://api.github.com using admin/****** (GitHub Access Token)
Obtained Jenkinsfile from 030d6e5dc55356678255ca506b05e8cc223d15a6
Running in Durability level: MAX_SURVIVABILITY
GitHub has been notified of this commit’s build result
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: Expected a stage # line 4, column 9.
stages ('Build') {
^
WorkflowScript: 3: No stages specified # line 3, column 5.
stages {
^
2 errors
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
Any idea what i am missing here. Thanks

How to set docker file in jenkin pipeline stage

I have a simple JenkinsFile uploaded in my git repo
pipeline {
agent any
stages {
stage('Initialize'){
def dockerHome = tool 'myDocker'
env.PATH = "${dockerHome}/bin:${env.PATH}"
}
stage('Push to Docker Registry'){
withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)
}
}
}
}
In jenkins, i have created a pipeline project and just passed path to my git repo.
myDocker is defined in global tool configuration.
dockerhub credentials are defined in jenkins->credentials.
On build , i am getting this:
Started by user ********
Obtained Jenkinsfile from git https://github.com/*****/*****.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: Not a valid stage section definition: "def dockerHome = tool 'myDocker'". Some extra configuration is required. # line 4, column 9.
stage('Initialize'){
^
WorkflowScript: 4: Not a valid stage section definition: "env.PATH = "${dockerHome}/bin:${env.PATH}"". Some extra configuration is required. # line 4, column 9.
stage('Initialize'){
^
WorkflowScript: 8: Unknown stage section "withCredentials". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. # line 8, column 9.
stage('Push to Docker Registry'){
^
WorkflowScript: 4: Expected one of "steps", "stages", or "parallel" for stage "Initialize" # line 4, column 9.
stage('Initialize'){
^
WorkflowScript: 8: Expected one of "steps", "stages", or "parallel" for stage "Push to Docker Registry" # line 8, column 9.
stage('Push to Docker Registry'){
^
5 errors
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:327)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
I am sure , i am making any small mistake. Please redirect me to right direction.
You need to wrap your code within steps inside a stage like below.
pipeline {
agent any
stages {
stage('Initialize'){
steps {
def dockerHome = tool 'myDocker'
env.PATH = "${dockerHome}/bin:${env.PATH}" }
}
stage('Push to Docker Registry'){
steps {
withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)
}
}
}
}}
Documentation Link. Creating Pipeline

Jenkins pipeline script error WorkflowScript: Expected a step

I have created a testcase from a much bigger Jenkins pipeline project and it works fine.
pipeline {
agent none
stages {
stage ("Check Parameters")
{
steps {
echo "In pipeline"
script {
echo "Start condition check"
}
build job: 'printuser'
//def slaveJob = build job: 'printuser'
//println slaveJob.rawBuild.log
}
}
}
}
Howevr, inorder to get the output of printuser job onto the pipeline i comment build job: 'printuser' and uncomment the slaveJob code as below:
//build job: 'printuser'
def slaveJob = build job: 'printuser'
println slaveJob.rawBuild.log
It now fails with the below error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 13: Expected a step # line 13, column 15.
def slaveJob = build job: 'printuser'
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:133)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:126)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
Can you please suggest what is the issue here ?
If you want to use groovy, you must put it inside the script section, not outside:
script {
echo "Start condition check"
build job: 'printuser'
def slaveJob = build job: 'printuser'
println slaveJob.rawBuild.log
}
PS: not sure your code will work as such, but you should at least put it inside the script part. Outside of the script closure, jenkins will only accept a set of predefined steps.

How to use jenkins version number plugin in pipeline

I am using version number jenkin plugin in Jenkins ver. 2.107.2. below pipeline code is not working. any help to fix this issue?
pipeline code
pipeline {
agent any
stages {
stage('Pre-Build') {
steps {
sh 'echo Building Docker'
def tag = VersionNumber (versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
}
}
}
}
Error:
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 8: Expected a step # line 8, column 1.
def tag = VersionNumber (versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:325)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
this link How to use jenkins version number plugin in Jenkinsfile? has answer but not working for me.
You are using the declarative Pipeline, which has some restrictions regarding the commands which can be run inside a stage.
Take a look at the syntax guide for declarative pipelines.
You can either use the scripted pipeline or declare the variable in an environment block:
environment {
tag = VersionNumber(versionNumberString: '${BUILD_DATE_FORMATTED,"yyyyMMdd"}-develop-${BUILDS_TODAY}');
}
and later use it as $tag in your pipeline.

Resources