Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

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

The following exemplary workflow runs without issues:

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

However, some of my CI/CD code needs to run in a container:

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

Now, hub suddenly doesn't work anymore:

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.
question from:https://stackoverflow.com/questions/65894993/github-actions-with-hub-results-in-unauthorized-http-401-bad-credentials

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue was actually with mismatching versions: hub on native ubuntu-latest GitHub Actions was the (as of now) most recent version 2.14.2 while apt install on the ubuntu:latest container installed only version 2.7.0 (from Dec 28, 2018!).

The solution is to install the latest hub binary directly from their GitHub releases page instead of using 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 '"' '
' | 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)"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...