【问题标题】:Create Release on GitLab with PowerShell使用 PowerShell 在 GitLab 上创建发布
【发布时间】:2019-05-29 16:54:23
【问题描述】:

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

我找到了这个教程 How to create releases in 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 : {"error":"file is invalid"}

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

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

【问题讨论】:

    标签: powershell gitlab gitlab-api


    【解决方案1】:

    以下是我为此目的编写或使用的一些 PowerShell sn-ps 的集合。也许这可以帮助一些人。您是否也不能在脚本中创建 $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"
    

    【讨论】:

      猜你喜欢
      • 2015-06-13
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2019-09-27
      • 2015-07-31
      • 2011-09-13
      • 2018-11-29
      • 1970-01-01
      相关资源
      最近更新 更多