Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading to Artifactory from a Windows Powershell script

I've successfully downloaded a file from Artifactory (Generic Repo) via a WebClient object. I'm having troubles uploading a file via the same method. I'm trying to figure out the simplest method for uploading via Powershell to our server.

Please note that installing other utilities like Curl is not an option at this point. I'm writing automation scripts and want to stick with a basic Windows 2008 r2 server, no installing other utilities since I can't count on them being there across all the servers.

If someone has an example script utilizing the Rest API, that would be perfect!

Example of the download code (this works):

$SOURCE = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_1.0.zip"  
$DESTINATION = ".\BF_1.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER,$AF_PWD)  
$WebClient.DownloadFile($SOURCE,$DESTINATION)  

This is an example of the upload code (does not work):

$SOURCE = ".\BF_2.0.zip"  
$DESTINATION = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER, $AF_PWD)  
$URI = New-Object System.Uri($DESTINATION)  
$WebClient.UploadFile($URI,$SOURCE)  

This is the error I'm getting from the upload:

Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (405) Method Not Allowed."  
At E:\transient\af_put.ps1:8 char:1  
+ $WebClient.UploadFile($URI,$SOURCE)  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException  
    + FullyQualifiedErrorId : WebException  
like image 569
Macnlos Avatar asked Mar 28 '16 15:03

Macnlos


People also ask

How do I deploy files to Artifactory?

To deploy your package according to the configured layout, check Deploy According to Layout. Artifactory displays entry fields corresponding to the layout tokens for you to fill in. To deploy multiple files together, simple set the deploy Type to Multi, fill in the rest of the fields in the dialog and click "Deploy".

How do you upload artifacts to Artifactory?

You log in to Artifactory as an administrator/user which has access to deploy artifacts, click on "Deploy" tab, browse for the Artifactory file and once you select the file, click on "Upload" button.

How do I upload large files to Artifactory?

By default, Artifactory limits UI-generated file deployments to 100MB. You are free to adjust this limit at Administration > Artifactory > General (in version 7. x); at Admin > General (in version 6.


1 Answers

My answer is certainly derivative, but this is what worked for me when pushing something to my Artifactory repo from powershell using an API token for authentication:

$credential_bytes = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $api_token)
$credentials = [System.Convert]::ToBase64String($credential_bytes)
$credential_header = "Basic " + $credentials    
Invoke-WebRequest -Uri $artifactory_dest_url -InFile "my_file.zip" -Method Put -Headers @{"Authorization"="$credential_header"}

Sigh. I regret that I'm using Powershell. What am I doing with my life? Anyway, it works. Moving on.

Props to FoxDeploy and Maclnos.

like image 152
James T Snell Avatar answered Nov 05 '22 13:11

James T Snell