How to set up git and GitHub CLI (gh) for Codex Web (Cloud)

I had quite some issues trying to set up git and GitHub access for Codex Cloud. Now that I managed to get it working, I thought others might find it useful.

Setup script:

ensure_github_cli() {
  if command -v gh &> /dev/null; then
    return 0
  fi

  if command -v apt-get &> /dev/null; then
    apt-get update -y -qq
    apt-get install -y -qq gh
    return 0
  fi

  echo "GitHub CLI (gh) not found and no supported package manager available." >&2
  return 1
}

ensure_github_cli

if [ -n "${GITHUB_TOKEN:-}" ]; then
  github_token="${GITHUB_TOKEN}"
  unset GITHUB_TOKEN

  echo "${github_token}" | gh auth login --with-token
  gh auth setup-git
fi

if ! git remote get-url origin &> /dev/null; then
  git remote add origin https://github.com/YOUR/REPO.git
else
  git remote set-url origin https://github.com/YOUR/REPO.git
fi

Maintenance script:

if ! git remote get-url origin &> /dev/null; then
  git remote add origin https://github.com/YOUR/REPO.git
else
  git remote set-url origin https://github.com/YOUR/REPO.git
fi

You need to add a GitHub token as a secret in your environment and change YOUR/REPO to your org/repo. Then Codex should be able to use both git and GitHub CLI.

2 Likes