Skip to content

Commit

Permalink
added git_remotes_set_https_creds_helpers.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
HariSekhon committed Feb 9, 2024
1 parent e3fe512 commit cff2785
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions git/git_remotes_set_https_creds_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2024-02-09 02:48:01 +0000 (Fri, 09 Feb 2024)
#
# https://github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#

set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/utils.sh"

# shellcheck disable=SC2034,SC2154
usage_description="
Creates Git credential helper config for the major Git hosting providers if their tokens are found in the local environment
Support GitHub, GitLab, Bitbucket and Azure DevOps
Checks for the following environment variables and if they're set then set up the credentials helper for dynamic HTTPS credentials
This is useful because Azure DevOps personal access tokens have a maximum expiry of 1 year so you don't want to hardcode them across two dozen git repo checkouts but rather all inherit the auth from the environment
If you travel and lot, sometimes you can only git push through HTTPS due to egress port filtering in hotels
When combined with the adjacent scripts
git_remotes_set_ssh_to_https.sh
and
git_foreach_repo.sh
this allows you to quickly switch many git repo checkouts from SSH to HTTPS and have them all inherit the environment auth tokens
If no provider is found, checks for the following environment variables and sets them automatically in the local git repo if they're both found
GitHub:
GITHUB_USER
GITHUB_TOKEN / GH_TOKEN
GitLab:
GITLAB_USER
GITLAB_TOKEN
Bitbucket:
BITBUCKET_USER
BITBUCKET_TOKEN
Azure DevOps:
AZURE_DEVOPS_USER
AZURE_DEVOPS_TOKEN
"

# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="[<github|gitlab|bitbucket|azure|all>]"

help_usage "$@"

max_args 1 "$@"

provider="${1:-all}"

github_cred_helper(){
if [ -n "${GITHUB_USER:-}" ]; then
# GH_TOKEN is higher precedence in GitHub CLI so do the same here for consistency to avoid non-intuitive auth problems where one works and the other doesn't using different tokens
if [ -n "${GH_TOKEN:-${GITHUB_TOKEN:-}}" ]; then
timestamp "Setting credential helper for GitHub"
# env vars need to be evaluated dynamically not here
# shellcheck disable=SC2016
git config credential.https://github.com.helper '!f() { sleep 1; echo "username=${GITHUB_USER}"; echo "password=${GH_TOKEN:-${GITHUB_TOKEN}}"; }; f'
fi
fi
}

gitlab_cred_helper(){
if [ -n "${GITHUB_USER:-}" ]; then
if [ -n "${GITLAB_TOKEN:-}" ]; then
timestamp "Setting credential helper for GitLab"
# shellcheck disable=SC2016
git config credential.https://gitlab.com.helper '!f() { sleep 1; echo "username=${GITLAB_USER}"; echo "password=${GITLAB_TOKEN}"; }; f'
fi
fi
}

bitbucket_cred_helper(){
if [ -n "${GITHUB_USER:-}" ]; then
if [ -n "${BITBUCKET_TOKEN:-}" ]; then
timestamp "Setting credential helper for Bitbucket"
# shellcheck disable=SC2016
git config credential.https://bitbucket.org.helper '!f() { sleep 1; echo "username=${BITBUCKET_USER}"; echo "password=${BITBUCKET_TOKEN}"; }; f'
fi
fi
}

azure_devops_cred_helper(){
if [ -n "${GITHUB_USER:-}" ]; then
if [ -n "${AZURE_DEVOPS_TOKEN:-}" ]; then
timestamp "Setting credential helper for Azure DevOps"
# shellcheck disable=SC2016
git config credential.https://dev.azure.com.helper '!f() { sleep 1; echo "username=${AZURE_DEVOPS_USER}"; echo "password=${AZURE_DEVOPS_TOKEN}"; }; f'
fi
fi
}

if [ "$provider" = all ]; then
github_cred_helper
gitlab_cred_helper
bitbucket_cred_helper
azure_devops_cred_helper
elif [ "$provider" = github ]; then
github_cred_helper
elif [ "$provider" = gitlab ]; then
gitlab_cred_helper
elif [ "$provider" = bitbucket ]; then
bitbucket_cred_helper
elif [ "$provider" = azure_devops ]; then
azure_devops_cred_helper
else
usage "unrecognized provider specified: $provider"
fi

0 comments on commit cff2785

Please sign in to comment.