Skip to content

Instantly share code, notes, and snippets.

@shinenelson
Created May 27, 2016 23:55
Show Gist options
  • Select an option

  • Save shinenelson/2058b74bce872c67b87a5420045406f5 to your computer and use it in GitHub Desktop.

Select an option

Save shinenelson/2058b74bce872c67b87a5420045406f5 to your computer and use it in GitHub Desktop.

Revisions

  1. shinenelson created this gist May 27, 2016.
    34 changes: 34 additions & 0 deletions jenkins-remote-build-trigger.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/bin/bash

    # This script triggers Jenkins builds remotely
    # provided you have the necessary configurations set on
    # your Jenkins node

    # This script takes in 3 variables (Jenkins URL, User's name and API Key),
    # 2 arguments (job-name, authentication token)
    # and assumes that you don't have to build with parameters

    # Pre-Requisites
    # Jenkins v2.0+
    # User API Key from $JENKINS_URL/securityRealm/user/$JENKINS_USER/configure
    # Enable "Trigger Builds Remotely" option under "Build Triggers"
    # Assign an authentication token for the project

    # Variables
    JENKINS_URL='<put your Jenkins node URL here without the URL scheme like ci.jenkins.org>'
    JENKINS_USER='<put your name here>'
    JENKINS_USER_API_KEY='<put your user's API Key here>'
    # If you're worried about the security of your personal API key,
    # you could set it as an environment variable and pass it to the script

    job=$1
    token=$2

    # Verification Integrity of Input Arguments
    if [[ $# -ne 2 ]]; then
    echo "Uh, Uh! That's not the right number of arguments to trigger a remote Jenkins Build"
    echo "I'm taking a break for now. Bye."
    exit 1
    fi

    curl -X POST "https://$JENKINS_USER:$JENKINS_USER_API_KEY@$JENKINS_URL/job/$job/build?token=$token"