GitHub 使用中心的操作会导致未经授权 (HTTP 401) 凭据错误

GitHub Actions with hub results in Unauthorized (HTTP 401) Bad credentials

以下示例性工作流程 运行s 没有问题:

on: [push]

jobs:
  create_release:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Create release
        run: hub release create -m "$(date)" "v$(date +%s)"

但是,我的一些 CI/CD 代码需要 运行 在容器中:

on: [push]

jobs:
  create_release:
    runs-on: ubuntu-latest
    container:
      image: ubuntu:latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Install dependencies
        run: apt update && apt install -y git hub
      - name: Checkout
        uses: actions/checkout@v2
      - name: Create release
        run: hub release create -m "$(date)" "v$(date +%s)"

现在,hub突然不能用了:

Run hub release create -m "$(date)" "v$(date +%s)"
  hub release create -m "$(date)" "v$(date +%s)"
  shell: sh -e {0}
  env:
    GITHUB_TOKEN: ***
Error creating release: Unauthorized (HTTP 401)
Bad credentials
Error: Process completed with exit code 1.

问题实际上是版本不匹配:hub 原生 ubuntu-latest GitHub Actions 是(截至目前)最新版本 2.14.2 而 apt install ubuntu:latest 容器仅安装了 2.7.0 版(自 2018 年 12 月 28 日起!)。

解决方案是直接从 GitHub 发布页面安装最新的 hub 二进制文件,而不是使用 apt:

on: [push]
 
jobs:
  create_release:
    runs-on: ubuntu-latest
    container:
      image: ubuntu:latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Install dependencies
        run: |
          apt update && apt install -y git wget
          url="$(wget -qO- https://api.github.com/repos/github/hub/releases/latest | tr '"' '\n' | grep '.*/download/.*/hub-linux-amd64-.*.tgz')"
          wget -qO- "$url" | tar -xzvf- -C /usr/bin --strip-components=2 --wildcards "*/bin/hub"
      - name: Checkout
        uses: actions/checkout@v2
      - name: Create release
        run: hub release create -m "$(date)" "v$(date +%s)"

添加sudo后,对我有用

- name: Install Deps
  run: |
    sudo apt-get update 2> /dev/null || true
    sudo apt-get install -y git 
    sudo apt-get install -y wget
    url="$(sudo wget -qO- https://api.github.com/repos/github/hub/releases/latest | tr '"' '\n' | grep '.*/download/.*/hub-linux-amd64-.*.tgz')"
    sudo wget -qO- "$url" | sudo tar -xzvf- -C /usr/bin --strip-components=2 --wildcards "*/bin/hub"