'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.


Solution 1:[1]

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

Solution 2:[2]

After adding sudo, it works for me.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 finefoot
Solution 2