Publish to Artifactory with PowerShell

Great Monday everyone!

How is your weekend? I have a good one with a hiking session on Saturday to Point Reyes and The Fate of Furious session on Sunday! Will write a review on those stuffs in another thread :)

Now, come to what we will have today, should be a simple one to start the week. I have a need to publish artifacts into artifactory, simple, right? Yes, however, I want to have a bit manipulation on the publishing. Not sure if it's worth some effort to do this research but I love to have some more knowledge on this area. 

Use Case:

I have some artifacts (4) to publish into multiple artifactory repositories. In general, you can use the artifactory plugin to define the upload specification file then trigger this plugin by gradle or from Jenkins. However, my artifacts has a pattern to deploy so instead of creating a session for each artifact I will have a loop to go through those artifacts with a pattern

Solution:

I will use scripting to publish into artifactory in a loop and build the target URL, source location base on the pattern. Because I'm familiar with Powershell so I use this scripting. Again, we can do a lot of stuffs on artifactory with its REST API, so I will use curl as a simple solution

Sample Code:
$curlCmd = '-u ' + $user + ':' + $password  + ' -X PUT '
$targetURL = "http://[artifactory]/[groupID]/[artifactID]/[version]/filename
$fileLocation = "[localPath]
$batCmd = $curlCmd + "`"$targetURL`" -T `"$fileLocation`""
Start-Process -FilePath "curl.exe" -ArgumentList $batCmd -Verb runAs -Wait

After publishing, the others need to use my artifacts and they need to have a pom file for dependency resolving so I have to create a pom file for each artifact. In general, this pom file is an description xml for the maven location. Here is the code to create this pom file

$pomLocation = "[localPath]"
$encoding = [System.Text.Encoding]::UTF8
$xmlWriter = New-Object System.XMl.XmlTextWriter($pomLocation,$encoding)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 4
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement('project')
$xmlWriter.WriteAttributeString("xmlns", "http://maven.apache.org/POM/4.0.0")
$xmlWriter.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd")
     
$xmlWriter.WriteStartElement("modelVersion");
$xmlWriter.WriteString("4.0.0");
$xmlWriter.WriteEndElement();

$xmlWriter.WriteStartElement("groupId");
$xmlWriter.WriteString("[group]");
$xmlWriter.WriteEndElement();

$xmlWriter.WriteStartElement("artifactId");
$xmlWriter.WriteString("[artifactID]");
$xmlWriter.WriteEndElement();

$xmlWriter.WriteStartElement("version");
$xmlWriter.WriteString("[version]");
$xmlWriter.WriteEndElement();

$xmlWriter.WriteStartElement("description");
$xmlWriter.WriteString("Description here");
$xmlWriter.WriteEndElement();

$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
Write-Host (Get-Content -Path "$pomLocation" -Raw)

If you publish a file, then it needs to fix the checksum because when you publish it, you don't put the file checksum together. This is how you do it with Powershell

function publishArtifactory
{
  param($targetURL,$fileLocation)
  $targetURI = New-Object System.Uri($targetURL)
  $md5Hash = Get-FileHash "$fileLocation" -Algorithm MD5
  $sha1Hash = Get-FileHash "$fileLocation" -Algorithm SHA1
  $webCredential = New-Object System.Management.Automation.PSCredential($user, (ConvertTo-SecureString -String $password -AsPlainText -Force))
  $webHearders = @{"X-Checksum-Deploy"="true"}
  $webHearders.Add("X-Checksum-Sha1", $sha1Hash.Hash.ToLower())
  $webHearders.Add("X-Checksum-Md5", $md5Hash.Hash.ToLower())
  Invoke-WebRequest $targetURI -Headers $webHearders -Credential $webCredential -InFile "$fileLocation" -Method PUT -ContentType "multipart/form-data"
}

However, there should be some tweaks here as I put this function into a loop, it causes some errors from second call. But if you do some simple task, this should be a handy tweak here.

PS: I have read this thread but not sure it can fix the problem above.

Comments

Post a Comment