bLuB
bLuB

Reputation: 11

Create Release on GitLab with PowerShell

I try to create a powershell script which can help us to create a release on our private gitlab server.

I found this tutorial How to create releases in GitLab?

I'm not able to figure out how I can replace the curl command with the powershell invoke-webrequest command.

I already tried multiple concepts for this. In our case we have some code files and create the package in the same repository. The package is included in the gitignore file. (That's fine for us)

Now I tried to do this

$body = @{'form'='file=@/our awesome app.app'}
Invoke-RestMethod -Headers @{ 'PRIVATE-TOKEN'='<mytoeken>' } -Uri https://scm.domain.net/api/v4/projects/19/uploads -Body $Body

Result:

Invoke-RestMethod : {"error":"file is invalid"}

Does anyone have a script for creating a gitlab release page with download and changelog with powershell.

I also tried release it but I'm not able to configure it for gitlab. https://www.npmjs.com/package/release-it#gitlab-releases

Upvotes: 1

Views: 3270

Answers (2)

talbot_sk
talbot_sk

Reputation: 23

You should use:

$form = "file=@/our awesome app.app"

See https://docs.gitlab.com/ee/api/projects.html#upload-a-file (or How to create releases in GitLab?) I had a problem with uploading file via PowerShell/Invoke-WebRequest. So I used curl instead:

$curlExecutable = "C:\Windows\System32\curl.exe"
$curlArguments = 
    "--request", "POST",
    "--header", "PRIVATE-TOKEN: $gitlab_personal_access_token", 
    "--form", "file=@$localFilePath", 
    "$gitlab_host/api/v4/projects/$gitlab_project_id/uploads"
& $curlExecutable $curlArguments

Upvotes: 0

Hottemax
Hottemax

Reputation: 354

Below is a collection of some PowerShell snippets I have written or used for this purpose. Maybe this can help some people. Can you not create the $Body in the script also - it looks like it fails create the content from the file?

# Provides code snippets for Windows Powershell to use Gitlabs API to do various tasks
# for example [ Issue management, Release creation etc.]

# https://docs.gitlab.com/ee/api/

# Variables
# modify with you own content
$glUrl = "http://<your-gitlab-url>"
$p =  "3"                                   # Project ID
$tag = "1.3"                                # Modify this to create/delete release ...
$token = "<EDIT_ME>"                        # Your access token (http://<your-gitlab-url>/profile/personal_access_tokens)
$projectsUrl = "$glUrl/api/v4/projects"     # i.e. http://<your-gitlab-url>/api/v4/projects
$pUrl = "$projectsUrl/$p"                   # i.e. http://<your-gitlab-url>/api/v4/projects/3
$releaseUrl = "$pUrl/releases"              # i.e. http://<your-gitlab-url>/api/v4/projects/3/releases
$artifactsUrl = "$pUrl/jobs/artifacts/$tag/download?job=build-standard" # i.e. Build artifacts created for $tag and the relevant job ("build-standard"; can be modified)

# Project List
$r = Invoke-RestMethod -Headers @{ 'PRIVATE-TOKEN'="$token" } -Uri $projectsUrl
$r | Sort-Object -Property id | Format-Table -Property id, name

# Issues List
$r = Invoke-RestMethod -Headers @{ 'PRIVATE-TOKEN'="$token" } -Uri $pUrl/issues
$r | Sort-Object -Property id | Format-Table -Property id, state, title

# New Issue
Invoke-RestMethod -Method Post -Headers @{ 'PRIVATE-TOKEN'="$token" } -Uri "$pUrl/issues?title=Hello from PS&labels=test"

# Comment on the Issue
Invoke-RestMethod -Method Post -Headers @{ 'PRIVATE-TOKEN'="$token" } -Uri "$pUrl/issues/3/notes?body=Hello PowerShell"

function Register-NewIssue {
    param(
        [string]$title,
        [string]$desc = '',
        [string]$uri = '$projectUrl/issues'
    )

    $title = [System.Web.HttpUtility]::UrlEncode($title)
    $desc = [System.Web.HttpUtility]::UrlEncode($desc)
    $u = "$uri`?title=$title&description=$desc"
    $r = Invoke-RestMethod -Method Post -Headers @{ 'PRIVATE-TOKEN'= "$token" } -Uri $u
    $r | Format-List -Property iid, state, title, description
}
# Get list of  Releases
Invoke-RestMethod -Method Get -Headers @{ 'PRIVATE-TOKEN'="$token" } -Uri $releaseUrl

# Create a Release
$JSON = @"
{
  "name": "New release",
  "tag_name": $tag,
  "ref": "master",
  "description": "FromPS",
  "assets":{
      "links":[
         {
            "name":"Executables",
            "url":"$artifactsUrl"
         }
      ]
   }
}
"@

Invoke-RestMethod -Method Post -Headers @{ 'PRIVATE-TOKEN'="$token"; 'Content-Type'='application/json' } -Body $JSON -Uri "$releaseUrl"
Read-Host -Prompt "Press Enter to continue"

# Adds only a link to this Release manually
$JSONLINK = @'
{"name":"awesome-exec",
 "url":"$artifactsUrl"
}
'@
Invoke-RestMethod -Method Post -Headers @{ 'PRIVATE-TOKEN'= "$token"; 'Content-Type'='application/json' } -Body $JSONLINK -Uri "$releaseUrl/$tag/assets/links"

# Delete a Release
Invoke-RestMethod -Method Delete -Headers @{ 'PRIVATE-TOKEN'= "$token" } -Uri "$releaseUrl/$tag"

Upvotes: 2

Related Questions