본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Git 자격 증명

원문 보기 위키 갱신

독립 실행형 Buildkite 에이전트 설치와 마찬가지로, 프라이빗 Git 저장소에 접근해서 clone하려면 에이전트가 사용할 수 있도록 Git 자격 증명을 준비해야 해요. 자격 증명은 ssh://로 clone하기 위한 SSH 키 형태일 수도 있고, https://로 clone하기 위한 .git-credentials 파일 형태일 수도 있어요.

출처: 문서

본문

Git 자격 증명

독립 실행형 Buildkite 에이전트 설치와 마찬가지로, 프라이빗 Git 저장소에 접근하고 clone하려면 에이전트가 사용할 Git 자격 증명을 준비해 둬야 해요. 이 자격 증명은 ssh://로 clone하기 위한 SSH 키 형식이거나, https://로 clone하기 위한 .git-credentials 파일 형식일 수 있어요.

SSH 키로 저장소 clone하기

SSH로 프라이빗 Git 저장소를 clone하려면 승인된 SSH 프라이빗 키가 담긴 Kubernetes Secret을 만들고, Buildkite Agent Stack for Kubernetes가 이 Secret을 Git 저장소 clone을 수행하는 checkout 컨테이너에 마운트하도록 구성해야 해요.

SSH 프라이빗 키로 Kubernetes Secret 만들기

🚧 경고! DSA 키 지원은 2025년 초 OpenSSH에서 제거되었어요. 이 변경은 buildkite/agent 버전 v3.88.0 이후에 영향을 줘요. RSA, ECDSA, EDDSA 키로 마이그레이션하세요.

SSH 키 쌍을 만들고 그 공개 키를 원격 저장소 제공자(예: GitHub)에 등록한 후에는, SSH 프라이빗 키 파일로 Kubernetes Secret을 만들 수 있어요.

환경 변수 이름이 docker-ssh-env-config에서 인식되는 이름(SSH_PRIVATE_*_KEY)과 일치하는지 확인하세요.

  • SSH_PRIVATE_ECDSA_KEY
  • SSH_PRIVATE_ED25519_KEY
  • SSH_PRIVATE_RSA_KEY

이 프로젝트의 스크립트는 기본 buildkite/agent Docker 이미지의 기본 엔트리포인트에 포함돼 있어요. 이 스크립트가 Kubernetes Secret의 값을 처리해서 checkout 컨테이너의 ~/.ssh 디렉터리에 프라이빗 키를 써 줘요.

SSH 프라이빗 키 파일 $HOME/.ssh/id_rsa의 내용이 담긴 my-git-ssh-credentials라는 Kubernetes Secret을 만들려면:

kubectl create secret generic my-git-ssh-credentials --from-file=SSH_PRIVATE_RSA_KEY="$HOME/.ssh/id_rsa" -n buildkite

이 Kubernetes Secret은 컨트롤러 구성 안에서 EnvFrom으로, 또는 kubernetes 플러그인의 gitEnvFrom 구성으로 Buildkite Agent Stack for Kubernetes 컨트롤러가 참조할 수 있어요.

구성 파일로 Kubernetes Secret 제공하기

pod-spec-patch를 사용하면 구성 값 YAML 파일에서 envFrom으로 SSH 프라이빗 키가 담긴 Kubernetes Secret을 지정할 수 있어요.

# values.yaml
...
config:
  ...
  pod-spec-patch:
    containers:
    - name: checkout                    # <---- this is needed for the secret to only be mounted on the checkout container
      envFrom:
      - secretRef:
          name: my-git-ssh-credentials  # <---- this is an example name of the Kubernetes Secret

Kubernetes 플러그인으로 Kubernetes Secret 제공하기

kubernetes 플러그인 아래에서 gitEnvFrom 구성으로 Kubernetes Secret의 이름을 지정해요.

