Skip to content

Instantly share code, notes, and snippets.

@danmayer
Last active August 29, 2015 13:56
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 danmayer/8816670 to your computer and use it in GitHub Desktop.
Save danmayer/8816670 to your computer and use it in GitHub Desktop.
client for remote configuring a jenkins
#!/usr/bin/env ruby
require 'jenkins_api_client'
require 'ruby-debug'
CI_PROJECT_NAME = "server_responder"
@client = JenkinsApi::Client.new(:server_ip => 'localhost',
:server_port => '8888',
:username => 'somename',
:password => 'secret password')
def build_deals_ci_job
@client.job.create_freestyle(
:name => CI_PROJECT_NAME,
:keep_dependencies => true,
:concurrent_build => false,
:scm_provider => "git",
:scm_url => "https://github.com/danmayer/server_responder.git",
:scm_branch => "master",
:shell_command => <<-eos
service=$JOB_NAME
service_port=8999
branch=$(echo $GIT_BRANCH | cut -d/ -f 2)
docker build -t $service:$branch $WORKSPACE
# how to stop the currently running docker
[ -z "$(docker ps | grep .*$service_port | awk '{print $1;}')"] || docker stop $(docker ps | grep .*$service_port | awk '{print $1;}')
container_id=$(docker run -d -p $service_port:$service_port $service:$branch)
#container_port=$(docker inspect $container_id | awk 'BEGIN { FS = "\"" } ; /"'$service_port'":/ { print $4 }')
echo "App running on http://localhost:$service_port"
eos
)
end
### find all jobs
jobs = @client.job.list(".")
puts "found jobs: #{jobs.inspect}"
unless jobs.include?(CI_PROJECT_NAME)
build_deals_ci_job
end
deals_ci_job = @client.job.list(CI_PROJECT_NAME).first
code = @client.job.build(deals_ci_job)
raise "Could not build the job specified" unless code != 201
# unfortunately exec cli fails for me
# puts @client.exec_cli("version")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment