Skip to content
Docs

GitLab CI - ArgoCD authentication

Create an ArgoCD JWT token and use it in GitLab CI to deploy applications.

Updated
On this page

This guide creates a JWT token in ArgoCD and uses it in a GitLab CI pipeline to refresh, sync and wait for an application in ArgoCD.

Every Zone gets its own ArgoCD project with the roles maintainer, observer, contributor and cicd. The cicd role is meant for CI/CD pipelines, so the token you create for it lets the pipeline deploy the Zone’s applications without using a personal account.

For more information about authentication in ArgoCD, see ArgoCD documentation.

  • A working Zone. Its ArgoCD project holds the cicd role.
  • Access to the Zone’s project in the ArgoCD UI, to create the token.
  • Permission to add CI/CD variables in the GitLab project.
  • Network access from the GitLab runner to ArgoCD. By default ArgoCD is served at argocd.<internal domain> through the internal load balancer, so check that a runner outside the VPC can reach it.
  • The argocd CLI in the image the deploy job runs in.

Settings -> Projects -> <project-name> -> Roles -> cicd -> Create (under JWT Tokens)

Copy the token value.

2. Store the token as a GitLab CI/CD variable

Section titled “2. Store the token as a GitLab CI/CD variable”

Settings -> CI/CD -> Variables -> Add variable

Field Value
Type Variable
Visibility Masked and hidden
Flags Protect variable
Key ARGOCD_TOKEN_<ENV>
Value <token-value>

Click Add variable.

The deploy job passes the token to the argocd CLI with --auth-token. Set one ARGOCD_HOST_<ENV> and ARGOCD_TOKEN_<ENV> pair per environment and map them to ARGOCD_HOST and ARGOCD_TOKEN in the job:

.gitlab-ci.yml
variables:
ARGOCD_HOST_DEV: argocd.dev.example.com
KUBERNETES_NAMESPACE: example-namespace
workflow:
rules:
- when: always
stages:
- requirements
- build
- deploy
requirements-variables:
stage: requirements
script: |
...
requirements-helm:
stage: requirements
script: |
...
requirements-docker:
stage: requirements
script: |
...
build-image:
stage: build
script: |
...
build-chart:
stage: build
script: |
...
dev:
stage: deploy
variables:
ARGOCD_TOKEN: ${ARGOCD_TOKEN_DEV}
ARGOCD_HOST: ${ARGOCD_HOST_DEV}
extends: .deploy
needs:
- job: requirements-variables
- job: build-image
optional: true
- job: build-chart
.deploy:
script: |
...
APP_NAME=example-app
argocd --server "${ARGOCD_HOST}" --auth-token="${ARGOCD_TOKEN}" app get --refresh ${KUBERNETES_NAMESPACE}/$APP_NAME
argocd --server "${ARGOCD_HOST}" --auth-token="${ARGOCD_TOKEN}" app diff --exit-code=false ${KUBERNETES_NAMESPACE}/$APP_NAME
argocd --server "${ARGOCD_HOST}" --auth-token="${ARGOCD_TOKEN}" app sync ${KUBERNETES_NAMESPACE}/$APP_NAME
argocd --server "${ARGOCD_HOST}" --auth-token="${ARGOCD_TOKEN}" app wait --timeout 300 --health --sync --operation ${KUBERNETES_NAMESPACE}/$APP_NAME

Run the pipeline. The dev job succeeds when argocd app wait returns, which means the application is synced and healthy. The token also appears under JWT Tokens of the cicd role in the ArgoCD project.

Symptom Cause / fix
The argocd command times out or cannot connect The runner cannot reach ArgoCD. ArgoCD is served through the internal load balancer by default; run the job on a runner with access to the VPC.
ARGOCD_TOKEN is empty in the job Protected variables are only passed to pipelines on protected branches and tags. Run the pipeline on a protected ref, or adjust the variable’s flags.