# pipeline.yaml
steps:
- label: "\:kubernetes\: Hello World!"
  command: echo Hello World!
  agents:
    queue: kubernetes
  plugins:
  - kubernetes:
      gitEnvFrom:
      - secretRef:
          name: my-git-ssh-credentials  # <---- this is an example name of the Kubernetes Secret

📘 kubernetes 플러그인으로 Kubernetes Secret의 SSH 프라이빗 키를 제공한다면, 프라이빗 Git 저장소에 접근해야 하는 모든 단계에 이 구성을 정의해야 해요. Buildkite Agent Stack for Kubernetes 컨트롤러 구성으로 Kubernetes Secret을 정의한다면 한 번만 구성하면 돼요.

checkout이 아닌 컨테이너에 SSH 프라이빗 키 제공하기

위 구성은 SSH 프라이빗 키를 Kubernetes Secret으로 checkout 컨테이너에만 제공해요. 사용자 정의 job 컨테이너에 Git SSH 자격 증명이 필요하다면 다음과 같은 옵션을 쓸 수 있어요.

  • 기본 buildkite/agent Docker 이미지를 기반으로 한 컨테이너 이미지를 사용하되, job 스펙에서 command를 재정의하지 않아 기본 엔트리포인트가 유지되도록 해요.
  • job 컨테이너 이미지의 엔트리포인트에 ssh-env-config.sh 스크립트의 기능을 포함하거나 재현해서 인식되는 환경 변수 이름에서 가져오도록 해요.

다음 예시는 container-0에 SSH 프라이빗 키를 설정하는 방법을 보여줘요.

# values.yaml
...
config:
  ...
  pod-spec-patch:
    containers:
    ...
    - name: container-0
      env:
        - name: SSH_PRIVATE_RSA_KEY
          valueFrom:
            secretKeyRef:
              name: my-git-ssh-credentials # <---- this is the name of the Kubernetes Secret to use for container-0 (setup on the same way as the command above)
              key: SSH_PRIVATE_RSA_KEY

container-0에 사용할 SSH 키를 설정했다면, git 연산을 위해 container-0을 설정해야 해요. 권장하는 방법은 pre-command 훅을 구현해서 ssh-keyscan을 실행하고 known_hosts 파일을 생성하는 거예요.

#!/bin/bash
set -eufo pipefail

eval $(ssh-agent -s)
mkdir -p ~/.ssh
chmod 700 ~/.ssh

touch ~/.ssh/config
chmod 600 ~/.ssh/config
touch ~/.ssh/known_hosts
chmod 600 ~/.ssh/known_hosts

ssh-keyscan github.com >> ~/.ssh/known_hosts
echo "$SSH_PRIVATE_RSA_KEY" | tr -d '\r' | ssh-add -

이제 파이프라인 YAML의 command 단계에서 git clone 같은 git 연산을 추가할 수 있어요.

# pipeline.yaml
steps:
  - label: "\:kubernetes\: Hello World!"
    command:  git clone -v git@github.com:{repo_name}.git
    plugins:
      - kubernetes:
          podSpec:
            containers:
                - image: buildkite/agent:alpine-k8s

Git 자격 증명으로 저장소 clone하기

HTTPS로 프라이빗 Git 저장소를 clone하려면 Secret에 저장된 .git-credentials 파일을 사용하고, gitCredentialsSecret checkout 파라미터로 이 Secret을 참조하면 돼요.

Git 자격 증명 파일로 Kubernetes Secret 만들기

store Git credential helper가 기대하는 형식의 .git-credentials 파일을 만들어요. 파일을 만든 후에는 이 파일의 내용이 담긴 Kubernetes Secret을 만들 수 있어요.

kubectl create secret generic my-git-https-credentials --from-file='.git-credentials'="$HOME/.git-credentials" -n buildkite

구성 파일로 Kubernetes Secret 제공하기

default-checkout-params를 사용하면 구성 YAML 파일을 통해 Kubernetes Secret을 다음과 같이 정의할 수 있어요.

