【问题标题】:How to upload an asset from Azure Release pipeline to Github Enterprise using Bash如何使用 Bash 将资产从 Azure 发布管道上传到 Github Enterprise
【发布时间】:2020-03-13 08:26:39
【问题描述】:

由于我工作的公司不允许来自 Azure 市场的任何非 Microsoft 发布的软件包,我需要创建一个 bash 脚本,以便能够将我构建/发布的资产上传到 Github Enterprise 存储库。出于某种原因,当我尝试获取发布标签的信息时,我总是会被重定向,无论是特定标签还是最新标签,都没关系。

以下是我的脚本:

set -e
xargs=$(which gxargs || which xargs)

# Validate settings.
[ "$TRACE" ] && set -x

CONFIG=$@

for line in $CONFIG; do
  eval "$line"
done

# Define variables.
GH_API="https://git.[company].com/api/v3"
GH_REPO="$GH_API/repos/[owner]/$(Build.Repository.Name)"
GH_TAGS="$GH_REPO/releases/tags/$(Build.SourceBranchName)"
AUTH="Authorization: token $github_api_token"
WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
CURL_ARGS="-LJO#"
tag="$(Build.SourceBranchName)"
filename="BaseRepoName_$(Build.SourceBranchName)_$(Build.BuildId).zip"
echo "tag is: $tag"
echo $AUTH
echo "Repo: $GH_REPO"

if [[ "$tag" == 'LATEST' ]]; then
  GH_TAGS="$GH_REPO/releases/latest"
fi
echo "Tags url:  $GH_TAGS"

echo "Validate token ..."
# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!";          exit 1; }

echo "Get api endpoints"
apiresponse=$(curl -sH "$AUTH" "$GH_API")
echo "API:  $apiresponse"

echo "Read asset tags: curl -sH "$AUTH" $GH_TAGS"
# Read asset tags.
response=$(curl -sH "$AUTH" $GH_TAGS)
echo "Response: $response"

## In case of success, we upload a file
upload=$(echo $succ | grep upload_url)
if [[ $? -eq 0 ]]; then
    echo Release created.
else
    echo Error creating release!
    return
fi

echo "Get the upload url for the given tag"
upload=$(echo $upload | cut -d "\"" -f4 | cut -d "{" -f1)
upload="$upload?name=$filename"

# Upload asset
echo "Uploading asset... "
succ=$(curl -H "Authorization: token $perstok" \
     -H "Content-Type: $(file -b --mime-type $filename)" \
     --data-binary @$filename $upload)

Bash 任务的日志:

