Search code examples
authenticationgithubcontinuous-integrationcontinuous-deploymentgithub-actions

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

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