non-master branch is not being build in Jenkins - jenkins-pipeline

My code repo is private on bitbucket and my parent bbranch is masster-php7 instead of master.
I created a bbranch from master-php7 and did some changes. Now I want to build from this branch but still fail.
On dashboard this message print after 10 minutes
git fetch --no-tags --progress -- https://abc#bitbucket.org/def/xyz.git
+refs/heads/:refs/remotes/origin/ # timeout=10 ERROR: Checkout failed java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:502) at java.lang.UNIXProcess.waitFor(UNIXProcess.java:395) at hudson.Proc$LocalProc.join(Proc.java:325) at hudson.Proc.joinWithTimeout(Proc.java:171) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2423) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2044) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:81) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:569) at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:907) at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1131) at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1167) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:124) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:155) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:69) at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:303) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:427) ERROR: Maximum checkout retry attempts reached, aborting
In docker logs "WARNING o.j.p.w.flow.FlowExecutionList#unregister:
Owner[asif_copy_test_convo_on_devstack_before_build/1:asif_copy_test_convo_on_devstack_before_build #1] was not in the list to begin with: []"

The UI you have included is not for defining which branch to build, it is a part of Job configuration.
With the GUI as above, you are telling Jenkins that your job is a Pipeline job and it needs to run a Jenkinsfile. Jenkins needs to get the Jenkinsfile from Source Control (git), so it needs to know how to get that file from there. In this GUI you define it. The Repository resides at the URL you provided, the credentials are as provided, the branch name is */asif_imap_ev2-..., the path to Jenkinsfile inside the repository is "Jenkinsfile" (e.g. the file resides at repo root). Jenkins tries to checkout the Jenkinsfile according to the above instructions and fails, probably because the branch name is invalid (it contains */).

Related

wrapper for git, for jenkins pipeline, on windows 10 jenkins slave

What I am trying to accomplish - I want to have a wrapper for git itself, that will retry if the git command fails (timeout/network issue).
I have following dummy code of wrapper:
"C:\Program Files\Git\cmd\git.exe" %*
echo Success!
After changing $PATH and saving the file as git.cmd and removing path to git.exe it works well if I call any git command directly from jenkins:
script {
bat 'git clone %REPO%'
}
result:
(...)
Updating files: 100% (5314/5314), done.
17:58:51 E:\jenkins\workspace\sandbox>echo Success!
17:58:51 Success!
However I want it to work for multibranch pipeline, which uses jenkins git plugin
https://github.com/jenkinsci/git-plugin/blob/master/README.adoc
And running checkout with this plugin results in:
18:48:46 Caused by: java.io.IOException: Cannot run program "git" (in directory "E:\jenkins\workspace\PR-XXXX"): CreateProcess error=2, The system cannot find the file specified
Somehow the git plugin knows, it is missing the git.exe and it is not picking up the git.cmd wrapper script as in the first example. Any suggestions, Guys?
The Git Plugin uses String getGitExe() which returns the git exe for builtOn node, often "Default" or "jgit".
So check first, as in "Relevance of specifying Tool Locations in Jenkins Node Configuration", if you can specify the full path of your git.cmd wrapper in the Git (Default) field of your node (agent) tool configuration.
The Wojtas.Zet confirms in the comments:
I looked the source code of git-plugin and realized there is a way to define custom git executable in the jenkins node GUI
I have clicked it in Jenkins GUI - Jenkins ---> Nodes ---> NodeA (Node properties ---> Tool Locations ---> List of tool locations)

Change repo checkout agent for reading Jenkinsfile in pipeline

I have a git repo that contains multiple Jenkinsfiles. These Jenkinsfiles contain various agent directive to control which node they are running on.
However, the first step of the job, which is reading the Jenkinsfile itself, is defined on the Jenkins website (since it cannot be contained in the Jenkinsfile itself). Namely, I use the "Pipeline script from SCM" section, with the repo and credentials correctly set up.
The problem is that this first checkout step is not always being executed on the master node. It is sometimes - seemingly randomly - performed on another agent which has no SSH access to the repo (public key). Is there any way I can control on which node this checkout is executed?
When the checkout is performed correctly on the master node, the Jenkinsfile redirects it to another node which performs the rest of the job without any issues.
This is what it looks like:
agent1 runs a (lightweight) checkout
agent1 reads the Jenkinsfile
The Jenkinsfile contains agent { label 'agent2' }
The rest of the Jenkinsfile is executed on agent2
I would like to force agent1 to be a certain agent (the master node, specifically). Is there any way I can do this?

Can't fetch from same repository in Bitbucket Pipelines

I'm trying to do the following with Bitbucket Pipelines when i push to my test brand:
- git fetch
- git checkout master
- git pull origin test
- git push origin master
But i get the following message on git fetch: Permission denied (publickey). I was following this tutorial https://confluence.atlassian.com/bitbucket/use-ssh-keys-in-bitbucket-pipelines-847452940.html so i already added an ssh key, but cannot understand what are the next steps that i need for the execution to have permissions to connect to the repository.
If “from same repository” really means that you want to access the repository in which the pipeline runs, the answer is: you don’t need that. When the pipeline runs, it starts the Docker image you defined in your YAML configuration and automatically checks out the commit you pushed. This means that at the moment when when your command (git fetch) is executed, the sources are already waiting for you in path /project.
BitBucket Pipelines automatically checks out the repository upon running. However, if you want to make changes to the repository (e.g. git tag or git push) you will need to add the SSH keys according to the post you have already found (https://confluence.atlassian.com/bitbucket/use-ssh-keys-in-bitbucket-pipelines-847452940.html). That works for our environment.
Can you post the full bitbucket-pipelines.yml file?

Increase version after commit to master in VSTS

Our application uses VSTS for our CI flow and we have a requirement that the patch version should increase by one each time code is merged back into master.
We have created a shell script that will bump the version of the application and tag the repo, but now we are looking for the best place in the flow to inject it. We've thought of the following:
Master commit hook build - The problem with putting this here is that we have protected the master branch with certain policies (must resolve all comments, project must build, etc). Because of this, the build agent runs the script does not have access to push changes.
Pull Request build - This option would integrate the script into the build job that is used to verify the build. This actually works, but when the updates are pushed to the branch, this triggers an infinite build loop because the PR automatically rebuilds the branch. In general, this option seems more brittle.
I would love to have option 1 work out, but despite all attempts to grant the agent proper roles, it still does not have access to the repo. We're getting the following error:
TF402455: Pushes to this branch are not permitted; you must use a pull request to update this branch.
Is there a standard way to do this that I'm missing?
To update the file version and add tag for the new commit through CI build, you can add a PowerShell task. detail steps as below:
Set permission and configure in build definition
First go to Version control Tab, allow below items for Build Service:
Branch creation: Allow
Contribute: Allow
Read: Inherited allow
Tag creation: Inherited allow
Then in your build definition, add the variable system.prefergit as true in Variable Tab, and enable Allow script to access OAuth token in Options Tab.
More details, you can refer Run Git commands in a script.
Add powershell script
git -c http.extraheader="AUTHORIZATION: bearer %SYSTEM_ACCESSTOKEN%" fetch
git checkout master
# Get the line of the file that contain the version
# split the line, and only get the version number major.minor.patch.
# check the version numbers (such as if patch less than 9) and increase the version
#replace the old version with new version in the file
git add .
git commit -m 'change the version as $newVersion'
git tag $newVersion
git push origin master --tags
Note: Even the new version and tag can push to remote repo successful, the PowerShell task may failed. So you should set the task after PowerShell task with custom option as Even if a previous task has failed.
You can try to update your branch policy per documentation on https://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies?view=azure-devops and https://learn.microsoft.com/en-us/azure/devops/organizations/security/permissions?view=azure-devops#git-repository-object-level
Try updating your branch policy with to "Allow" "Bypass policies when pushing"

Location of Git Executable for Jenkins on Windows

Which is the correct installation directory for Jenkins to use? Here are the options I have tried and the results I have seen.
C:\Git\bin\git.exe
C:\Git\cmd\git
same as above
C:\Git\cmd\gitk.cmd
If I continue and ask the job to build here is the console output.
Started by an SCM change
Building in workspace C:\Jenkins\workspace\git_test
Checkout:git_test / C:\Jenkins\workspace\git_test - hudson.remoting.LocalChannel#12f2468
Using strategy: Default
Cloning the remote Git repository
Cloning repository ssh:///jenkins#xxxx.yyyyyyyyy.com:test.git
git --version
Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
Fetching upstream changes from origin
Seen 0 remote branches
No candidate revisions
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE
Since they all result in one error or another its not clear which one is correct.
This is not a problem with git not being found but that putty has not yet been told that it can trust the ssh-key presented by the repository sshd server.
Run a manual git command first on the Jenkins server, so you can say yes to accept the host key, and then try again.

Resources