Question
There is a requirement to fetch from or push to the Git repository used by the build in a build step, while the build is running. However:
- TeamCity doesn't store credentials on the build agent
- TeamCity doesn't fetch the branch other than the one checked out by the build.
Therefore, plain git fetch, git checkout, or git push commands fail.
Answer
There are multiple ways of implementing this.
HTTPS
- Create a build configuration parameter named
env.GH_PAT, set the type toPassword, and set the value to the VCS hosting's Personal Access Token.
- If the required branch isn't already fetched and checked out, the following commands in the command line build step can be used:
BRANCH="feature/new-feature"
git -c credential.helper= -c credential.helper='!f() { echo "password=$GH_PAT"; }; f' fetch origin $BRANCH && \
git checkout $BRANCH
- If changes are committed and need to be pushed, the following command can be used:
BRANCH="feature/new-feature"
git -c credential.helper= -c credential.helper='!f() { echo "password=$GH_PAT"; }; f' push origin HEAD:$BRANCH
SSH
-
Add the SSH Agent build feature.
-
If fetch/checkout is required:
BRANCH="feature/new-feature"
export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
git fetch origin $BRANCH && \
git checkout $BRANCH
- If push is required:
BRANCH="feature/new-feature"
export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
git push origin HEAD:$BRANCH
Comments
0 comments
Please sign in to leave a comment.