2020-03-13T05:53:35.4274045Z ##[section]Starting: Bash Script
2020-03-13T05:53:35.4654068Z ==============================================================================
2020-03-13T05:53:35.4654773Z Task         : Bash
2020-03-13T05:53:35.4655254Z Description  : Run a Bash script on macOS, Linux, or Windows
2020-03-13T05:53:35.4655535Z Version      : 3.163.1
2020-03-13T05:53:35.4656320Z Author       : Microsoft Corporation
2020-03-13T05:53:35.4656864Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash
2020-03-13T05:53:35.4657312Z ==============================================================================
2020-03-13T05:53:36.4699984Z Generating script.
2020-03-13T05:53:36.4703019Z [command]"C:\Program Files\Git\bin\bash.exe" --noprofile --norc -c pwd
2020-03-13T05:53:36.8704279Z /d/a/_temp
2020-03-13T05:53:36.8755504Z 
2020-03-13T05:53:36.8794400Z ========================== Starting Command Output ===========================
2020-03-13T05:53:36.8801355Z [command]"C:\Program Files\Git\bin\bash.exe" --noprofile --norc /d/a/_temp/c7a40af4-c1f2-4127-a2cf-4aa11ac19e48.sh
2020-03-13T05:53:36.9965374Z which: no gxargs in (/mingw64/bin:/usr/bin:/c/Users/VssAdministrator/bin:/c/hostedtoolcache/windows/Python/3.6.8/x64:/c/hostedtoolcache/windows/Python/3.6.8/x64/Scripts:/c/Program Files/Mercurial:/c/ProgramData/kind:/c/vcpkg:/c/cf-cli:/c/Program Files (x86)/NSIS:/c/Program Files/Mercurial:/c/hostedtoolcache/windows/Boost/1.69.0:/c/Program Files/dotnet:/c/mysql-5.7.21-winx64/bin:/c/Program Files/Java/zulu-8-azure-jdk_8.40.0.25-8.0.222-win_x64/bin:/c/SeleniumWebDrivers/GeckoDriver:/c/Program Files (x86)/sbt/bin:/c/Rust/.cargo/bin:/c/hostedtoolcache/windows/Ruby/2.5.7/x64/bin:/c/Go1.12.7/bin:/bin:/c/hostedtoolcache/windows/Python/3.6.8/x64/Scripts:/c/hostedtoolcache/windows/Python/3.6.8/x64:/c/npm/prefix:/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/wbin:/c/Program Files/Microsoft MPI/Bin:/c/windows/system32:/c/windows:/c/windows/System32/Wbem:/c/windows/System32/WindowsPowerShell/v1.0:/c/ProgramData/Chocolatey/bin:/c/Program Files/Docker:/c/Program Files/PowerShell/7:/c/Program Files/dotnet:/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/c/Program Files (x86)/Microsoft SQL Server/110/DTS/Binn:/c/Program Files (x86)/Microsoft SQL Server/120/DTS/Binn:/c/Program Files (x86)/Microsoft SQL Server/130/DTS/Binn:/c/Program Files (x86)/Microsoft SQL Server/140/DTS/Binn:/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/c/Program Files (x86)/Windows Kits/10/Windows Performance Toolkit:/c/Program Files/Microsoft Service Fabric/bin/Fabric/Fabric.Code:/c/Program Files/Microsoft SDKs/Service Fabric/Tools/ServiceFabricLocalClusterManager:/c/Program Files/nodejs:/c/Strawberry/c/bin:/c/Strawberry/perl/site/bin:/c/Strawberry/perl/bin:/cmd:/mingw64/bin:/usr/bin:/c/tools/php:/c/Program Files (x86)/sbt/bin:/c/Program Files (x86)/Subversion/bin:/c/SeleniumWebDrivers/ChromeDriver:/c/SeleniumWebDrivers/EdgeDriver:/c/ProgramData/chocolatey/lib/maven/apache-maven-3.6.3/bin:/c/Program Files/CMake/bin:/c/Program Files/OpenSSL/bin:/c/Users/VssAdministrator/.dotnet/tools:/c/Program Files (x86)/Microsoft SQ)
2020-03-13T05:53:37.0042653Z tag is: MBP_TestTag6
2020-03-13T05:53:37.0043260Z Authorization: token [Edited: secret]
2020-03-13T05:53:37.0043852Z Repo: https://git.[company].com/api/v3/repos/[owner]/[MyTestRepo]
2020-03-13T05:53:37.0044723Z Tags url:  https://git.[company].com/api/v3/repos/[owner]/[MyTestRepo]/releases/tags/MBP_TestTag6
2020-03-13T05:53:37.0045218Z Validate token ...
2020-03-13T05:53:38.5703717Z Get api endpoints
2020-03-13T05:53:38.8038551Z API:  {
2020-03-13T05:53:38.8050142Z   "current_user_url": "https://git.[company].com/api/v3/user",
2020-03-13T05:53:38.8053206Z   "current_user_authorizations_html_url": "https://git.[company].com/settings/connections/applications{/client_id}",
2020-03-13T05:53:38.8060689Z   "authorizations_url": "https://git.[company].com/api/v3/authorizations",
2020-03-13T05:53:38.8064874Z   "code_search_url": "https://git.[company].com/api/v3/search/code?q={query}{&page,per_page,sort,order}",
2020-03-13T05:53:38.8071647Z   "commit_search_url": "https://git.[company].com/api/v3/search/commits?q={query}{&page,per_page,sort,order}",
2020-03-13T05:53:38.8073059Z   "emails_url": "https://git.[company].com/api/v3/user/emails",
2020-03-13T05:53:38.8074445Z   "emojis_url": "https://git.[company].com/api/v3/emojis",
2020-03-13T05:53:38.8075767Z   "events_url": "https://git.[company].com/api/v3/events",
2020-03-13T05:53:38.8077032Z   "feeds_url": "https://git.[company].com/api/v3/feeds",
2020-03-13T05:53:38.8078149Z   "followers_url": "https://git.[company].com/api/v3/user/followers",
2020-03-13T05:53:38.8079345Z   "following_url": "https://git.[company].com/api/v3/user/following{/target}",
2020-03-13T05:53:38.8080572Z   "gists_url": "https://git.[company].com/api/v3/gists{/gist_id}",
2020-03-13T05:53:38.8081548Z   "hub_url": "https://git.[company].com/api/v3/hub",
2020-03-13T05:53:38.8082418Z   "issue_search_url": "https://git.[company].com/api/v3/search/issues?q={query}{&page,per_page,sort,order}",
2020-03-13T05:53:38.8083431Z   "issues_url": "https://git.[company].com/api/v3/issues",
2020-03-13T05:53:38.8084430Z   "keys_url": "https://git.[company].com/api/v3/user/keys",
2020-03-13T05:53:38.8086272Z   "label_search_url": "https://git.[company].com/api/v3/search/labels?q={query}&repository_id={repository_id}{&page,per_page}",
2020-03-13T05:53:38.8091275Z   "notifications_url": "https://git.[company].com/api/v3/notifications",
2020-03-13T05:53:38.8092166Z   "organization_repositories_url": "https://git.[company].com/api/v3/orgs/{org}/repos{?type,page,per_page,sort}",
2020-03-13T05:53:38.8093095Z   "organization_url": "https://git.[company].com/api/v3/orgs/{org}",
2020-03-13T05:53:38.8096487Z   "public_gists_url": "https://git.[company].com/api/v3/gists/public",
2020-03-13T05:53:38.8097620Z   "rate_limit_url": "https://git.[company].com/api/v3/rate_limit",
2020-03-13T05:53:38.8098584Z   "repository_url": "https://git.[company].com/api/v3/repos/{owner}/{repo}",
2020-03-13T05:53:38.8100945Z   "repository_search_url": "https://git.[company].com/api/v3/search/repositories?q={query}{&page,per_page,sort,order}",
2020-03-13T05:53:38.8102239Z   "current_user_repositories_url": "https://git.[company].com/api/v3/user/repos{?type,page,per_page,sort}",
2020-03-13T05:53:38.8104230Z   "starred_url": "https://git.[company].com/api/v3/user/starred{/owner}{/repo}",
2020-03-13T05:53:38.8104831Z   "starred_gists_url": "https://git.[company].com/api/v3/gists/starred",
2020-03-13T05:53:38.8105328Z   "team_url": "https://git.[company].com/api/v3/teams",
2020-03-13T05:53:38.8105772Z   "user_url": "https://git.[company].com/api/v3/users/{user}",
2020-03-13T05:53:38.8106279Z   "user_organizations_url": "https://git.[company].com/api/v3/user/orgs",
2020-03-13T05:53:38.8106871Z   "user_repositories_url": "https://git.[company].com/api/v3/users/{user}/repos{?type,page,per_page,sort}",
2020-03-13T05:53:38.8107491Z   "user_search_url": "https://git.[company].com/api/v3/search/users?q={query}{&page,per_page,sort,order}"
2020-03-13T05:53:38.8108556Z }
2020-03-13T05:53:38.8109344Z Read asset tags: curl -sH Authorization: token [Edited: Secret] https://git.[company].com/api/v3/repos/[owner]/[MyTestRepo]/releases/tags/MBP_TestTag6
2020-03-13T05:53:39.0378180Z Response: {
2020-03-13T05:53:39.0379040Z   "message": "Not Found",
2020-03-13T05:53:39.0379599Z   "documentation_url": "https://developer.github.com/enterprise/2.20/v3/repos/releases/#get-a-release-by-tag-name"
2020-03-13T05:53:39.0380100Z }
2020-03-13T05:53:39.2992296Z 
2020-03-13T05:53:39.3060923Z ##[error]Bash exited with code '1'.
2020-03-13T05:53:39.3120104Z ##[section]Finishing: Bash Script

