Skip to main content

Review Argo CD

Deploys a review environment as an Argo CD Application and removes it when the GitLab review environment stops.

The component creates or updates an Argo CD Application from the manifest input after expanding CI variables such as $CI_COMMIT_SHA. It looks up the Application in the Argo CD project declared by the manifest (default: default). The token must be able to read that project’s Applications. Creation, sync and removal use the argocd CLI.

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/helm@$CI_COMMIT_SHA
inputs:
name: my-app
path: chart
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/review-argocd@$CI_COMMIT_SHA
inputs:
server: argocd.example.com
token-variable: ARGOCD_AUTH_TOKEN
environment: int
needs:
- job: build:helm:my-app
artifacts: true
manifest: |
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app-$CI_COMMIT_SHORT_SHA
namespace: argocd
spec:
project: default
sources:
- repoURL: oci://$CI_REGISTRY_IMAGE/charts
chart: my-app
targetRevision: $CI_HELM_RELEASE_TAG
destination:
name: "cluster-001"
namespace: review-my-app
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=false
url-script: scripts/review-argocd-url.sh

Set server or the ARGOCD_SERVER CI/CD variable to the Argo CD API server hostname. Generate an Argo CD API token and store it in the variable named by token-variable, which defaults to ARGOCD_AUTH_TOKEN. Set insecure: true for internal or self-signed Argo CD API servers where certificate validation is intentionally skipped.

For Helm OCI chart sources, the component accepts Helm-style oci:// repository URLs and normalizes them to the registry-style repoURL that Argo CD expects in an Application.

When the chart is built in the same pipeline, keep the start job on when: on_success so a failed publisher cannot trigger a deployment of an unpublished chart. Override only the start job's rules; keep the stop job running after failed or partial starts:

review-argocd:start:
rules:
- when: on_success

Stop accepts both NotFound and PermissionDenied: Argo CD can return either response for an absent Application. Authentication, connection, and other API failures still fail the stop job. A suppressed PermissionDenied is not confirmation that an existing Application was deleted.

If url-script exists and is executable, it must print the review URL. Otherwise, the component uses the first URL reported in the Argo CD Application summary.

Set wait-enabled: false only for smoke tests or clusters where another validation step proves the Application reconciled. Production review environments should keep the default so the start job waits for Argo CD to report Synced and Healthy.

Argo CD account​

Create a dedicated Argo CD local account for GitLab CI and allow it to read, sync, and delete the review Applications.

apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cm
namespace: argocd
data:
accounts.gitlab-review: apiKey
---
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-rbac-cm
namespace: argocd
data:
policy.csv: |
p, role:gitlab-review, applications, get, default/my-app-*, allow
p, role:gitlab-review, applications, create, default/my-app-*, allow
p, role:gitlab-review, applications, update, default/my-app-*, allow
p, role:gitlab-review, applications, sync, default/my-app-*, allow
p, role:gitlab-review, applications, delete, default/my-app-*, allow
g, gitlab-review, role:gitlab-review

Claim a token​

  • Login to the application controller
kubectl exec -it pod/argocd-application-controller-0 -n argocd -- bash
  • Connect to the Argo CD server
argocd login --core
  • Generate a token
argocd account generate-token --account gitlab-review
  • Store the token as the masked CI/CD variable ARGOCD_AUTH_TOKEN:

Recover an abandoned Application on the next start​

Ownership checks run automatically. To reclaim an old Application on the next start, use a stable Application name and a stable GitLab environment name (the start and stop resource group). Use separate slots for different merge requests and for the default branch. All writers for the Application must use that same lock. The Argo CD token must be able to read the exact Application and distinguish a missing Application from denied access.

The component records the owner on the Application. Before replacing it, start checks that the previous pipeline was canceled or failed and has no live jobs or downstream triggers. It checks the Application identity again before cascading deletion. Stop refuses to delete an Application now owned by a replacement. The destination namespace itself is never deleted. Existing, unmarked Applications are not reclaimed. If no replacement starts, this mode does not provide independent cleanup.

Inputs​

NameDescriptionDefaultType
------------
environmentGitLab environment prefix.intstring
environment-identifierGitLab environment suffix like `/$CI_PIPELINE_IID-$CI_COMMIT_SHORT_SHA`. CI variables are expanded before use.string
insecureSkip Argo CD server TLS certificate verification.falseboolean
manifestArgo CD Application manifest. Variables are expanded before applying.apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: "$CI_PROJECT_PATH_SLUG-$CI_PIPELINE_IID-$CI_COMMIT_SHORT_SHA" namespace: argocd labels: app.kubernetes.io/managed-by: gitlab-ci app.kubernetes.io/part-of: "$CI_PROJECT_PATH_SLUG" spec: project: default sources: - repoURL: "$CI_PROJECT_URL.git" targetRevision: "$CI_COMMIT_SHA" path: chart destination: server: "https://kubernetes.default.svc" namespace: "$CI_ENVIRONMENT_SLUG" syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=false string
nameThe name prefix for review start and stop jobs.review-argocdstring
needsThe jobs that the start job depends on.[]array
needs-stopThe jobs that the stop job depends on.[]array
rulesThe rules to determine when to run the review process.[{"if":"$CI_PIPELINE_SOURCE == \"schedule\"","when":"never"},{"if":"$CI_COMMIT_TAG =~ $CI_PATTERN_SEMVER","when":"never"},{"when":"always"}]array
serverArgo CD API server. Defaults to ARGOCD_SERVER when unset.string
stageThe pipeline stage for start and stop jobs.teststring
timeoutTimeout while waiting for the Argo CD Application to sync and become healthy.15mstring
token-variableEnvironment variable that contains an Argo CD auth token.ARGOCD_AUTH_TOKENstring
url-scriptOptional executable script that prints the review URL after sync.scripts/review-argocd-url.shstring
wait-enabledWait for the Argo CD Application to sync and become healthy.trueboolean