How to provision dynamic review environments using merge requests and Argo CD

Madou Coulibaly and Joe Randazzo ·
Aug 2, 2022 · 6 min read

We recently learned of a new contribution to the ApplicationSet in the Argo CD project, specifically the Pull Request generator for GitLab and decided to take it for a spin. What makes this interesting is now dynamic review environments can be provisioned intuitively from the merge request (MR) using a GitOps workflow. The benefit is code reviewers or designers can quickly review any app changes to your Kubernetes cluster all from within the merge request.

In traditional testing workflows, you may have pushed your changes into a development environment, waiting for the QA and UX team to pull those changes into their environment for further review, and then received feedback based on your small change. At this point, time was wasted between various teams with environment coordination or adding bugs to the backlog of the new changes.

With the combination of a merge request and review environments, you can quickly spin up a test environment based on the changes of your feature branch. This means the QA or UX team can suggest improvements or changes during the code review process without wasting cycles.

The introduction of the ApplicationSet has given greater flexibility to Argo CD workflows such as:

Let's review the ApplicationSet and the GitLab Pull Request Generator

The Pull Request Generator will use the GitLab API to automatically discover new merge requests within a repository. Depending on the filter match of the MR, a review environment will then be generated.

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: review-the-application
  namespace: argocd
spec:
  generators:
  - pullRequest:
      gitlab:
        project: <project-id>
        api: https://gitlab.com/
        tokenRef:
          secretName: <gitlab-token>
          key: token
        pullRequestState: opened
      requeueAfterSeconds: 60
  template:
    metadata:
      name: 'review-the-application-{{number}}'
    spec:
      source:
        repoURL: <repository-with-manifest-files>
        path: chart/
        targetRevision: 'HEAD'
        helm:
          parameters:
          - name: "image.repository"
            value: "registry.gitlab.com/<group-and-project-path>/{{branch}}"
          - name: "image.tag"
            value: "{{head_sha}}"
          - name: "service.url"
            value: "the-application-{{number}}.<ip>.nip.io"
      project: default
      destination:
        server: https://kubernetes.default.svc
        namespace: dynamic-environments-with-argo-cd

Fields

Filter options include GitLab labels, merge request state (open, closed, merged), and branch match. Templating options include merge request ID, branch name, branch slug, head sha, and head short sha.

See the latest ApplicationSet documentation for additional details.

For this blog post, we explore using the Argo CD ApplicationSet to provision a “ReviewOps” environment based on merge request changes.

Prerequisites

The following tools are required for running this tutorial. Please install and/or configure them before getting started.

Explore the Source Code

First, let’s explore the source code for the tutorial.

This GitLab group is composed of the 2 following projects:

git-repository

Setting up GitLab

  1. Create your GitLab Group and fork the The Application and The Application Configuration projects into it.

  2. In The Application Configuration project, edit the **manifests/applicationset.yml** as follows:

Note: keep the {{branch}} string as is and replace with the name of the group you created in step 1.

Note: keep the {{number}} string as is and replace with the base domain of your Kubernetes Cluster.

  1. Define the following CI/CD variables at the group level:

    • ARGOCD_SERVER_URL, the Argo CD server address
    • ARGOCD_USERNAME, the username of your Argo CD account
    • ARGOCD_PASSWORD, the password of your Argo CD account
    • KUBE_INGRESS_BASE_DOMAIN, the base domain of your Kubernetes Cluster

    cicd-variables

  2. Generate a Group access token to grant read_api and read_registry access to this group and its sub-projects.

    group-access-token

    Save the group access token somewhere safe. We will use it later.

Setting up Kubernetes

  1. Create a namespace called dynamic-environments-with-argo-cd.
    kubectl create namespace dynamic-environments-with-argo-cd
    
  2. Create a Kubernetes secret called gitlab-token-dewac to allow Argo CD to use the GitLab API.
    kubectl create secret generic gitlab-token-dewac -n argocd --from-literal=token=<Your_Access_Token>
    
  3. Create another Kubernetes secret called gitlab-token-dewac to allow Kubernetes to pull images from the GitLab Container Registry.
    kubectl create secret generic gitlab-token-dewac -n dynamic-environments-with-argo-cd --from-literal=token=<Your_Access_Token>
    

Setting up Argo CD

  1. Create the Argo CD ApplicationSet to generate an Argo CD Application associated with a merge request.
    kubectl apply -f https://gitlab.com/<Your_GitLab_Group>/the-application-configuration/-/raw/main/manifests/applicationset.yaml
    

Update the source code

  1. In The Application project, create a GitLab issue, then an associated branch and merge request.
  2. In Argo CD, a new application is provisioned called review-the-application based on the new merge request event.

    review-the-application-argocd

  3. In The Application project, edit the index.pug and replace p Welcome to #{title} with p Bienvenue à #{title}.
  4. Commit into your recent branch which is going to trigger a pipeline run.
  5. In the CI/CD > Pipelines, you will find the following pipeline running on your merge request:

    feature-branch-pipeline

    where,

    • docker-build: builds the container image
    • reviewops: configures and deploys the container into the review environment using Argo CD
    • stop-reviewops: deletes the review environment
  6. Once completed, the review-the-application application in Argo CD is now synced.

    review-the-application-synced

  7. From the merge request, click on the View app button to access to your application.

    view-app-button

    The outcome should be as follows:

    express-app

  8. You have succesfully provisioned a dynamic review environment based on your merge request! Once the merge request is closed, the environment will be automatically cleaned up.

To sum up

Hopefully this tutorial has been helpful and has inspired your GitLab + Argo CD workflows with review environments.

We'd love to hear in the comments on how this is working for you, as well as your ideas on how we can make GitLab a better place for GitOps workflows.

“Create a review environment based on an MR change with ArgoCD + @gitlab” – Madou Coulibaly and Joe Randazzo

Click to tweet

Edit this page View source