#!/usr/bin/env ruby

STDOUT.sync = true
STDERR.sync = true

@hostname = ENV['JENKINS_URL'].split("/")[-1].downcase
@job_url = "/job/#{ENV['OPENSHIFT_APP_NAME']}-build"
@action = ARGV[0]

def job_action
  status_code = `curl -s -w %{http_code} --output /dev/null -X POST --insecure https://${JENKINS_USERNAME}:${JENKINS_PASSWORD}@#{@hostname}#{@job_url}/#{@action}`

  if status_code != '302' && status_code != '200' # returns a 302 but checking 200 just to be safe
    exit 1
  end
end

job_action
