We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.

Deploying with GitHub Actions and ArgoCD: simplifying the process

Hey DevOps! In this post we'll set everything up so you can build a build-and-deploy pipeline for your app using GitHub Actions and ArgoCD. If you already know GitHub and ArgoCD, even better — you'll move faster.

CloudScript Technology
November 5, 20243 min read
Deploying with GitHub Actions and ArgoCD: simplifying the process

Hey, DevOps! All good?

In this post, we’re going to set everything up so you can spin up a build-and-deploy pipeline for your application using GitHub Actions and ArgoCD. If you already know GitHub and ArgoCD, even better — the idea here is to go straight to the point.

Let’s get to it.

Step 1: Get organized with a folder for your Kubernetes objects

Let’s create a folder named deploy with two files:

$ cd /path/to/your/repository
$ mkdir deploy

The final structure will look like this:

deploy/
├── deployment.yaml
└── service.yaml

Create the file deployment.yaml inside the deploy folder and add the content below:

$ cd deploy
$ touch deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: minha-api
  labels:
    app: minha-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: minha-api
  template:
    metadata:
      labels:
        app: minha-api
    spec:
      containers:
        - name: minha-api-container
          image: usuario/minha-api:latest # your image address in the registry goes here
          ports:
            - containerPort: 8080
          env:
            - name: NODE_ENV
              value: "production"
            - name: PORT
              value: "8080"
          resources:
            requests:
              memory: "64Mi"
              cpu: "250m"
            limits:
              memory: "128Mi"
              cpu: "500m"

deploy/deployment.yaml

File deploy/service.yaml

$ cd deploy
$ touch service.yaml
apiVersion: v1
kind: Service
metadata:
  name: minha-api-service
  labels:
    app: minha-api
spec:
  selector:
    app: minha-api
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP

deploy/service.yaml

Step 2: Prepare the Dockerfile to run everything in a container

Create one more file in your repository named "Dockerfile". Every application might need a specific Dockerfile, but here’s an example that works well for a Golang application:

$ cd /path/to/your/repository
$ touch Dockerfile
FROM golang:1.23.1-alpine AS builder
WORKDIR /api
COPY . .
RUN go mod tidy
RUN go build -o main cmd/main.go


FROM alpine:3.19.1 AS release
RUN apk --no-cache add ca-certificates
WORKDIR /api
COPY --from=builder /api/main .
RUN chmod +x main
EXPOSE 8080
CMD ["./main"]

Dockerfile

Step 3: Set up the Action for build and deploy

Now the final touch: configuring the GitHub Action. With it, every time you push, the pipeline will handle building and deploying the application. In this example we’ll use Kaniko to build and push the image to your image registry. Create a .github/workflows/deploy.yml file with the content below:

$ cd /path/to/your/repository
$ mkdir .github/workflows
$ cd .github/workflows/
$ touch deploy.yml
name: build and deploy

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build and push Docker image with Kaniko
        uses: gcr.io/kaniko-project/executor:latest
        env:
          DOCKER_CONFIG: /kaniko/.docker/
          DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
        with:
          args: |
            --context .
            --dockerfile ./Dockerfile
            --destination usuario/minha-api:v1.0.0

  deploy:
    runs-on: ubuntu-latest
    needs: build

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up ArgoCD CLI
        run: |
          curl -sSL -o argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
          chmod +x argocd
          sudo mv argocd /usr/local/bin/

      - name: Deploy to Kubernetes using ArgoCD
        run: |
          argocd app create my-app \
              --server my-argocd.my-domain.com.br \
              --auth-token ${{ secrets.ARGOCD_TOKEN }} \
              --repo https://github.com/usuario/repo \
              --path deploy/ \
              --dest-server https://kubernetes.default.svc \
              --grpc-web \
              --http-retry-max 3 \
              --revision main \
              --upsert \
              --dest-namespace default

          argocd app sync my-app \
              --grpc-web \
              --server my-argocd.my-domain.com.br \
              --force \
              --retry-limit 3 \
              --timeout "300" \
              --auth-token ${{ secrets.ARGOCD_TOKEN }}

.github/workflows/deploy.yml

Conclusion

That’s it! Just commit this code to your Github and the pipeline should kick off. This workflow checks out the code, installs dependencies, runs tests, builds the application and the Docker image, and finishes by deploying with ArgoCD. Now you can sit back and let the pipeline do the heavy lifting.

Need more complex pipeline flows? We at Cloudscript are here to help. Feel free to get in touch with us.

Cheers,

Jonathan Schmitt.

Stay up to date

Get our articles on DevOps, Kubernetes, Platform Engineering and Cloud Native delivered to your inbox.

No spam. Unsubscribe anytime.