Skip to content

Instantly share code, notes, and snippets.

@chtz
Created March 11, 2018 21:21
Show Gist options
  • Save chtz/6b741f0cf2b46e1718d1ec847a909bab to your computer and use it in GitHub Desktop.
Save chtz/6b741f0cf2b46e1718d1ec847a909bab to your computer and use it in GitHub Desktop.
Artifactory (4.16.0 / default admin) deployment sample in ruby (no error handling)
require 'nokogiri'
require 'net/http'
ARTIFACTORY_URL = "http://localhost:8081/artifactory/libs-release-local"
ARTIFACTORY_USER = "admin"
ARTIFACTORY_PASS = "password"
TARGET_FOLDER = "target"
def open_pom
File.open("pom.xml") { |f| Nokogiri::XML(f) }
end
def get_version(pom)
pom.xpath("/x:project/x:version", 'x' => 'http://maven.apache.org/POM/4.0.0').first.content
end
def get_group(pom)
pom.xpath("/x:project/x:groupId", 'x' => 'http://maven.apache.org/POM/4.0.0').first.content
end
def get_artifact(pom)
pom.xpath("/x:project/x:artifactId", 'x' => 'http://maven.apache.org/POM/4.0.0').first.content
end
def artifactory_deploy(group, artifact, version, type, target_file, content_type)
group = group.gsub(/\./, '/')
uri = URI("#{ARTIFACTORY_URL}/#{group}/#{artifact}/#{version}/#{artifact}-#{version}.#{type}")
http = Net::HTTP.new(uri.host, uri.port)
File.open(target_file, "r") do |file|
req = Net::HTTP::Put.new(uri.path)
req.content_length = File.size(target_file)
req.basic_auth ARTIFACTORY_USER, ARTIFACTORY_PASS
req.body_stream = file
req.content_type = content_type
res = http.request(req)
puts res.body
end
end
def deploy_jar(group, artifact, version)
type = "jar"
target_file = "#{TARGET_FOLDER}/#{artifact}-#{version}.#{type}"
content_type = "application/java-archive"
artifactory_deploy group, artifact, version, type, target_file, content_type
end
def deploy_pom(group, artifact, version)
type = "pom"
target_file = "pom.xml"
content_type = "application/x-maven-pom+xml"
artifactory_deploy group, artifact, version, type, target_file, content_type
end
pom = open_pom
version = get_version(pom)
group = get_group(pom)
artifact = get_artifact(pom)
deploy_jar group, artifact, version
deploy_pom group, artifact, version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment