Martin Olausson
Martin Olausson

Reputation: 371

Jenkins parameterized build: build fails due to git parameter includes {^commit}

I would like to set up a parameterized build so the user can select branch and then press "build" to build that branch.

In configure I have added a git parameter "branch", like this: enter image description here

I have then added the "branch" parameter as branch specifier in the pipeline definition like this: enter image description here

When I start the build, I can select the branch without a problem. In this example /origin/feature/kvarntorp-test1. But the build will fail with the following console output:

Started by user xxxx Lightweight checkout support not available, falling back to full checkout. Checking out git ssh://[email protected]/ermms/acm.git into /var/lib/jenkins/workspace/6smCustomBuild@script to read ci/jenkins/6msBuildAndTest.groovy

> git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository
> git config remote.origin.url ssh://[email protected]/ermms/acm.git # timeout=10 Fetching upstream changes from ssh://[email protected]/ermms/acm.git
> git --version # timeout=10 using GIT_SSH to set credentials
> git fetch --tags --progress ssh://[email protected]/ermms/acm.git +refs/heads/:refs/remotes/origin/
> git rev-parse /origin/feature/kvarntorp-test1^{commit} # timeout=10
> git rev-parse refs/remotes/origin//origin/feature/kvarntorp-test1^{commit} # timeout=10
> git rev-parse /origin/feature/kvarntorp-test1^{commit} # timeout=10

ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
ERROR: Maximum checkout retry attempts reached, aborting Finished: FAILURE

So Jenkins can not find the branch /origin/feature/kvarntorp-test1 because he is searching for /origin/feature/kvarntorp-test1^{commit}

Where did ^{commit} come from? Can I somehow remove the ^{commit} in the build configuration? Have I configured the build wrong?

I have tried different names instead of "branch". Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job suggests I should use sha1 with the default value "master". I tried sha1 as Git Parameter Name and ${sha1} as Branch Specifier. The build failed with the following console output:

hudson.plugins.git.GitException: Command "git fetch --tags --progress origin +refs/heads/${sha1}:refs/remotes/origin/${sha1} --prune" returned status code 128:
stdout: 
stderr: fatal: Couldn't find remote ref refs/heads/${sha1}
fatal: The remote end hung up unexpectedly

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2002)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1721)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:72)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:405)
    at jenkins.plugins.git.GitSCMFileSystem$BuilderImpl.build(GitSCMFileSystem.java:351)
    at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:196)
    at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:172)
    at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:108)
    at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:67)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:298)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

Upvotes: 3

Views: 10410

Answers (2)

Martin Olausson
Martin Olausson

Reputation: 371

I got it working. It was a combination of two things. First, I hade to uncheck Lightweight checkout as described in Jenkins Git Branch not working with Environment Variables.

But that alone did not solve it. I also had to set the parameter name to sha1 and the branch specifier to ${sha1}.

Upvotes: 5

3sky
3sky

Reputation: 890

From SCM/Branches to build's manual:

Specify the branches if you'd like to track a specific branch in a repository. If left blank, all branches will be examined for changes and built.

The safest way is to use the refs/heads/ syntax. This way the expected branch is unambiguous.

If your branch name has a / in it make sure to use the full reference above. When not presented with a full path the plugin will only use the part of the string right of the last slash. Meaning foo/bar will actually match bar If you use a wildcard branch specifier, with a slash (e.g. release/), you'll need to specify the origin repository in the branch names to make sure changes are picked up. So e.g. origin/release/

Possible options:

Tracks/checks out the specified branch. If ambiguous the first result is taken, which is not necessarily the expected one. Better use refs/heads/. E.g. master, feature1,...

So try refs/heads/${branch}

Upvotes: 0

Related Questions