P4 Plugin failed in jenkins - jenkins

I am trying to set up a job in Jenkins using this p4 plugin. I successfully installed the plugin. While I am trying to run perforce commands in execute shell I got below exception.
[workspace] $ /bin/sh -xe /tmp/hudson6165069639422088214.sh
+ echo 'Trunk Release is r2017.1.0'
Trunk Release is r2017.1.0
++ /usr/local/perforce/p4 -P E208C3B005AB1F8E7C138F7156F857EC counter cos_sso_trunk_build_number
User jenkins doesn't exist.
+ BLD_NUM=
Build step 'Execute shell' marked build as failure
Warning: you have no plugins providing access control for builds, so falling back to legacy behavior of permitting any downstream builds to be triggered
Finished: FAILURE
I have included P4 Configuration, Its working fine. But above error is when I will try P4 commands in console earlier Perforce plugin (old) its working fine

It's expecting an account for the user "jenkins". Either create an account for that user, or use an existing user.
On the Configure page, under Source Code Management-->Perforce Software Perforce Credentials, select Add to add the user information.

Related

How to stop a build by buildnumber in a Jenkins pipeline

We uses notes (comments) from Gitlab to issue a couple of user-commands, for example retry a build that seems to have failed from a failed integration etc. We would now like to ask jenkins to stop a build. We have got the buildnumber from the comment (through a callback to to gitlab searching the comments) so we know what build to stop but now we stumbled on the problem. We don't seem to find an api-call to stop the build by buildnumber alone.
We could of course make a request to https://server/project/buildnumber/stop (same url as used by the ui) but then we have to enable the crumbIssuer and according to Ops open for a CSRF-attack.
Is there a way to do this operation from inside a pipeline?
Navigate to Manage Jenkins > Script Console.
Run the following script setting the job name and number of the hung build accordingly:
def build = Jenkins.instance.getItemByFullName("jobName").getBuildByNumber(jobNumber)
build.doStop()
build.doKill()
Otherwise you can create a separate pipeline job and configure the above script
Abort the job build from a cid node (if the previous option did not help):
Log in to any cid node.
Run:
cd /srv/volumes/jenkins/jobs/<job-name>/builds/
rm -rf <hung-build-number>

Terraform Jenkins integration - forcing (bypassing) the auto approval process

