chromatic-cli icon indicating copy to clipboard operation
chromatic-cli copied to clipboard

Support PR builds from Jenkins by using CHANGE_BRANCH

Open markcellus opened this issue 5 years ago • 2 comments

Hello,

We've recently enabled the "UI Review" feature in Chromatic and it doesn't seem to show any changes under the "PRs" tab in the interface. We believe its because we're using Jenkins which sets the branch name to a PR-[NUM] style instead of normal github branch names. I believe when the chromatic cli command gets ran in Jenkins, the branch name is being overridden to the PR- style names so it never shows the changeset in the UI Review feature. I was told that we could manually set the branch name Chromatic CLI uses by simply setting a CHROMATIC_BRANCH environment variable. But when we do this, we still get the same results and doesn't work.

I've tried speaking with the technical support but no luck. Any ideas or information to get UI Review feature working?

Thanks!


@ghengeveld: Jenkins appears to create and checkout a custom PR branch for this type of build, and set GIT_BRANCH to this as well. The CLI should detect and handle this automatically.

When we see Jenkins as CI provider and GIT_BRANCH is formatted like /^PR-\d+$/, we'll use the value of CHANGE_BRANCH instead of GIT_BRANCH or the branch name reported by the git command.

markcellus avatar Oct 29 '20 12:10 markcellus

I haven't seen those PR-[NUM] branch names before. Is that normal behavior in Jenkins? Specifically for Jenkins, we recommend using the GitHub PR plugin. From our docs:

If you’re using Jenkins’ GitHub PR plugin, choose the ghprbPullId specifier for the refspec, and ensure you’ve set the Branch Specifier to ${ghprbActualCommit}.

We've just released [email protected] which tries to detect the branch name in a more intelligent way. It may work for Jenkins as well, but I suspect it will not if Jenkins checks out the code under a custom branch name (so git will report the custom name) rather than just setting an environment variable for it.

Our CLI supports overriding the git branch name and commit hash by setting the CHROMATIC_BRANCH and CHROMATIC_SHA environment variables. Both of these must be set in order for this to work. Admittedly it would be more flexible if we'd simply respect them individually. I'd be happy to accept a PR for that.

Right now, I think the easiest fix is to make sure CHROMATIC_SHA is set as well. Its value should be the last commit hash on the current branch. I suspect you may be able to use GIT_COMMIT for this, which appears to be an environment variable exposed by Jenkins. There's also GIT_BRANCH and GIT_LOCAL_BRANCH. I suspect the latter is the best bet there, if you haven't yet found a way to determine the correct branch name.

ghengeveld avatar Oct 29 '20 14:10 ghengeveld

Hmmm... upgrading didn't fix issue but specifying CHROMATIC_SHA environment variable along with CHROMATIC_BRANCH did.

It appears that chromatic cli reads from GIT_BRANCH but checkout plugin used in our Jenkins setup assigns that to PR-[NUM] (see this StackOverflow question). So although GIT_BRANCH is set to the wrong value in our Jenkins setup, it still allows me to read the correct branch name from CHANGE_BRANCH.

So I manually set CHROMATIC_BRANCH to CHANGE_BRANCH. Here's what works in the script inside our Jenkins pipeline stage:

// Jenkins sets PR numbers as branch names but Chromatic
// needs the actual branch name in order to automatically
// run the UI Review feature
if (env.BRANCH_NAME.startsWith('PR')) {
    env.CHROMATIC_BRANCH = env.CHANGE_BRANCH;
    // we must set CHROMATIC_SHA manually in order
    // for it to pick up CHROMATIC_BRANCH variable above
    env.CHROMATIC_SHA = env.GIT_COMMIT;
}
sh 'npm run chromatic -- --ci'

markcellus avatar Oct 29 '20 14:10 markcellus