Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not to expose commit hash from SCM as an environment variable? #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ababushk
Copy link

@ababushk ababushk commented Aug 19, 2019

I've started to use GitHub Branch Source plugin for my project. While developing my pipeline I was trying to set PR commit status for which I commit hash was needed. I was wondering if there any way to get it without checkout step. It didn't look too impossible - in Multibranch Pipelines and Organization Folders Jenkins "knows" from which commit has Jenkinsfile was taken. I've written the this https://gist.github.com/ababushk//930980d79ab09c74da34be459563f760 and added it to my Global Library. But it looks so "hacky" that I've started to search for a place where other environment variables like CHANGE_ID and BRANCH_NAME are created. I've found BranchNameContributor class to which I've added variable that I desire so much.
I'm completely new in Jenkins plugin development and Java in general, but I'm wondering why such simple thing as commit hash is not available by default.

Copy link
Contributor

@bitwiseman bitwiseman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting this PR!

This is a common request and it is great to get some traction on it.
Unfortunately, the situation is a little more complex that it might seem at first.

Perhaps we could discuss a bit and I can help you figure what something that will work overall.

Thanks!

SCMHead head = branch.getHead();
// Note: not using Branch.name, since in the future that could be something different
// than SCMHead.name, which is what we really want here.
envs.put("BRANCH_NAME", head.getName());
if (rev != null) {
envs.put("CHANGE_REVISION", rev.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that rev.toString() is the right thing here, as it is intended to be
"human-readable". It may work in some of implementations but it, I'm not sure.

It looks like SCMRevision doesn't have a method for what you're trying to do. There probably needs to be a getId() method added to that in order for this to work reliably. Note, we'll also need to handle ChangeRequestSCMRevision differently since it has several possible revisions that users would be interested in.

It might be better if each SCM source implemented their own environment contributor.

@ababushk
Copy link
Author

ababushk commented Oct 8, 2019

Hi @bitwiseman , thanks for taking a look at this!
My great desire is to retrieve information about revision from which Jenkinsfile was taken without calling 'checkout' step (which requires node context).
This PR wasn't submitted as a complete solution, it is just quick'n'dirty way to get revision string. I'm completely new at Java and Jenkins plugins development, the only source of knowledge for me is doc files in this repository :) I agree that SCM source implementations should be able to contribute to the environment, I'll be able to add something like 'revision timestamp' then, and the way to retrieve it differs for GitHub and GitLab for example. Is there a way for an SCM Source implementation to contribute to the environment?

@jglick
Copy link
Member

jglick commented Jul 7, 2020

I was wondering if there any way to get it without checkout step. It didn't look too impossible

The return value of this step should be a map containing the information you need.

@jglick
Copy link
Member

jglick commented Jul 7, 2020

(Though beware GitHub PR merge builds.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants