KAHIBARO Bookshelves
Discord Login Register

14.5. Integrating OpenShift with external CI tools

Integration patterns and architecture

External CI tools (Jenkins, GitHub Actions, GitLab CI, Azure DevOps, etc.) typically integrate with OpenShift through a small set of patterns:

  • Push-based deployments
    CI runs builds/tests and then pushes a new image or triggers a rollout in OpenShift:
    • Push image to a registry OpenShift can access.
    • Call OpenShift APIs (oc/REST) to update deployments or trigger rollouts.
    • Optionally update Git (GitOps) instead of talking directly to the cluster.
  • Pull-based (GitOps) deployments
    CI updates a Git repository containing Kubernetes/OpenShift manifests; a GitOps tool (e.g., Argo CD) running in OpenShift reconciles changes:
    • External CI only manages tests/builds and Git.
    • OpenShift deployment is indirectly driven by repo changes.
  • Hybrid
    CI might:
    • Build and push images.
    • Update application configuration in Git.
    • Optionally trigger a PipelineRun for OpenShift Pipelines or a GitOps sync.

When choosing a pattern consider:

  • Who “owns” deployments (platform team vs app team).
  • Network/connectivity (can the CI reach the cluster API?).
  • Security and compliance (API tokens, RBAC, auditability).
  • Desired level of Git-centric workflow.

Accessing OpenShift from external CI

Authentication options

External CI needs credentials to call the OpenShift API or run oc:

  • User tokens
    • Use personal access tokens (not ideal for automation).
    • Rotating and revoking is harder for shared automation.
  • Service accounts (recommended)
    • Create a service account in the target project/namespace:
    oc create sa ci-deployer -n my-app
  • Assign appropriate roles (e.g., edit, or a custom role with minimal permissions):
    oc adm policy add-role-to-user edit -z ci-deployer -n my-app
  • Get the token (or use OAuth tokens in modern clusters where secret-based tokens are disabled; for CI use oc login --token=...):
    oc sa get-token ci-deployer -n my-app
  • Store the token and API URL in CI secrets.
  • OIDC / SSO integration
    • Some CI platforms can authenticate via OpenShift’s OAuth/OIDC provider directly.
    • Useful in tightly integrated enterprise setups; usually requires cluster and IdP configuration by platform admins.

Network and security considerations

  • Ensure CI runners can reach:
    • The OpenShift API endpoint.
    • The image registry (internal or external) used by the cluster.
  • Enforce least privilege:
    • Use namespace-scoped service accounts.
    • Avoid cluster-admin and broad roles for CI.
  • Manage secrets:
    • Use CI’s secret store for tokens and registry credentials.
    • Use short-lived tokens or automated rotation where possible.

Using `oc` in external CI pipelines

Many integrations rely on oc rather than direct REST calls.

Installing and configuring `oc` in CI

  • Containerized approach (preferred)
    • Use an oc-based image as the CI job executor, e.g.:
      • Red Hat openshift-cli image
      • Custom image: FROM registry.redhat.io/openshift4/ose-cli
    • Example GitLab CI job:
    deploy:
      image: registry.redhat.io/openshift4/ose-cli
      stage: deploy
      script:
        - oc login "$OCP_API_URL" --token="$OCP_TOKEN" --insecure-skip-tls-verify=false
        - oc project my-app
        - oc rollout restart deployment/my-app
  • Binary install
    • Download oc in the job script:
    curl -L "$OC_DOWNLOAD_URL" | tar -xz
    sudo mv oc /usr/local/bin
  • Less reproducible than containerized approach.

Common deployment actions from CI

Once authenticated, typical CI actions include:

  • Switch project:
  oc project my-app
  • Update image of a deployment:
  oc set image deployment/my-app \
    my-app-container=quay.io/myorg/my-app:${CI_COMMIT_SHA}
  • Apply a manifest bundle:
  oc apply -f k8s/
  • Trigger a rollout:
  oc rollout restart deployment/my-app
  • Wait for rollout:
  oc rollout status deployment/my-app --timeout=120s

Image registry integration patterns

External CI builds, OpenShift only deploys

Typical for GitHub Actions, GitLab CI, Jenkins:

  1. CI builds image (Docker/Buildah/kaniko).
  2. CI pushes to registry (Quay.io, Docker Hub, GitLab registry, etc.).
  3. OpenShift pulls the image for deployment.

