Passing branch parameter from Jenkins to Spinnaker

#1

I have Spinnaker pipeline setup to be triggered from a Jenkins job. Is there a way to pass a parameter from Jenkins to a Spinnaker pipeline with the GitHub branch that we would like to build? If so, how can it be done?

1 Like

#2

You can pass a property file from Jenkins to Spinnaker. You will need to archive a file with the branch info as a part of your Jenkins job and specify the file in the Property File field on the Jenkins trigger in Spinnaker. From there, you can reference the branch through pipeline expressions.

Check out https://www.spinnaker.io/guides/user/pipeline-expressions/#property-files for more information.

2 Likes

#3

Thanks for the info. I’m having a little trouble trying to figure out how to pass the property file from Jenkins to Spinnaker. On the Jenkins job that is used to trigger the Spinnaker pipeline, I’ve added a “Post-build Actions” > “Archive the artifacts” > “Files to archive” and specified a file name to archive. Is that how to do it? If not, can you provide some more details on it’s done?

Also do you have an example of how the property files is used once it’s passed to Spinnaker? I’m having trouble figuring out the correct syntax from the pipeline expressions guide.

0 Likes

#4

Yes, that is the correct way to archive the artifact properties file. After running a build, you should see that file you specified under Build Artifacts section in the status page of that Jenkins build. From there, you add that file name to the properties field in the Jenkins trigger on Spinnaker, and Spinnaker will pick up that the properties in the next pipeline run. You can check if the Spinnaker picked up the artifect properites by looking at the execution json of a pipeline with the Jenkins trigger. The properties should be populated in the trigger.properties section of the json.

For an example,
Our property file looks like:

GIT_COMMIT=1234567

and in the comments section of our bake stage we add

${ trigger.properties[‘GIT_COMMIT’]}

0 Likes

#5

how do I do this? Should I create a property file in the git repo or where else? Can the jenkin job write the branch name to the property file during the execution of the job?

0 Likes

#6

If you commit it to the git repo, it wont be as dynamic, unless you have some bash code to append to that file dynamically.

We add something like this near the end of our jenkins jobs, which then can be used within a pipeline.

echo "---
BUILD_NUMBER: ${BUILD_NUMBER}
BUILD_TAG: ${BUILD_TAG}
GIT_BRANCH: ${GIT_BRANCH}
GIT_COMMIT: ${GIT_COMMIT}
GIT_URL: ${GIT_URL}
JOB_NAME: ${JOB_NAME}
JOB_URL: ${JOB_URL}
" > build_properties.yaml
0 Likes

#7

Thanks Sijis. this helps but from there, can you pass it to the git trigger that we use in configuration stage of spinnaker pipeline. I guess pipeline expression cannot be used in configuration stage of spinnaker pipeline? I’m trying to add the branch name dynamically to the GIT Trigger in spinnaker pipeline. Is that possible in any mean?

0 Likes

#8

From that file, you can use ${ trigger.properties[‘GIT_BRANCH’]} in any of your pipeline stages.

You will have to setup the trigger as such:

The rest of that page gives pretty good examples on what can be done with pipeline expressions.

0 Likes

#9

but that document says that pipeline expression cannot be used in configuration stage but GIT trigger is only available in configuration stages. With that restriction, how to parse the Git branch name in configuration stage in the Branch textbox of the attached image assuming we cant use pipeline expression over there

0 Likes

#10

I suppose not being able to use an expression trigger in the config state would make sense, as that’s what is initiating everything and where you are likely getting any data from.

A way to make that dynamic, is to write tooling that automatically creates/updates a pipeline when a change is desired.

For example, let say in an application repository you have a file named deployments.yaml, with the contents with something like:

---
git_trigger_branch: master
jenkins_job: myapp-job-test
slack_channel: #app1-dev

When you detect a change to that file, then your tool/script (or maybe Spin) will create/update a pipeline for app1 by using some of the data in this deployments.yaml file.

These are just ideas to help you work around some limitations.

0 Likes