2
votes

I have a Build job in Jenkins that checks out a specific Git commit and packages it for deployment as artifacts.

There is a later Deployment job that takes the built artifacts and actually deploys the code. It also does a sparse Git checkout of a specific directory containing deployment scripts. After successful completion, we write a Git tag.

The problem is that the tag is being written to the HEAD of master, not to the hash of the commit used for the original upstream build. (master is the branch defined in the job configuration.)

Is there a way to get the upstream SCM information if it's not passed directly through a parameterized trigger? I can see commits listed in the build.xml file that Jenkins generates in the build directory; is there a way to read this information from the downstream job?

I realize that it's not really "downstream", since it's manually triggered. We do have a selector that defines UPSTREAM_BUILD and UPSTREAM_PROJECT, though.

1

1 Answers

2
votes

If you are using the Copy Artifact plugin, you could write a file with the commit hash during the Build job and read it back in during the Deployment job:

# Build
echo ${GIT_COMMIT} > COMMIT_HASH

# Deployment, after copying COMMIT_HASH into the workspace
git checkout $(cat COMMIT_HASH)