Key points:

  • Configure ImagePullSecrets in OpenShift to pull from the external registry:
  oc create secret docker-registry external-regcred \
    --docker-server=registry.example.com \
    --docker-username="$REG_USER" \
    --docker-password="$REG_PASS" \
    --docker-email=dev@example.com \
    -n my-app
  oc secrets link default external-regcred --for=pull -n my-app
  • CI must tag images with a stable tag or digest used by deployments:
    • e.g. my-app:main, my-app:${CI_COMMIT_SHA}.

Using OpenShift internal registry from external CI

  • External CI can push directly to the in-cluster registry:
    • Get registry route/URL (varies by OpenShift version).
    • Create a service account with registry-editor or appropriate role.
    • Generate a token and configure it as registry credentials in CI.
  • Useful when images must remain internal to the cluster/network.

Jenkins and OpenShift

Jenkins is a common external CI tool in OpenShift environments.

Integration models

  • Jenkins outside OpenShift, deploying into OpenShift
    • Jenkins agents or controllers run outside the cluster.
    • Use oc CLI and service account tokens.
    • Jenkins credentials store holds:
      • OpenShift API URL
      • Service account token
      • Registry credentials (if needed)
  • Jenkins inside OpenShift (Jenkins as a deployment)
    • Jenkins runs as a pod in the cluster (closer to “internal” than truly external, but often managed as separate CI).
    • Automatically has access to the internal registry and API (via service accounts).
    • Build agents can be ephemeral pods.

Example Jenkins pipeline snippet

pipeline {
  agent any
  environment {
    OCP_API_URL = 'https://api.my-cluster.example.com:6443'
    OCP_TOKEN   = credentials('ocp-ci-token')  // Jenkins credential ID
    OCP_PROJECT = 'my-app'
  }
  stages {
    stage('Login to OpenShift') {
      steps {
        sh """
          oc login ${OCP_API_URL} \
            --token=${OCP_TOKEN} \
            --insecure-skip-tls-verify=false
          oc project ${OCP_PROJECT}
        """
      }
    }
    stage('Deploy') {
      steps {
        sh """
          oc set image deployment/my-app \
            my-app-container=quay.io/myorg/my-app:${GIT_COMMIT}
          oc rollout status deployment/my-app --timeout=120s
        """
      }
    }
  }
}

GitHub Actions and OpenShift

GitHub Actions commonly integrates using oc plus an external registry.

Typical workflow

  1. Build and push the image:
   - name: Build and push image
     uses: docker/build-push-action@v6
     with:
       context: .
       push: true
       tags: quay.io/myorg/my-app:${{ github.sha }}
  1. Deploy to OpenShift using oc:
   - name: Install oc
     run: |
       curl -L "$OC_DOWNLOAD_URL" | tar -xz
       sudo mv oc /usr/local/bin
   - name: Login to OpenShift
     run: |
       oc login ${{ secrets.OCP_API_URL }} \
         --token=${{ secrets.OCP_TOKEN }} \
         --insecure-skip-tls-verify=false
       oc project my-app
   - name: Update deployment
     run: |
       oc set image deployment/my-app \
         my-app-container=quay.io/myorg/my-app:${{ github.sha }}
       oc rollout status deployment/my-app --timeout=120s

GitHub Environments and approvals

  • Use GitHub Environments (e.g. dev, staging, prod) to:
    • Store environment-specific OpenShift credentials.
    • Require manual approvals before deploying to sensitive clusters.
  • Each environment can point to different OpenShift clusters or namespaces.

GitLab CI and OpenShift

GitLab CI integrates similarly, often with GitLab’s container registry.

Example `.gitlab-ci.yml` snippet

stages:
  - build
  - deploy
variables:
  IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
build:
  stage: build
  image: docker:24
  services:
    - docker:24-dind
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
    - docker build -t "$IMAGE_TAG" .
    - docker push "$IMAGE_TAG"
deploy:
  stage: deploy
  image: registry.redhat.io/openshift4/ose-cli
  environment:
    name: production
  script:
    - oc login "$OCP_API_URL" --token="$OCP_TOKEN" --insecure-skip-tls-verify=false
    - oc project my-app
    - oc set image deployment/my-app my-app-container="$IMAGE_TAG"
    - oc rollout status deployment/my-app --timeout=120s
  only:
    - main
  • Credentials:
    • OCP_API_URL and OCP_TOKEN stored as GitLab CI variables (protected/masked).
    • Registry credentials provided automatically when using GitLab Container Registry.

