使用 PowerShell 在 GitLab 上创建版本

问题描述 投票:0回答:2

我尝试创建一个 powershell 脚本,它可以帮助我们在我们的私人 gitlab 服务器上创建一个版本。

我找到了这个教程 如何在 GitLab 中创建版本?

我无法弄清楚如何用 powershell invoke-webrequest 命令替换curl 命令。

我已经为此尝试了多种概念。在我们的例子中,我们有一些代码文件并在同一存储库中创建包。该包包含在 gitignore 文件中。 (这对我们来说很好)

现在我尝试这样做

$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

结果:

Invoke-RestMethod:{“错误”:“文件无效”}

有没有人有一个脚本可以使用 powershell 创建带有下载和更改日志的 gitlab 发布页面。

我也尝试过发布它,但我无法为 gitlab 配置它。 https://www.npmjs.com/package/release-it#gitlab-releases

powershell gitlab gitlab-api
2个回答
2
投票

下面是我为此目的编写或使用的一些 PowerShell 片段的集合。也许这可以帮助一些人。您是否也不能在脚本中创建

$Body
- 看起来无法从文件创建内容?

# 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"

0
投票

您应该使用:

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

请参阅 https://docs.gitlab.com/ee/api/projects.html#upload-a-file(或如何在 GitLab 中创建版本?

© www.soinside.com 2019 - 2024. All rights reserved.