# values.yaml
...
config:
  ...
  default-checkout-params:
    gitCredentialsSecret:
      secretName: my-git-https-credentials

Kubernetes Secret 안에서 .git-credentials와 다른 키를 사용하고 싶다면, gitCredentialsSecret 안의 items를 사용해 프로젝션해서 .git-credentials로 만들 수 있어요.

# values.yaml
...
default-checkout-params:
  gitCredentialsSecret:
    secretName: my-git-https-credentials
    items:
    - key: funky-creds
      path: .git-credentials

Kubernetes 플러그인으로 Kubernetes Secret 제공하기

kubernetes 플러그인 아래에서 checkout.gitCredentialsSecret 구성으로 Kubernetes Secret의 이름을 지정해요.

# pipeline.yaml
steps:
- label: "\:kubernetes\: Hello World!"
  command: echo Hello World!
  agents:
    queue: kubernetes
  plugins:
  - kubernetes:
      checkout:
        gitCredentialsSecret:
          secretName: my-git-https-credentials  # <---- this is the name of the Kubernetes Secret (ex. my-git-https-credentials, from command above)

checkout이 아닌 컨테이너에 Git 자격 증명 제공하기

위 구성은 Git 자격 증명을 Kubernetes Secret으로 checkout 컨테이너에만 제공해요. .git-credentials 파일이 사용자 정의 job 컨테이너에 필요하다면 git-credentials 볼륨에 대한 볼륨 마운트를 추가하고, 그 안의 파일을 사용하도록 Git을 구성하면 돼요(예: git config credential.helper 'store --file ...').

Checkout 건너뛰기 (v0.13.0 이상)

📘 버전 요구 사항 아래 구성 옵션을 구현하려면 Agent Stack for Kubernetes 컨트롤러의 v0.13.0 이상이 필요해요.

일부 단계에서는 checkout(소스 저장소 clone)을 피하고 싶을 수 있어요. kubernetes 플러그인의 checkout 블록으로 할 수 있어요.

steps:
- label: "\:kubernetes\: Hello World!"
  agents:
    queue: kubernetes
  plugins:
  - kubernetes:
      checkout:
        skip: true # prevents scheduling the checkout container

default-checkout-params 사용하기

default-checkout-params를 사용하면 checkout, command, sidecar 컨테이너 각각에 envFrom을 추가할 수 있어요. 이는 파이프라인에서 단계별로 하거나 values.yaml에서 모든 job에 대해 설정할 수 있어요.

파이프라인 예시(블록이 각각 checkout, commandParams, sidecarParams라는 점을 주목하세요):

# pipeline.yml
...
  kubernetes:
    checkout:
      envFrom:
      - prefix: GITHUB_
        secretRef:
          name: github-secrets
    commandParams:
      interposer: vector
      envFrom:
      - prefix: DEPLOY_
        secretRef:
          name: deploy-secrets
    sidecarParams:
      envFrom:
      - prefix: LOGGING_
        configMapRef:
          name: logging-config

values.yml 구성 파일로 이렇게 하는 예시예요.

# values.yml
config:
  default-checkout-params:
    envFrom:
    - prefix: GITHUB_
      secretRef:
        name: github-secrets
  default-command-params:
    interposer: vector
    envFrom:
    - prefix: DEPLOY_
      secretRef:
        name: deploy-secrets
  default-sidecar-params:
    envFrom:
    - prefix: LOGGING_
      configMapRef:
        name: logging-config

더 알아보기 (Learn more)

Agent Stack for Kubernetes에서 프라이빗 Git 저장소에 접근하는 방법을 함께 살펴봤어요. SSH 키 방식과 .git-credentials 방식, 그리고 각각을 Kubernetes Secret으로 어떻게 구성하는지 익혔죠. checkout 컨테이너뿐 아니라 사용자 정의 job 컨테이너에 자격 증명을 전달하는 방법까지 알게 되면, 다양한 파이프라인 구성에 유연하게 대응할 수 있을 거예요.