Unable to run perforce commands via jenkins scripted pipeline - jenkins

I have one script test_run.py that internally runs few perforce/p4 commands. So in order to run perforce commands 1st we need to authenticate to perforce. So i have created p4 credentials named
:p4_creds with my login credentials In Jenkins
stage('Run script'){
withCredentials([usernamePassword(credentialsId: 'p4_creds', usernameVariable: 'USERNAME', passwordVariable: 'P4PASSWD')]){
sh '''
export P4USER=${USERNAME}
export P4PASSWD=${P4PASSWD}
export P4CLIENT=${jenkins-${NODE_NAME}-${JOB_NAME}}
chmod +x test_run.py
python2 test_run.py
'''
}
}
When i try to run the job, It fails with Perforce password (P4PASSWD) invalid or unset. , I am following this :- https://issues.jenkins.io/browse/JENKINS-58209 to setup my pipeline. Am i missing something?

Related

Jenkins build failed: gradlew: not found

I want to configure Jenkins 2.375.2 to build gradle project. But when I configure the pipe using Blue Ocean plugin and I run the pipeline I get error:
+ ./gradlew build
/var/lib/jenkins/workspace/jenkins_master#tmp/durable-dcccf1cd/script.sh: 1: ./gradlew: not found
Jenkins file:
pipeline {
agent any
stages {
stage('Build Image') {
steps {
sh "echo 'building..'"
// configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
git credentialsId: '8f6bc3ab-9ef5-4d89-8e14-4972d63325c5 ', url: 'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', branch: 'master'
// execute Java -jar ... and build docker image
sh './gradlew build'
sh 'docker build -t springio/gs-spring-boot-docker .'
}
}
}
I tried to add Gradle config
But still I get the same error. Do you know how I can fix this issue?

Jenkins Docker Pipeline Plugin: Get image building logs

Is there a way to extract the build logs when building a Docker image in a Jenkins pipeline using the "Docker Pipeline Plugin" solution docker.build()?
I want to be able to save these logs somewhere else (Jenkins console output):
I am able to get build logs of other shell commands (such as mvn install) very easily by using the sh() method, and then save the console output into a variable:
BUILD_JAR_LOGS = sh (
script: 'mvn install',
returnStdout: true
)
I know that I could use the same method with docker and do something like this:
BUILD_DOCKER_IMAGE_LOGS = sh (
script: 'docker build --network-host .',
returnStdout: true
)
However, I'd prefer to continue using the Docker Pipeline Plugin methods to achieve the actual image building.

How to use .env File with Jenkins

I have a Jenkins Pipeline to run my test suite in Cypress, in the cypress project i use a .env file to store sensible data like user credentials to execute the tests.
How can i set the process.env on Jenkins to use in my cypress project?
You can store your credentials in a Jenkins credentials store. Then you can use these credentials within the pipeline like below.
withCredentials([string(credentialsId: 'mytoken', variable: 'TOKEN')]) {
sh '''
curl -H "Token: $TOKEN" https://some.api/
'''
}
If you want to add them to a .env file you can add them to a file like below.
withCredentials([string(credentialsId: 'mytoken', variable: 'TOKEN')]) {
sh '''
echo "mytoken=$TOKEN" >> process.env
'''
}

how to go inside a specific directory and run commands inside it in a jenkins pipeline

I am trying to run a gradle command inside a jenkins pipeline and for that i should cd <location> where gradle files are.
I added a cd command inside my pipeline but that is not working. I did this
stage('build & SonarQube Scan') {
withSonarQubeEnv('sonarhost') {
sh 'cd $WORKSPACE/sonarqube-scanner-gradle/gradle-basic'
sh 'echo ${PWD}'
sh 'gradle tasks --all'
sh 'gradle sonarqube --debug'
}
}
But the cd is not working, I tried dir step as suggested in pipeline docs, but i want to cd inside $WORKSPACE folder.
How can i fix this?
Jenkins resets the directory after each command. So after the first sh, it goes back to the previous location. The dir command is the correct approach, but it should be used like this:
dir('') {
}
Similar to how you have used withSonarQubeEnv
Alternatively, you can simply chain all the commands
sh 'cd $WORKSPACE/sonarqube-scanner-gradle/gradle-basic & echo ${PWD} & ...'
But this is not recommended. Since this will all be in the same command, it will run fine though.

How to pass jenkins passwords into ansible playbook via pipeline

How to inject passwords to the build as environment variables(these are job passwords) for deployment through ansible via pipeline or dsl script
First, those job passwords should be registered as credentials inside Jenkins.
Second, you can use said file when calling your ansible-playbook command, through the Credentials Binding plugin.
See "How to use multiple credentials in withCredentials in Jenkins Pipeline"
node {
withCredentials([
usernamePassword(credentialsId: credsId1, usernameVariable: 'USER1', passwordVariable: 'PASS1'),
usernamePassword(credentialsId: credsId2, usernameVariable: 'USER2', passwordVariable: 'PASS2')
...
]) {
sh '''
set +x
ansible-playbook /path/to/ansible-playbook.yml -i /path/to/hosts_list -u AUTO_USER --private-key=/path/to/private-key \
-e $USER1=$PASS1 -e $USER2=$PASS2
'''
}
}
Note: the file should have a JSON content, with your

Resources