Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Lab 4: CI/CD Integration

Event driven pipeline: git events -> Argo Events -> Argo Workflows (CI) -> Argo CD (CD). Assumes labs 1-3, Argo Workflows installed, Argo CD (namespace argocd) for the last step.

push/PR -> EventSource -> EventBus -> Sensor -> Workflow (build/test)
main push + approval   -> Sensor -> Workflow (deploy) / Argo CD Application

EventSources

The real github EventSource registers a webhook on GitHub through the API, so it needs a token and a URL GitHub can reach.

spec:
  github:
    myrepo:
      repositories:
        - owner: myorg
          names: [myapp]
      webhook:
        endpoint: /github
        port: "12000"
        method: POST
        url: https://events.example.com     # public URL
      events: [push, pull_request]
      apiToken:
        name: github-access
        key: token
      webhookSecret:
        name: github-access
        key: secret
      contentType: json
      active: true
  • Event name is the key (myrepo), payload is the GitHub payload under body

For the lab, fake GitHub with plain webhooks (same body.* paths as the real payload), plus one for the manual approval.

apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
  name: github-sim
  namespace: argo-events
spec:
  service:
    ports:
      - port: 12000
        targetPort: 12000
  webhook:
    push: {port: "12000", endpoint: /push, method: POST}
    pull-request: {port: "12000", endpoint: /pr, method: POST}
---
apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
  name: approval
  namespace: argo-events
spec:
  service:
    ports:
      - port: 13000
        targetPort: 13000
  webhook:
    approval: {port: "13000", endpoint: /approval, method: POST}
kubectl port-forward -n argo-events svc/github-sim-eventsource-svc 12000:12000 &
kubectl port-forward -n argo-events svc/approval-eventsource-svc 13000:13000 &

CI WorkflowTemplate

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: ci-pipeline
  namespace: argo-events
spec:
  entrypoint: main
  arguments:
    parameters:
      - name: repo
      - name: branch
      - name: commit
  templates:
    - name: main
      dag:
        tasks:
          - {name: lint, template: step, arguments: {parameters: [{name: msg, value: "lint {{workflow.parameters.repo}}"}]}}
          - {name: test, template: step, arguments: {parameters: [{name: msg, value: "test {{workflow.parameters.commit}}"}]}}
          - name: build
            template: step
            dependencies: [lint, test]
            arguments:
              parameters: [{name: msg, value: "build {{workflow.parameters.repo}}:{{workflow.parameters.commit}}"}]
    - name: step
      inputs:
        parameters:
          - name: msg
      container:
        image: alpine:3
        command: [sh, -c, "echo {{inputs.parameters.msg}}; sleep 2"]
  • Stand-in steps, swap in real checkout/test/build (kaniko etc.) later

Build sensor (push to main/develop)

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: build
  namespace: argo-events
spec:
  template:
    serviceAccountName: operate-workflow-sa
  dependencies:
    - name: push
      eventSourceName: github-sim
      eventName: push
      filters:
        data:
          - path: body.ref
            type: string
            value: ["refs/heads/main", "refs/heads/develop"]
  triggers:
    - template:
        name: ci
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: ci-
              spec:
                workflowTemplateRef:
                  name: ci-pipeline
                arguments:
                  parameters:
                    - {name: repo, value: placeholder}
                    - {name: branch, value: placeholder}
                    - {name: commit, value: placeholder}
          parameters:
            - src: {dependencyName: push, dataKey: body.repository.full_name}
              dest: spec.arguments.parameters.0.value
            - src: {dependencyName: push, dataKey: body.ref}
              dest: spec.arguments.parameters.1.value
            - src: {dependencyName: push, dataKey: body.after}
              dest: spec.arguments.parameters.2.value
curl -X POST localhost:12000/push -H "Content-Type: application/json" \
  -d '{"ref":"refs/heads/main","after":"abc123","repository":{"full_name":"myorg/myapp","name":"myapp"}}'
argo get -n argo-events @latest
  • workflowTemplateRef keeps the pipeline definition out of the sensor
  • Real GitHub payload has the same fields: ref, after, repository.full_name, head_commit.message, pusher.name

Deploy with approval

Deploy template (stand-in):

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: deploy-pipeline
  namespace: argo-events
