Search code examples
powershellteamcity-9.0

Accessing Teamcity Artifact with Dynamic name


I want to download a TeamCity artifact via powershell. It needs to be the last successful build of a specific branch.

I've noticed two common url paths to access the artifacts. One seems to be /repository/download/BUILD_TYPE_EXT_ID/.lastSuccessful/ARTIFACT_PATH

The problem is that the file at the end relies on the release version. Within TeamCity there is syntax to specify all files \*.msi. Is there any way to specify an artifact starting with FileName-{version.number}.msi when trying to access this url?


EDIT:

The other url I noticed is for the REST API.

http://teamcity/guestAuth/app/rest/builds/branch:[BRANCH],buildType:[BUILD TYPE],status:SUCCESS,state:finished/artifacts/[BUILD PATH] 

The problem is that I can't download artifacts from here. If I want to download the artifacts I have to use the current build id. The above url gives the following url: /guestAuth/app/rest/builds/id:[Build ID]/artifacts/content/[Artifact Path] to download the artifact.

I can use the first REST url to eventually get the second through the returned xml, but would prefer a more straightforward approach.


Solution

  • Unfortunately as TeamCity artifacts are not browsable the usual workarounds like wget recursive download or wildcards are not applicable.

    Using wildcards in wget or curl query How do I use Wget to download all Images into a single Folder

    One workaround you could try is formatting the link in the job, saving the link to a text file and storing that as an artifact as well, with a static name. Then you just need to download that text file to get the link.

    I found you can format the artifact URL in TeamCity job by doing:

    %teamcity.serverUrl%/repository/download/%system.teamcity.buildType.id%/%teamcity.build.id%:id/<path_to_artifact>

    In a command line step. You can write this to a file by doing:

    echo %teamcity.serverUrl%/repository/download/%system.teamcity.buildType.id%/%teamcity.build.id%:id/myMsi-1.2.3.4.msi > msiLink.txt"

    Now you have an artifact with a constant name, that points to the installer (or other artifact) with the changing name.

    If you use the artifact msiLink.txt you don't need to use the REST interface (it's still two calls, both through the same interface).

    You can easy download the latest version from batch/cmd by using:

    wget <url_server>/repository/download/BUILD_TYPE_EXT_ID/.lastSuccessful/msiLink.txt ---user #### --passsword #### set /P msi_url=<msiLink.txt wget %msi_url% --user #### --passsword ####

    Hope it helps.

    Update: Sorry I just realized the question asked for PowerShell:

    $WebClient = New-Object System.Net.WebClient $WebClient.Credentials = New-Object System.Net.Networkcredential("yourUser", "yourPassword") $WebClient.DownloadFile( "<url_server>/repository/download/BUILD_TYPE_EXT_ID/.lastSuccessful/msiLink.txt", "msiLink.txt" ) $msi_link = [IO.File]::ReadAllText(".\msiLink.txt") $WebClient.DownloadFile( $msi_link, "yourPath.msi" )