Build parameters versus JJB variables -- allowing users to specify Git branch in Build Parameters form

35 views
Skip to first unread message

Aleksey Tsalolikhin

unread,
Jul 7, 2021, 5:17:17 PM7/7/21
to jenkins-job-builder
Hi,

I would like to create a Jenkins job that has a Build Parameters form where the user can enter the name of a Git repo branch and the job will check out that branch of the repo.

I tried using the build parameter name (JJB variable) as the value for "branches" but that doesn't work: I end up with the most recent commit being checked out.

However if I use the build parameter in a shell builder, I can then check out the desired branch.

Example:

---
- job-template:
    name: 'xyzzy-{environment}'
    project-type: freestyle
    parameters:
      - string:
          name: MY_BRANCH
          default: origin/master
          description: "Git branch name"
    scm:
      - git:
          url: ... # redacted
          branches:
            - '{MY_BRANCH}' # this does not work
    builders:
      - shell: |
         git checkout $MY_BRANCH # this does work
 
Questions:
1. Does putting {MY_BRANCH} on the right hand side of scm.git.branches not work because a build parameter is a different animal than a JJB variable?  (Just to verify I understand what's happening.)
2. How is my workaround? Is there a more elegant or JJB-native way to allow users to select the Git branch in the Build Parameters form?

Best,
Aleksey

Thanh Ha

unread,
Jul 7, 2021, 9:42:05 PM7/7/21
to Aleksey Tsalolikhin, jenkins-job-builder
Hi Aleksey,

> 1. Does putting {MY_BRANCH} on the right hand side of scm.git.branches not work because a build parameter is a different animal than a JJB variable?  (Just to verify I understand what's happening.)

You are correct, "parameters" is a Jenkins concept and is not a JJB feature so that's why you cannot assume the "name" field of a parameter as a JJB variable.  Jenkins creates parameters as environment variables so you _should_ be able to call it as the environment variable in the SCM section via "$MY_BRANCH" but I have noticed not all Jenkins modules import all environment variables (I believe publisher modules tend to skip them).

> 2. How is my workaround? Is there a more elegant or JJB-native way to allow users to select the Git branch in the Build Parameters form?

Try calling the environment variable directly in the SCM section it might work as I mentioned above. If not, your workaround is fine. What you are trying to achieve is not a JJB variable but a Jenkins environment variable so I don't think there's a "JJB" way if you want to use user input in parameters as input.

Hope this helps,
Thanh

Aleksey Tsalolikhin

unread,
Jul 8, 2021, 1:12:40 PM7/8/21
to jenkins-job-builder
Thank you, Thanh.  That's very very helpful, and prompt, as always.  Much appreciated!!

Best,
Aleksey

Aleksey Tsalolikhin

unread,
Sep 7, 2021, 7:37:11 PM9/7/21
to Aleksey Tsalolikhin, jenkins-job-builder
Hi,

I just wanted to update this thread.  Calling the env var directly in the SCM section worked.  We had to keep JJB from interpolating the variable:

---
- job-template:
    name: 'xyzzy-{environment}'
    project-type: freestyle
    parameters:
      - string:
          name: MY_BRANCH
          default: origin/master
          description: "Git branch name"
    scm:
      - git:
          url: ... # redacted
          branches:
            - '${{MY_BRANCH}}' # this works

Thanks!
Aleksey
--
You received this message because you are subscribed to the Google Groups "jenkins-job-builder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkins-job-bui...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkins-job-builder/4e596011-e1e5-4233-8d8e-c293db3a001en%40googlegroups.com.


--
Aleksey Tsalolikhin
DevOps - Sr. Systems Engineer | Golf - Scrum Master / Developer | Data Nexus Integration Team - Scrum Master
 LinkedIn  •   Instagram  •   Youtube
 
Reply all
Reply to author
Forward
0 new messages