I want to loop my repository to find all manifact datas, and then I want to read the content in the manifest data.
in my jenkins job looks like this:
stage('Read File'){
steps{
script{
def baseDir = "**/MANIFEST.MF"
def dateien = findFiles(glob: baseDir)
echo "datei: ${dateien}"
dateien.each{
echo "File: ${it.name}"
echo "Paht: ${it.path}"
def content = readFile(file: it.paht)
println(content)
}
}
}
the error is it can not find the file.
any solution?
Related
I have a problem with a file in my jenkins workspace, I need to read a diffFile.txt, I'm using the global variable WORKSPACE like this File fileDiff = new File(env.WORKSPACE+"/diffFile.txt") but i get this error. I've checked and the file is there, I can read it with cat, but not with File, do you know what I can do to fix that?
Instead of using File try to use Jenkins native readFile step. Please check the following sample.
pipeline {
agent any
stages {
stage('Stage') {
steps {
script {
// Dummy code to create a file with new entries
sh "echo 'node1' >> nodeList.txt"
sh "echo 'node2' >> nodeList.txt"
sh "echo 'node3' >> nodeList.txt"
// Reading the file
def data = readFile(file: 'nodeList.txt')
for(def line: data.split('\n')) {
echo line
}
}
}
}
}
}
I am working on Jenkins [Multibranch Pipeline]. I configure the GitHub and its working fine.But I am facing an issue when I am trying to deploy the GitHub project to SALESFORCE production. I also placed Jenkins file in GitHub project.
Here is the code of my Jenkins file
#!groovy
import groovy.json.JsonSlurperClassic
node {
def BUILD_NUMBER=env.BUILD_NUMBER
def RUN_ARTIFACT_DIR="tests/${BUILD_NUMBER}"
def SFDC_USERNAME
def HUB_ORG=env.HUB_ORG_DH
def SFDC_HOST = env.SFDC_HOST_DH
def JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH
def CONNECTED_APP_CONSUMER_KEY=env.CONNECTED_APP_CONSUMER_KEY_DH
println 'KEY IS'
println JWT_KEY_CRED_ID
println HUB_ORG
println SFDC_HOST
println CONNECTED_APP_CONSUMER_KEY
def toolbelt = tool 'toolbelt'
stage('checkout source') {
// when running in multi-branch job, one must issue this command
checkout scm
}
withCredentials([file(credentialsId: JWT_KEY_CRED_ID, variable: 'jwt_key_file')]) {
stage('Create Scratch Org') {
rc = bat returnStatus: true,
script: "${toolbelt}/sfdx force:auth:jwt:grant --clientid ${CONNECTED_APP_CONSUMER_KEY} --username ${HUB_ORG} --jwtkeyfile ${jwt_key_file} --setdefaultdevhubusername --instanceurl ${SFDC_HOST}"
if (rc != 0) { error 'hub org authorization failed' }
// need to pull out assigned username
rmsg = bat returnStdout: true, script: "${toolbelt}/sfdx force:org:create --definitionfile config/project-scratch-def.json --json --setdefaultusername"
printf rmsg
def jsonSlurper = new JsonSlurperClassic()
def robj = jsonSlurper.parseText(rmsg)
if (robj.status != 0) { error 'org creation failed: ' + robj.message }
SFDC_USERNAME=robj.result.username
robj = null
}
}
}
I am trying to authorize salesforce by useing this line of code.
rc = bat returnStatus: true,
script: "${toolbelt}/sfdx force:auth:jwt:grant --clientid ${CONNECTED_APP_CONSUMER_KEY} --username ${HUB_ORG} --jwtkeyfile ${jwt_key_file} --setdefaultdevhubusername --instanceurl ${SFDC_HOST}"
But when i run jenkins I am getting this error.
'cmd' is not recognized as an internal or external command, operable program or batch file.
I WILL APPRECIATE IF ANYONE WILL HELP ME
I want to parametrize my Jenkins pipeline with a simple properties config file
skip_tests=true
that I've added to Jenkins Config File Managment:
In my pipeline I'm importing this file and try to read from it using the Jenkins Pipeline Config File Plugin.
node('my-swarm') {
MY_CONFIG = '27206b95-d69b-4494-a430-0a23483a6408'
try {
stage('prepare') {
configFileProvider([configFile(fileId: "$MY_CONFIG", variable: 'skip_tests')]) {
echo $skip_tests
assert $skip_tests == 'true'
}
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
print e
}
}
This results in an error:
provisioning config files...
copy managed file [my.properties] to file:/home/jenkins/build/workspace/my-workspace#tmp/config7043792000148664559tmp
[Pipeline] {
[Pipeline] }
Deleting 1 temporary files
[Pipeline] // configFileProvider
[Pipeline] }
[Pipeline] // stage
[Pipeline] echo
groovy.lang.MissingPropertyException: No such property: $skip_tests for
class: groovy.lang.Binding
Any ideas what I'm doing wrong here?
With the help of the other answers and How to read properties file from Jenkins 2.0 pipeline script I found the following code to work:
configFileProvider([configFile(fileId: "$PBD1_CONFIG", variable: 'configFile')]) {
def props = readProperties file: "$configFile"
def skip_tests = props['skip_tests']
if (skip_tests == 'true') {
print 'skipping tests'
} else {
print 'running tests'
}
}
I had to use readProperties from Jenkins' Pipeline Utility Steps Plugin.
Since the file is in property format you can use it in a shell step:
sh """
source ${MY_CONFIG}
.
.
.
"""
You would need to export the properties that need to be available on programs that the shell calls (e.g. Maven)
You made a wrong usage of Groovy GString, you should wrap $skip_tests in " or use skip_tests directly.
configFileProvider([configFile(fileId: "$MY_CONFIG", variable: 'skip_tests')]) {
echo skip_tests
assert skip_tests == 'true'
echo "$skip_tests"
assert "$skip_tests" == 'true'
}
Note: the value of skip_tests is the file path of the config file which is copied from master to job's workspace. It's not the content of the config file.
I am trying a pipeline script in which I need to open a file and change some text in . So my script goes like this :import java.io.File
node {
stage('File settings') {
dir ('gitfile') {
dir('config') {
sh 'dir'
sh 'pwd > outFile'
curPath = readFile 'outFile'
echo "The current date is ${curPath}"
def file = new File("${curPath}/"+"const.js")
def lines = file.readLines()
println "${file} has ${lines.size()} lines of text"
println "Here is the first line: ${lines[0]}"
println "Here is the last line: ${lines[lines.size()-1]}"
}
}
}
}
But I get error like :
java.io.FileNotFoundException: /var/lib/jenkins/workspace/Daily/smoke/config
/const.js (No such file or directory)
But the file is present in that location. Please let me know why this error happens.
You should use the readFile() and writeFile() Jenkins pipeline steps to manipulate file contents on the workspace directory. See https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/
Trying to get this pipeline working..
I need to prepare some variables (list or string) in groovy, and iterate over it in bash. As I understand, groovy scripts run on jenkins master, but I need to download some files into build workspace, that's why I try to download them in SH step.
import groovy.json.JsonSlurper
import hudson.FilePath
pipeline {
agent { label 'xxx' }
parameters {
...
}
stages {
stage ('Get rendered images') {
steps {
script {
//select grafana API url based on environment
if ( params.grafana_env == "111" ) {
grafana_url = "http://xxx:3001"
} else if ( params.grafana_env == "222" ) {
grafana_url = "http://yyy:3001"
}
//get available grafana dashboards
def grafana_url = "${grafana_url}/api/search"
URL apiUrl = grafana_url.toURL()
List json = new JsonSlurper().parse(apiUrl.newReader())
def workspace = pwd()
List dash_names = []
// save png for each available dashboard
for ( dash in json ) {
def dash_name = dash['uri'].split('/')
dash_names.add(dash_name[1])
}
dash_names_string = dash_names.join(" ")
}
sh "echo $dash_names_string"
sh """
for dash in $dash_names_string;
do
echo $dash
done
"""
}
}
}
}
I get this error when run..
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: dash for class: WorkflowScript
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:42)
Looks like I'm missing something obvious...
Escape the $ for the shell variable with a backslash, that should help:
for dash in $dash_names_string;
do
echo \$dash
done
the problem is on line three here:
for dash in $dash_names_string;
do
echo $dash
done
it's trying to find a $dash property in groovy-land and finding none. i can't actually think how to make this work vi an inline sh step (possibly not enough sleep), but if you save the relevant contents of your json response to a file and then replace those four lines with a shell script that reads the file and call it from the Jenkinsfile like sh './hotScript.sh', it will not try to evaluate that dollar value as groovy, and ought to at least fail in a different way. :)