0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Docker】Jenkinsの認証設定(リモートホスト、GitHub、DockerHub)

Last updated at Posted at 2025-10-29

1. リモートホスト(SSH)認証設定

リモートホスト側(公開鍵を登録)

mkdir -p ~/.ssh
cat <<'PUB' >> ~/.ssh/authorized_keys
# AAAAC3NzaC1lZDI1... → Jenkinsに登録する秘密鍵のペアとなる公開鍵
# jenkins-deploy → 任意の識別用ラベル
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEXAMPLE_PUBLIC_KEY_CONTENTS jenkins-deploy
PUB
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

Jenkins 側(秘密鍵を登録)

登録画面:
Jenkins → Manage JenkinsCredentials → Global → Add Credentials

項目 設定値例 説明
Kind SSH Username with private key SSH秘密鍵を登録する
ID ssh_build_host Jenkinsfile 内で参照するID
Username deployuser リモートホストのユーザー名
Private Key -----BEGIN OPENSSH PRIVATE KEY----- ... 秘密鍵内容を貼り付ける

Jenkinsfile 内での参照例

withCredentials([sshUserPrivateKey(
  credentialsId: 'ssh_build_host',  // Jenkinsで登録したIDに合わせて変更
  keyFileVariable: 'SSH_KEY',
  usernameVariable: 'SSH_USER'
)]) {
  sh '''
  # 192.168.10.64 → リモートホストの実際のIPまたはホスト名に変更
  ssh -i "$SSH_KEY" "${SSH_USER}@192.168.10.64" docker version
  '''
}

2. Docker Hub 認証設定

登録画面:
Jenkins → Manage JenkinsCredentials → Global → Add Credentials

項目 設定値例 説明
Kind Username with password ユーザー名+トークンでログイン
ID dockerhub_example Jenkinsfile 内で参照するID
Username exampleuser Docker Hub のユーザー名
Password docker_hub_token Docker Hub のトークン

Jenkinsfile 内での参照例

withCredentials([usernamePassword(
  credentialsId: 'dockerhub_example',  // Jenkinsで登録したIDに変更
  usernameVariable: 'DUSER',
  passwordVariable: 'DPASS'
)]) {
  sh '''
  echo "$DPASS" | docker login -u "$DUSER" --password-stdin docker.io
  '''
}

3. GitHub 認証設定

登録画面:
Jenkins → Manage JenkinsCredentials → Global → Add Credentials

項目 設定値例 説明
Kind Username with password PAT を登録する場合
ID github_example Jenkins SCM設定時に選択するID
Username example-org GitHub ユーザー名または組織名
Password ghp_xxxxxxxxxxxxx Personal Access Token(PAT)

Jenkinsジョブ設定時の利用例

  • Pipeline script from SCM

    • Repository URL:GitリポジトリのURL
    • Branch:main
    • Credentials:登録済みのCredential ID

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?