How to Use Travis CI to Deploy Applications via ArgoCD in a Kubernetes Environment?

I’m trying to set up a CI/CD pipeline where Travis CI handles the CI part (building and pushing Docker images) and ArgoCD takes care of the CD (deploying the application to Kubernetes). Here’s my situation:

I have a Dockerized application that I build and push to an AWS ECR repository using Travis CI. The Kubernetes manifests for the application are stored in a GitHub repository, which is already being tracked by ArgoCD. My goal is to automate the deployment process so that every time a new Docker image is built and pushed, ArgoCD triggers a sync to deploy the updated image to the Kubernetes cluster.

The challenges I’m facing include:

  1. Updating the image tag in the Kubernetes manifests automatically after Travis CI builds the image.
  2. Triggering ArgoCD to sync the changes without manual intervention.
  3. Ensuring the process works seamlessly across multiple clusters.

I’ve come across tools like argocd app sync and kustomize, but I’m unsure how to tie these together with Travis CI. Can someone provide a clear explanation or example of how to achieve this?

Hi @Mearsheimer,

Use kustomize or a tool like sed to dynamically update the image tag in your Kubernetes manifests during the Travis CI build process. For example:

images:
  - name: your-app-image
    newTag: <dynamic-tag>

In your .travis.yml file, you can update the newTag with the Travis CI build number or commit hash. After updating the manifest files, commit the changes back to the GitHub repository tracked by ArgoCD. Use a script like this in your Travis CI pipeline:

git config --global user.email "travis@ci.org"
git config --global user.name "Travis CI"
git add .
git commit -m "Update image tag to $IMAGE_TAG"
git push origin main

After the updated manifests are pushed to the Git repository, you can trigger ArgoCD to sync the changes. You have two options, option one would be automated Sync, ensure ArgoCD is configured for auto-sync. It will detect changes in the repository and automatically deploy them. Option two would be manual trigger via the CLI, use the ArgoCD CLI to trigger a sync from your Travis CI pipeline. Add this command to your .travis.yml:

argocd app sync your-app-name --auth-token $ARGOCD_AUTH_TOKEN --server $ARGOCD_SERVE`

Make sure to store ARGOCD_AUTH_TOKEN and ARGOCD_SERVER as environment variables in Travis CI.

If you’re deploying to multiple clusters, you can use a cluster-specific configuration in ArgoCD or separate applications for each cluster. Automate the updates and sync process for all clusters using the same steps. This is an .travis.yml I put together for you (not to follow step by step, but more as a scaffolding):

language: bash
services:
  - docker

env:
  global:
    - IMAGE_TAG=$TRAVIS_COMMIT
    - KUBE_MANIFESTS_REPO=https://github.com/your-org/kube-manifests

script:
  - docker build -t your-ecr-repo:latest .
  - docker tag your-ecr-repo:latest your-ecr-repo:$IMAGE_TAG
  - docker push your-ecr-repo:$IMAGE_TAG

after_success:
  - git clone $KUBE_MANIFESTS_REPO
  - cd kube-manifests
  - sed -i "s|your-app-image:.*|your-app-image:$IMAGE_TAG|g" deployment.yaml
  - git add deployment.yaml
  - git commit -m "Update image tag to $IMAGE_TAG"
  - git push origin main
  - argocd app sync your-app-name --auth-token $ARGOCD_AUTH_TOKEN --server $ARGOCD_SERVER

This above setup in theory ensures that Travis CI builds and pushes the Docker image, updates the manifests, and triggers ArgoCD to deploy the changes automatically.

1 Like

This did it,

Thanks Montana.

Great,

Glad I could help you solve the problem.