Azure DevOps, CircleCI, and other CI systems

The same building blocks apply across CI vendors:

  • Install or use an image with oc.
  • Authenticate with a service account token.
  • Apply manifests or update images.

Example Azure DevOps outline

  • Use a container job:
  container: registry.redhat.io/openshift4/ose-cli
  • Steps:
    • bash: oc login $(OCP_API_URL) --token=$(OCP_TOKEN)
    • bash: oc apply -f k8s/

Example CircleCI snippet

version: 2.1
jobs:
  deploy:
    docker:
      - image: registry.redhat.io/openshift4/ose-cli
    steps:
      - checkout
      - run:
          name: Login to OpenShift
          command: |
            oc login "$OCP_API_URL" --token="$OCP_TOKEN"
      - run:
          name: Deploy
          command: |
            oc apply -f k8s/
            oc rollout status deployment/my-app --timeout=120s
workflows:
  deploy_workflow:
    jobs:
      - deploy

Integrating external CI with OpenShift Pipelines and GitOps

External CI doesn’t have to drive deployments directly; it can instead trigger OpenShift-native deployment mechanisms.

Triggering OpenShift Pipelines (Tekton) from external CI

Possible patterns:

  • CI sends a PipelineRun to OpenShift:
  oc apply -f pipelinerun.yaml
  • CI calls the Kubernetes API directly with a Tekton PipelineRun manifest.
  • CI stores artifacts or build results; Tekton pipelines handle deployment and cluster-side tasks.

Advantages:

  • Keeps cluster-specific logic inside the cluster.
  • External CI focuses on SCM integration and testing.
  • Aligns with platform team ownership of deployment pipelines.

External CI with GitOps (Argo CD, etc.)

Typical GitOps integration pattern:

  1. External CI:
    • Builds and pushes image.
    • Updates image tag or manifest in a Git repo (e.g., update kustomization.yaml or Helm values.yaml).
    • Commits and pushes changes.
  2. Argo CD (running in OpenShift):
    • Detects Git changes.
    • Syncs manifests to the cluster.

Example: update image tag in a GitOps repo from CI:

- name: Update image tag
  run: |
    sed -i "s|image: .*$|image: quay.io/myorg/my-app:${GIT_COMMIT}|" k8s/deployment.yaml
    git config user.email "ci@example.com"
    git config user.name "CI Bot"
    git commit -am "Update image to ${GIT_COMMIT}"
    git push origin main

This pattern:

  • Avoids direct oc access from CI to production.
  • Centralizes deployments and drift management in Argo CD.

Designing robust OpenShift–CI workflows

When stitching external CI to OpenShift, consider:

  • Environments and promotion
    • Separate namespaces or clusters for dev/stage/prod.
    • CI jobs per environment, or GitOps-based promotion.
  • Rollback strategies
    • Keep previous image tags.
    • Use oc rollout undo for Kubernetes-style deployments.
    • Git-based rollback (GitOps) for configuration.
  • Validation gates
    • Health checks and smoke tests executed from CI after deployment:
    curl -f https://my-app.example.com/healthz
  • Policy checks (image scanning, manifest validation) before applying changes.
  • Observability hooks
    • CI can annotate deployments with build metadata:
    oc annotate deployment/my-app \
      ci.build-url="$CI_JOB_URL" \
      ci.commit="$CI_COMMIT_SHA" \
      --overwrite
  • Helps trace from cluster objects back to CI runs.

Common pitfalls and mitigation

  • Over-privileged CI service accounts
    • Mitigation: create dedicated namespace-scoped SAs, minimal RBAC.
  • Long-lived tokens without rotation
    • Mitigation: use credential management, shorter-lived tokens, regular rotation.
  • CI unable to reach OpenShift API
    • Mitigation: adjust network/firewall rules, private runners in same network/VPN, or prefer GitOps where only Git is needed.
  • Non-reproducible deployment scripts
    • Mitigation: version deployment manifests and scripts in Git; avoid ad-hoc oc commands scattered across jobs.
  • Tight coupling between CI and cluster details
    • Mitigation: encapsulate cluster-specific logic in templates, Tekton pipelines, or GitOps repos; CI passes parameters, not raw cluster commands.