spec:
  entrypoint: main
  arguments:
    parameters:
      - name: environment
      - name: version
  templates:
    - name: main
      container:
        image: alpine:3
        command: [sh, -c, "echo deploying {{workflow.parameters.version}} to {{workflow.parameters.environment}}"]

Prod sensor, two dependencies, so BOTH a push to main and an approval are needed (default AND):

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: deploy-prod
  namespace: argo-events
spec:
  template:
    serviceAccountName: operate-workflow-sa
  dependencies:
    - name: main-push
      eventSourceName: github-sim
      eventName: push
      filters:
        data:
          - {path: body.ref, type: string, value: ["refs/heads/main"]}
    - name: approved
      eventSourceName: approval
      eventName: approval
      filters:
        data:
          - {path: body.approved, type: bool, value: ["true"]}
  triggers:
    - template:
        name: deploy-prod
        argoWorkflow:
          operation: submit
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: deploy-prod-
              spec:
                workflowTemplateRef:
                  name: deploy-pipeline
                arguments:
                  parameters:
                    - {name: environment, value: production}
                    - {name: version, value: placeholder}
          parameters:
            - src: {dependencyName: main-push, dataKey: body.after}
              dest: spec.arguments.parameters.1.value
  • Dev is the same sensor with a single dependency and value: ["refs/heads/develop", "refs/heads/feature/.*"] (string values are regexes), no approval
curl -X POST localhost:12000/push -H "Content-Type: application/json" \
  -d '{"ref":"refs/heads/main","after":"prod456","repository":{"full_name":"myorg/myapp","name":"myapp"}}'
kubectl logs -n argo-events -l sensor-name=deploy-prod     # waiting on approval
curl -X POST localhost:13000/approval -H "Content-Type: application/json" \
  -d '{"approved":true,"approver":"me"}'
argo list -n argo-events
  • The sensor holds the first event until the other arrives
  • Events are consumed after the trigger fires, a second approval needs a second push

Argo CD

Sensor creates an Argo CD Application for the pushed commit. Argo CD does the sync (syncPolicy.automated), the CD part is not a workflow.

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: manage-applications
  namespace: argocd
rules:
  - apiGroups: [argoproj.io]
    resources: [applications]
    verbs: [create, get, list, update, patch, delete]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: manage-applications
  namespace: argocd
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: manage-applications
subjects:
  - kind: ServiceAccount
    name: operate-workflow-sa
    namespace: argo-events
  triggers:
    - template:
        name: argocd-app
        k8s:
          operation: create     # use patch/update to change an existing app
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Application
              metadata:
                name: placeholder
                namespace: argocd
              spec:
                project: default
                source:
                  repoURL: placeholder
                  targetRevision: placeholder
                  path: k8s
                destination:
                  server: https://kubernetes.default.svc
                  namespace: default
                syncPolicy:
                  automated: {prune: true, selfHeal: true}
          parameters:
            - src: {dependencyName: main-push, dataKey: body.repository.name}
              dest: metadata.name
            - src: {dependencyName: main-push, dataTemplate: "https://github.com/{{ .Input.body.repository.full_name }}"}
              dest: spec.source.repoURL
            - src: {dependencyName: main-push, dataKey: body.after}
              dest: spec.source.targetRevision
  • Common pattern: CI workflow builds + pushes image and commits the new tag to the GitOps repo, Argo CD notices the commit. Events -> Workflows -> CD

PR checks

Same shape as the build sensor, on the pull-request event:

  dependencies:
    - name: pr
      eventSourceName: github-sim
      eventName: pull-request
      filters:
        data:
          - {path: body.action, type: string, value: ["opened", "synchronize"]}
  # parameters: body.pull_request.number, body.pull_request.head.ref, body.pull_request.user.login
curl -X POST localhost:12000/pr -H "Content-Type: application/json" \
  -d '{"action":"opened","pull_request":{"number":42,"head":{"ref":"feature/x"},"user":{"login":"me"}}}'

Notes

  • Production: validate webhook secrets, keep tokens in Secrets, narrow RBAC for the sensor SA, filter on branch/event type
  • Check each hop when debugging: EventSource log -> sensor log (filters/dependencies) -> workflow

Cleanup

kill %1 %2     # port-forwards
kubectl delete sensor,eventsource --all -n argo-events
kubectl delete workflows,workflowtemplates --all -n argo-events
kubectl delete role,rolebinding manage-applications -n argocd
kubectl delete application myapp -n argocd --ignore-not-found