LoginSignup
0
0

More than 1 year has passed since last update.

複数AWSアカウントでCodeCommitにアクセスする認証情報を切り替える方法(Windows環境)

Last updated at Posted at 2022-08-08

目的

備忘録。

複数のAWSアカウントでCodeCommitを利用する際に資格情報を切り替えるのが面倒に感じたため、その手順を残します。
ちなみに同一AWSアカウントでの複数IAMユーザ切替えでも同様です。

環境

  • Windows 10
  • git version 2.31.1.windows.1

やり方

Gitでcloneする際にnamespaceを指定することで、Windowsの資格情報マネージャで個別に資格情報を管理することができます。
ただし、そのままだと.git/configに反映されないので手動で反映する必要があります。

【Git clone】
git -c credential.namespace=${NAMESPACE} clone ${REPOSITORY_URL} ${DEST}

【設定を追加】
cd ${DEST}
git config credential.namespace ${NAMESPACE}

スクリプト

定型処理なのでスクリプトファイルにしてパスを通したフォルダに置いて使います。※PowerShellはBOM付きで保存してください。
また、よく使うnamespaceはデフォルトに設定しておくと使いやすくなります。

gitc.ps1
param (
    [parameter(mandatory = $true)][String] $namespace = "",
    [parameter(ValueFromRemainingArguments = $true)] $others
)

if (${namespace}.IndexOf("https://") -eq 0) {
    ${tmp} = @(${namespace}, ${others})
    ${others} = ${tmp}
    ${namespace} = "デフォルトにしたいnamespace"
}

if ($others[0].IndexOf("https://") -ne 0) {
    Write-Host "[Error] git clone の引数を指定してください"
    exit 1
}

if ("${namespace}" -eq "" ) {
    Write-Host git clone ${others}
    git clone ${others}
    exit
}

Write-Host git -c credential.namespace=${namespace} clone ${others}
git -c credential.namespace=${namespace} clone ${others}

$dest = Split-Path $others[0] -Leaf

if (-Not $(Test-Path $dest)) {
    Write-Host "[Error] クローンしたディレクトリが見つかりません: ${dest}"
    exit 1
}

Push-Location ${dest}
git config credential.namespace ${namespace}
Pop-Location

所感

どのAWSアカウントの資格情報でアクセスするのかスッキリ。
ちなみに資格情報の確認と削除は「資格情報マネージャー」から行えます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0