By combining these patterns, you can integrate nearly any external CI system with OpenShift in a secure, maintainable, and auditable way, while still leveraging OpenShift’s native deployment and platform capabilities.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Course Overview
    • ☰ 1.1. What is OpenShift
      • ☰ 1.2. Why OpenShift is used in modern IT and cloud environments
        • ☰ 1.3. OpenShift vs Kubernetes
          • ☰ 1.4. Typical OpenShift use cases
            • ☰ 1.5. Structure and goals of the course
            • ☰ 2. Containers and Cloud-Native Fundamentals
              • ☰ 2.1. What is a container
                • ☰ 2.2. Containers vs virtual machines
                  • ☰ 2.3. Introduction to Docker and container images
                    • ☰ 2.4. Container registries
                      • ☰ 2.5. Basics of cloud-native applications
                      • ☰ 3. Kubernetes Fundamentals
                        • ☰ 3.1. What is Kubernetes
                          • ☰ 3.2. Kubernetes architecture
                            • ☰ 3.3. Control plane components
                              • ☰ 3.4. Worker node components
                                • ☰ 3.5. Kubernetes objects and resources
                                  • ☰ 3.6. Declarative configuration model
                                  • ☰ 4. OpenShift Architecture
                                    • ☰ 4.1. Overview of OpenShift architecture
                                      • ☰ 4.2. Control plane in OpenShift
                                        • ☰ 4.3. Nodes and machine roles
                                          • ☰ 4.4. OpenShift Operators
                                            • ☰ 4.5. OpenShift networking model
                                              • ☰ 4.6. OpenShift storage model
                                              • ☰ 5. OpenShift Installation and Deployment Models
                                                • ☰ 5.1. OpenShift deployment options
                                                  • ☰ 5.2. Installer-Provisioned Infrastructure
                                                    • ☰ 5.3. User-Provisioned Infrastructure
                                                      • ☰ 5.4. Managed OpenShift services
                                                        • ☰ 5.5. Single-node OpenShift
                                                          • ☰ 5.6. Cluster lifecycle management
                                                          • ☰ 6. Accessing and Using OpenShift
                                                            • ☰ 6.1. OpenShift web console
                                                              • ☰ 6.2. OpenShift CLI (oc)
                                                                • ☰ 6.3. Authentication and authorization
                                                                  • ☰ 6.4. Projects and namespaces
                                                                    • ☰ 6.5. Resource quotas and limits
                                                                    • ☰ 7. Building and Deploying Applications
                                                                      • ☰ 7.1. Application deployment concepts
                                                                        • ☰ 7.2. Source-to-Image (S2I)
                                                                          • ☰ 7.3. Container-based deployments
                                                                            • ☰ 7.4. DeploymentConfigs and Deployments
                                                                              • ☰ 7.5. Rolling updates and rollbacks
                                                                              • ☰ 8. Networking in OpenShift
                                                                                • ☰ 8.1. Services and service discovery
                                                                                  • ☰ 8.2. Routes and ingress
                                                                                    • ☰ 8.3. Network policies
                                                                                      • ☰ 8.4. Load balancing
                                                                                        • ☰ 8.5. External access to applications
                                                                                        • ☰ 9. Storage in OpenShift
                                                                                          • ☰ 9.1. Persistent storage concepts
                                                                                            • ☰ 9.2. Persistent Volumes
                                                                                              • ☰ 9.3. Persistent Volume Claims
                                                                                                • ☰ 9.4. StorageClasses
                                                                                                  • ☰ 9.5. Dynamic provisioning
                                                                                                    • ☰ 9.6. Stateful applications
                                                                                                    • ☰ 10. Configuration and Secrets Management
                                                                                                      • ☰ 10.1. ConfigMaps
                                                                                                        • ☰ 10.2. Secrets
                                                                                                          • ☰ 10.3. Environment variables
                                                                                                            • ☰ 10.4. Managing sensitive data
                                                                                                              • ☰ 10.5. Best practices for configuration
                                                                                                              • ☰ 11. Scaling and High Availability
                                                                                                                • ☰ 11.1. Horizontal pod autoscaling
                                                                                                                  • ☰ 11.2. Vertical scaling
                                                                                                                    • ☰ 11.3. Self-healing and pod restarts
                                                                                                                      • ☰ 11.4. High availability concepts
                                                                                                                        • ☰ 11.5. Multi-zone and multi-region clusters
                                                                                                                        • ☰ 12. Monitoring, Logging, and Observability
                                                                                                                          • ☰ 12.1. Built-in monitoring stack
                                                                                                                            • ☰ 12.2. Metrics and alerts
                                                                                                                              • ☰ 12.3. Logging architecture
                                                                                                                                • ☰ 12.4. Distributed tracing
                                                                                                                                  • ☰ 12.5. Troubleshooting applications
                                                                                                                                  • ☰ 13. Security in OpenShift
                                                                                                                                    • ☰ 13.1. Security model overview
                                                                                                                                      • ☰ 13.2. Role-Based Access Control
                                                                                                                                        • ☰ 13.3. Security Context Constraints
                                                                                                                                          • ☰ 13.4. Image security and scanning
                                                                                                                                            • ☰ 13.5. Network security
                                                                                                                                              • ☰ 13.6. Compliance and auditing
                                                                                                                                              • ☰ 14. CI/CD and DevOps with OpenShift
                                                                                                                                                • ☰ 14.1. DevOps concepts
                                                                                                                                                  • ☰ 14.2. CI/CD pipelines overview
                                                                                                                                                    • ☰ 14.3. OpenShift Pipelines (Tekton)
                                                                                                                                                      • ☰ 14.4. GitOps with Argo CD
                                                                                                                                                        • ☰ 14.5. Integrating OpenShift with external CI tools
                                                                                                                                                        • ☰ 15. Operators and Platform Services
                                                                                                                                                          • ☰ 15.1. What are Operators
                                                                                                                                                            • ☰ 15.2. Operator Lifecycle Manager
                                                                                                                                                              • ☰ 15.3. Installing and managing Operators
                                                                                                                                                                • ☰ 15.4. Common platform Operators
                                                                                                                                                                  • ☰ 15.5. Custom Operators
                                                                                                                                                                  • ☰ 16. Upgrades, Maintenance, and Operations
                                                                                                                                                                    • ☰ 16.1. OpenShift upgrade process
                                                                                                                                                                      • ☰ 16.2. Backup and restore
                                                                                                                                                                        • ☰ 16.3. Node maintenance
                                                                                                                                                                          • ☰ 16.4. Cluster troubleshooting
                                                                                                                                                                            • ☰ 16.5. Capacity planning
                                                                                                                                                                            • ☰ 17. OpenShift on HPC and Specialized Workloads
                                                                                                                                                                              • ☰ 17.1. Running batch workloads on OpenShift
                                                                                                                                                                                • ☰ 17.2. OpenShift and GPUs
                                                                                                                                                                                  • ☰ 17.3. MPI and parallel workloads in containers
                                                                                                                                                                                    • ☰ 17.4. Hybrid HPC and cloud workflows
                                                                                                                                                                                    • ☰ 18. OpenShift in Practice
                                                                                                                                                                                      • ☰ 18.1. Typical OpenShift workflows
                                                                                                                                                                                        • ☰ 18.2. Development to production lifecycle
                                                                                                                                                                                          • ☰ 18.3. Case studies from industry
                                                                                                                                                                                            • ☰ 18.4. Common pitfalls and best practices
                                                                                                                                                                                            • ☰ 19. Future Directions and Ecosystem
                                                                                                                                                                                              • ☰ 19.1. Kubernetes ecosystem overview
                                                                                                                                                                                                • ☰ 19.2. OpenShift roadmap
                                                                                                                                                                                                  • ☰ 19.3. Serverless and event-driven workloads
                                                                                                                                                                                                    • ☰ 19.4. AI and data platforms on OpenShift
                                                                                                                                                                                                    • ☰ 20. Final Project and Hands-On Exercises
                                                                                                                                                                                                      • ☰ 20.1. Deploying a containerized application
                                                                                                                                                                                                        • ☰ 20.2. Implementing CI/CD with OpenShift
                                                                                                                                                                                                          • ☰ 20.3. Securing and scaling an application
                                                                                                                                                                                                            • ☰ 20.4. Monitoring and troubleshooting report