Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Last active July 14, 2020 16:00
Show Gist options
  • Save Luzifer/0c9d5502d7fc32faad8976835d2fd9d6 to your computer and use it in GitHub Desktop.
Save Luzifer/0c9d5502d7fc32faad8976835d2fd9d6 to your computer and use it in GitHub Desktop.
Git credential-helper to supply $GITHUB_TOKEN to github auth
#!/bin/bash
set -euo pipefail
###
# How to use?
#
# - Download into path with name `git-credential-githubenv`
# - Enable helper `git config --global credential.helper 'githubenv'`
# - Expose corresponding token as `GITHUB_TOKEN` env variable
#
# Pay attention: Credentials in ENV variables is a bad idea on insecure
# or shared systems. If anyone can anyhow access your system, they can
# access your env variables! So use with care and don't blame me if
# things have gone very wrong.
###
action=${1:-invalid}
case ${action} in
get) ;; # Only supported action
*)
echo "Action not supported: ${action}" >&2
exit 1
;;
esac
[[ -n ${GITHUB_TOKEN:-} ]] || exit 1
declare -A input
while read LINE; do
[[ -n ${LINE} ]] || break
key=$(echo "${LINE}" | cut -d '=' -f 1)
value=$(echo "${LINE}" | cut -d '=' -f 2-)
input[$key]=$value
done
[[ ${input["protocol"]} == "https" ]] && [[ ${input["host"]} == "github.com" ]] || exit 1
for k in ${!input[@]}; do
echo "${k}=${input[$k]}"
done
echo "username=api"
echo "password=${GITHUB_TOKEN}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment