Skip to content

Commit

Permalink
Add ability to get more than one artifact from a job.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Harvey-Smith committed Nov 27, 2017
1 parent 2785f56 commit 6ea42ea
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/jenkins_api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,41 @@ def get_artifact(job_name,filename)
end
end

# Connects to the server and download all artifacts of a build to a specified location
#
# @param [String] job_name
# @param [String] dldir location to save artifacts
# @param [Integer] build_number optional, defaults to current build
# @returns [String, Array] list of retrieved artifacts
#
def get_artifacts(job_name, dldir, build_number = nil)
@artifacts = job.find_artifacts(job_name,build_number)
results = []
@artifacts.each do |artifact|
uri = URI.parse(artifact)
http = Net::HTTP.new(uri.host, uri.port)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = @ssl
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth(@username, @password)
response = http.request(request)
# we want every thing after the last 'build' in the path to become the filename
if artifact.include?('/build/')
filename = artifact.split("/build/").last.gsub('/','-')
else
filename = File.basename(artifact)
end
filename = File.join(dldir, filename)
results << filename
if response.code == "200"
File.write(File.expand_path(filename), response.body)
else
raise "Couldn't get the artifact #{artifact} for job #{job}"
end
end
results
end

# Connects to the Jenkins server, sends the specified request and returns
# the response.
#
Expand Down
24 changes: 24 additions & 0 deletions lib/jenkins_api_client/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,30 @@ def artifact_exists?(job_name, build_number = 0)
end
end

# Find the artifacts for build_number of job_name, defaulting to current job
#
# @param [String] job_name
# @param [Integer] build_number Optional build number
# @return [String, Hash] JSON response from Jenkins
#
def find_artifacts(job_name, build_number = nil)
current_build_number = build_number || get_current_build_number(job_name)
job_path = "job/#{path_encode job_name}/"
response_json = @client.api_get_request("/#{job_path}#{current_build_number}")
response_json['artifacts'].map do |art|
URI.escape("#{response_json['url']}artifact/#{art['relativePath']}")
end
end

# Find the artifacts for the current job
#
# @param [String] job_name
# @return [String, Hash] JSON response from Jenkins
#
def find_latest_artifacts(job_name)
find_artifacts(job_name)
end

private

# Obtains the threshold params used by jenkins in the XML file
Expand Down

0 comments on commit 6ea42ea

Please sign in to comment.