I am trying to integrate Terraform (to create an AWS instance) with Jenkins and need to force (bypass) the approval process. I am trying to do it as a freestyle job providing the required information under the "Build ENv" section.
I am able to create an AWS instance from Terraform. Now I am trying to automate the process using Jenkins. The job is failing because I am not able to by pass the approval process.
Building in workspace C:\Program Files (x86)\Jenkins\workspace\TerrafromInstancecreation
[ModuleOne] $ "C:\Program Files (x86)\Jenkins\tools\org.jenkinsci.plugins.terraform.TerraformInstallation\Terrafrom_0.12.6\terraform.exe" get -update
[ModuleOne] $ "C:\Program Files (x86)\Jenkins\tools\org.jenkinsci.plugins.terraform.TerraformInstallation\Terrafrom_0.12.6\terraform.exe" apply -input=false "-state=C:\Program Files (x86)\Jenkins\workspace\TerrafromInstancecreation\terraform-plugin\terraform-plugin.tfstate"
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
[1mEnter a value:[0m [0m
Apply cancelled.
FATAL: java.lang.Exception: Terraform Apply failed: 1
at org.jenkinsci.plugins.terraform.TerraformBuildWrapper.executeApply(TerraformBuildWrapper.java:249)
at org.jenkinsci.plugins.terraform.TerraformBuildWrapper.setUp(TerraformBuildWrapper.java:269)
at hudson.model.Build$BuildExecution.doRun(Build.java:157)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1810)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
I need to force the auto approval process so that the APPLY phase can run through without prompting and then failing.
My sincere request to please provide any scripts / parameters that I need to use . I am performing this as a freestyle jenkins job.
You should use the -auto-approve=true flag when running non interactively. This will also skip the showing of the plan before and just apply out any changes that Terraform deems necessary.

How to parse jenkins artifactory trigger plugin output?

I have setup "Enable Artifactory trigger" on my jenkins job. The job gets triggered automatically whenever there is an update in the artifactory path.
The trigger job output provides the artifact that triggered the build.
What i need is to parse the artifact and use as an environment variable?
Jenkins console log of artifactory trigger job
The build was triggered by a change in Artifactory.
The artifact which triggered the build is: http://xxx.xxx.com/artifactory/api/storage/artifactory-snapshot/ABC/exp-abc-12-5-br/abc-12-5-99-015/up-image.tgz
[EnvInject] - Loading node environment variables.
Building remotely on slave in workspace /data/engit-private-jenkins/workspace/Tools/test_artifactory_trigger
[test_artifactory_trigger] $ /bin/sh -xe /tmp/jenkins6159521155154940953.sh
+ echo 'hello world'
hello world
Finished: SUCCESS
How do I access the http artifactory url in the above jenkins console log in the same job? I dont see it getting stored in the environment
We can get the URL of the file in Artifactory that triggered the job using :
environment {
RT_TRIGGER_URL = "${currentBuild.getBuildCauses('org.jfrog.hudson.trigger.ArtifactoryCause')[0]?.url}"
}
Build trigger documentation link

Jenkins build logs in slave nodes

I am trying to push our jenkins build logs to S3.
I used Groovy plugin and the following script in Build phase
// This script should be run in a system groovy script build step.
// The FilePath class understands what node a path is on, not just the path.
import hudson.FilePath
// Get path to console log file on master.
logFile = build.getLogFile()
// Turn this into a FilePath object.
logFilePathOnMaster = new FilePath(logFile)
logFileName = build.envVars["JOB_BASE_NAME"] + build.envVars["RT_RELEASE_STAGING_VERSION"] + '.txt'
// Create remote file path obj to build agent.
remoteLogFile = new FilePath(build.workspace, logFileName)
// Copy contents of master's console log to file on build agent.
remoteLogFile.copyFrom(logFilePathOnMaster)
And then I am using S3 plugin to push .txt files to S3.
But this script fetches the build log file from the master node.
How are the build logs transferred from slave to master node ?
Can I access the build log file on my slave node without master's involvement at all ?
The slave node must be preserving the build logs while building somewhere ? I cant seem to find it.
I am not much familiar with Groovy but here is the solution which worked for me using shell script.
I am using Jenkins 'Node and Label parameter plugin' to run our java process on a slave node. Job is triggered using 'Build >> Execute Shell' option. The log is collected into a file as below :
sudo java -jar xxx.jar | sudo tee -a ${JOB_NAME}/${BUILD_NUMBER}.log 2>&1
This log file is then pushed to S3 :
sudo aws --region ap-south-1 s3 cp ${JOB_NAME}/${BUILD_NUMBER}.log s3://bucket/JenkinsLogs/${JOB_NAME}/${BUILD_NUMBER}.log
Its working perfectly for us. Hope it helps you too.

Jenkins shell script to git pull to production directory

I have a really simple Jenkins setup where it pulls down updates from BitBucket and runs some tests which all work lovely.
Jenkins and the testing website are both the same server so I want it to head off to my live website directory and pull down the repository that its just tested.
When I try and access : /var/www/vhosts/mysite/httpdocs/whatever/ I get a script error stating that this isn't a directory.
What would be the best way to do this?
Error is as follows:
[Pheme CI] $ /bin/sh -xe /tmp/hudson5490778292870793122.sh
+ cd /var/www/vhosts/mysite.co.uk/httpdocs/
/tmp/hudson5490778292870793122.sh: line 2: cd: /var/www/vhosts/mysite.co.uk/httpdocs/: Not a directory
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Edit this appears to be a permissions issue will update when its sorted!
Right simply put this comes down to the Jenkins user not having access to the directory I needed it to, I simply
chown jenkins <dir>
and it all works fine! There is probably a better way to do this.

Resources