Skip to content

Instantly share code, notes, and snippets.

@melo
Created December 6, 2016 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melo/cf01fd687f096163e5e781b3bb75b1f8 to your computer and use it in GitHub Desktop.
Save melo/cf01fd687f096163e5e781b3bb75b1f8 to your computer and use it in GitHub Desktop.
Trigger a Gitlab CI build with 'git build'
#!/bin/sh
project_id=`git config gitlab.project-id`
if [ -z "$project_id" ] ; then
echo "FATAL: could not find gitlab.project-id on your config"
echo "To fix: use 'git config gitlab.project-id ID'"
echo " find the project ID in the Settings > Triggers page,"
echo " in the examples, it is the number in the URL"
exit 1
fi
token=`git config gitlab.trigger-token`
if [ -z "$token" ] ; then
echo "FATAL: could not find gitlab.trigger-token on your config"
echo "To fix: use 'git config gitlab.trigger-token TOKEN'"
echo " you can create a TOKEN on your project page, Settings > Triggers"
exit 1
fi
branch="${1:-`git name-rev --name-only HEAD`}"
if [ -z "$branch" ] ; then
echo "FATAL: could not determine branch to build"
echo "To fix: use 'git build BRANCH', or try to understand why"
echo " 'git name-rev --name-only HEAD' returns nothing..."
exit 1
fi
echo "Triggering build for branch '$branch'"
curl -X POST \
-F "token=$token" \
-F "ref=$branch" \
-F "variables[BUILD_VIA]=git-build" \
"https://gitlab.com/api/v3/projects/$project_id/trigger/builds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment