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.

This was a huge help, thanks! One thing that was slightly not obvious was the unset of GITHUB_TOKEN, but there is codex cloud itself enforces that that be unset, otherwise it will fail at setup time. Thanks again!

This was what I needed. Thank you!

I am new to codex and the most natural use case was for me to send my requests to the cloud and let it push changes. It was puzzling why it could not connect to github even after using the connector provided. Lucky to have stumbled upon your guide while searching the forums. Definitely giving this a +1 so more can look up for this.

Sidenote: turns out the connector enables a “Create PR” UI button on codex web on the tasks you have assigned it. I guess that could work too but it was not the most natural workflow I was looking at.

I can’t believe how many hours I spent trying to fix this. I even used the support AI Chatbot on the support page and it didn’t have any clue as to how to fix this. The chatbot kept leading me down rabbit holes with no resolution

god, i spent 6h asking chatgpt for solutions and the good old google saved my ass! thanks for the great help. it’s hilarious that I spent 6h just trying to get the codex cloud to work with github and the first actual coding job was done in 30min lol openAI must spend more effort on the setup documentation - this is frustrating for new comers just to figure out how to start…