GitHub Actions 연동
GitHub Actions 연동
GitHub은 OAuth ID 프로바이더로, GitHub Actions에서 저장소와 실행 위치를 식별하는 토큰을 생성하는 데 사용할 수 있습니다. OAuth 2.0 토큰 교환(Token Exchange) 방식이 필요하며, Dex 같은 일부 ID 프로바이더는 이를 기본 지원합니다.
출처: 문서
본문
GitHub Actions
GitHub은 OAuth ID 프로바이더로, GitHub Actions에서 저장소와 실행 위치를 식별하는 토큰을 생성하는 데 사용할 수 있습니다.
OAuth 2.0 토큰 교환(Token Exchange)을 사용해야 합니다. Dex 같은 일부 ID 프로바이더는 이를 기본 지원합니다.
Dex 사용
argocd-cm을 편집하고 dex.config 섹션을 구성하세요:
dex.config: |
connectors:
- type: oidc
id: github-actions
name: GitHub Actions
config:
issuer: https://token.actions.githubusercontent.com/
# If using GitHub Enterprise Server, then use this issuer:
#issuer: https://github.example.com/_services/token
scopes: [openid]
userNameKey: sub
insecureSkipEmailVerified: true
ArgoCD는 GitHub Action에서 토큰을 가져오는 데 사용할 수 있는 argo-cd-cli라는 정적 클라이언트를 자동으로 생성합니다.
다음은 Dex에서 유효한 Argo CD 인증 토큰을 가져와 CLI로 작업을 수행하는 GitHub Action 예시입니다:
name: argocd-test
on:
pull_request:
permissions:
id-token: write # This is required for requesting the JWT
jobs:
argocd-test:
runs-on:
group: ephemeral_runners
steps:
# Actions have access to two special environment variables ACTIONS_CACHE_URL and ACTIONS_RUNTIME_TOKEN.
# Inline step scripts in workflows do not see these variables.
- uses: actions/github-script@v6
id: script
timeout-minutes: 10
with:
debug: true
script: |
const token = process.env['ACTIONS_RUNTIME_TOKEN']
const runtimeUrl = process.env['ACTIONS_ID_TOKEN_REQUEST_URL']
core.setOutput('TOKEN', token.trim())
core.setOutput('IDTOKENURL', runtimeUrl.trim())
- name: Obtain access token
id: idtoken
run: |
# get an token from github
echo "getting token from GitHub"
GH_TOKEN_RESPONSE=$(curl -sSf \
"${{steps.script.outputs.IDTOKENURL}}" \
-H "Authorization: bearer ${{ste...EN}}" \
-H "Accept: application/json; api-version=2.0" \
-H "Content-Type: application/json" \
-d "{}" \
)
GH_TOKEN=$(jq -r .value <<< $GH_TOKEN_RESPONSE)
echo "::add-mask::$GH_TOKEN"
# exchange it for a dex token
DEX_URL="https://argocd.example.com/api/dex/token"
echo "getting access token from Dex: $DEX_URL"
DEX_TOKEN_RESPONSE=$(curl -sSf \
"$DEX_URL" \
--user argo-cd-cli: \
--data-urlencode "connector_id=github-actions" \
--data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
--data-urlencode "scope=openid email profile federated:id" \
--data-urlencode "requested_token_type=urn:ietf:params:oauth:token-type:access_token" \
--data-urlencode "subject_token=$GH_TOKEN" \
--data-urlencode "subject_token_type=urn:ietf:params:oauth:token-type:id_token")
DEX_TOKEN=$(jq -r .access_token <<< $DEX_TOKEN_RESPONSE)
if [[ -z "$DEX_TOKEN" ]]; then
echo "::error::No token found in dex response"
exit 1
fi
echo "::add-mask::$(echo "$DEX_TOKEN" | base64 -w0)"
echo "::add-mask::$DEX_TOKEN"
echo "dex-token=$DEX_TOKEN" >> "$GITHUB_OUTPUT"
# use $DEX_TOKEN
- name: Setup ArgoCD CLI
run: |
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
mkdir -p "$RUNNER_TEMP/argocd"
install -m 555 argocd-linux-amd64 "$RUNNER_TEMP/argocd/argocd"
rm argocd-linux-amd64
echo "$RUNNER_TEMP/argocd" >> "$GITHUB_PATH"
- name: Use CLI in some commands
env:
ARGOCD_AUTH_TOKEN: ${{ steps.idtoken.outputs.dex-token }}
ARGOCD_SERVER: argocd.example.com
ARGOCD_OPTS: --grpc-web
run: |
set -x
argocd version
argocd account get-user-info
argocd proj list
argocd app list
RBAC 구성
ArgoCD v3.0.0 이상을 사용할 때는 policy.csv를 다음과 같이 정의합니다:
configs:
rbac:
policy.csv: |
p, repo:my-org/my-repo:pull_request, projects, get, my-project, allow
p, repo:my-org/my-repo:pull_request, applications, get, my-project/*, allow
p, repo:my-org/my-repo:pull_request, applicationsets, get, my-project/*, allow
자세한 내용: RBAC 구성
참고: ArgoCD v2에서는 정책 정의가 지원되지 않습니다. 정책을 정의하려면 v3.0.0 이상으로 업그레이드하세요.