从日志中可以看出,调用 api 以获取提供的标签(甚至是最新标签)的信息时总是失败。

知道系统为什么要重定向我吗?

【问题讨论】:

  • 也许你缺少一些标题。您可以尝试使用浏览器,在网络选项卡上使用Inspect mode 转到目标页面并查看请求标头。

标签: bash git github azure-devops azure-pipelines-release-pipeline


【解决方案1】:

您应该在这一行进行更改:

GH_API="https://git.[company].com/api/v3"

GH_API="https://api.github.com"

【讨论】:

  • 对不起,但我 99% 确定这不会起作用,因为访问 GitHub 和 GitHub Enterprise 之间存在很大差异,后者是我在这里使用的。 git.[company].com/api/v3 是根据 GitHub Enterprise 文档使用的 url。这是根据developer.github.com/enterprise/2.20/v3/enterprise-admin 的文档
  • @user5763204,你有没有注意到你分享的链接中的这句话? In addition to the GitHub.com API, these GitHub Enterprise-specific endpoints are available.
  • 是的,我已经看到了。由于我工作的公司拥有 GitHub Enterprise,并且所有存储库都是内部的,并且只能通过 Enterprise 服务器访问,因此我们无法通过 GitHub 本身访问任何内容。是的,我已经对此进行了测试。通过 github api (api.github.com) 在我的项目中查询版本没有任何结果,但是通过 github 企业 api (git.[company].com/api/v3) 我得到了版本列表。身份验证和其他命令也是如此。
  • 我开始怀疑公司禁用了某些 api 功能,或者新 api 中存在错误。 1) 查询 .../releases 只带回预发布版本。 2) ...releases/latest 只返回“未找到” 3) .../releases/[id of pre-release] 带回预发布的数据 4) .../releases/tags 带回一个所有 29 个标签的列表(每个标签都是一个版本) 5) .../releases/tags/latest 带回“未找到”。那么,禁用还是错误?我不知道,这里(公司)没有人知道在哪里可以找到......
【解决方案2】:

我遇到的问题与 Git 处理发布的方式有关。在这种情况下,有一个预发布版本(版本名称/标签:v0.0.1),这是我在查询所有版本时得到的。一旦我删除了这个预发布,我什么也没回来,但后来意识到其他“发布”都没有发布过。

在发布了几个版本之后,我终于得到了一份已发布版本的列表。然后所有其他命令也按预期工作。

所以,没有险恶的问题或错误。只是 DevOps 管道新手的一些小错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    相关资源
    最近更新 更多