Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Implement Source-to-Image builds in an Azure DevOps pipeline

March 21, 2024
Kevin Chung Amr Abualrub
Related topics:
CI/CDDevOpsGitOpsKubernetes
Related products:
Azure Red Hat OpenShiftRed Hat OpenShift

Share:

    The advent of cloud technologies brings many benefits to the enterprise, such as provisioning at scale and a cloud-native DevOps approach. However, it also brings new challenges for developers and infrastructure engineers who wish to migrate from their datacenter to the public cloud. Among those challenges are integrating existing development methodologies with tool sets provided by the cloud provider as on-premise and cloud environments can each differ greatly. In this article, we explore a real-world implementation of a complete CI/CD pipeline using Source-to-Image builds deployed in Microsoft Azure Red Hat OpenShift with Azure tooling (see Figure 1).

    Azure DevOps Pipeline
    Figure 1: This logical workflow highlights the pipeline that will be built in this article.

    The tools that will be leveraged in this article are:

    • Azure Red Hat OpenShift
    • Azure DevOps (ADO) with Azure Repos and Azure Pipelines
    • Azure Container Registry (ACR)
    • Red Hat OpenShift GitOps (Argo CD)
    • Argo CD Image Updater
    • OpenShift Source-to-Image (S2I)

    Disclaimer: The information contained in this article represents the views and opinions of its authors and does not necessarily represent recommended practices or guidance from Red Hat.

    Prerequisites

    This article assumes that you have already provisioned a Red Hat OpenShift cluster. To showcase the Azure stack, we have opted to deploy Azure Red Hat OpenShift, but any OpenShift cluster that can access Azure public cloud endpoints can be used. Enterprises typically deploy Azure Red Hat OpenShift as a private cluster with access to their corporate network. The Azure Pipelines agent needs network connectivity to the cluster to facilitate an Azure Pipelines build. The default Azure Pipelines agent can be used, but if you are attempting this tutorial in a private cluster, you should follow these instructions to create an Azure build agent instead.

    Next, instantiate a container registry in ACR. While other container registries can be used, we have opted to showcase ACR configured for an Azure Red Hat OpenShift private cluster as part of the Azure solution stack. Once that is set up, continue to Azure DevOps and set up an organization, create a project, and link the project to the ACR repository by setting up a service connection.

    Staging the builder image in ACR

    Red Hat’s builder images consist of three fundamental elements: source code, source-to-image (S2I) scripts, and builder images. In our example, we will use the Red Hat JBoss Web Server builder image.

    We will start by pushing the builder image to Azure Container Registry. Be sure to replace the <acr-registry-name> with the registry name you created earlier.

    To push the image to the registry, run the following commands to use a token and authenticate into the registry:

    $ ACR_REGISTRY=<acr-registry-name>.azurecr.io
    $ az login
    $ ACR_TOKEN=$(az acr login --name $ACR_REGISTRY --expose-token --query 'accessToken' | jq -r )

    After running this command, you will receive a temporary login token. Utilize the token to login, as follows:

    $ podman login $ACR_REGISTRY -u 00000000-0000-0000-0000-000000000000 -p $ACR_TOKEN 

    Then, pull the builder image from upstream, and tag and push to your ACR:

    $ podman login registry.redhat.io
    $ podman pull --arch=amd64 registry.redhat.io/jboss-webserver-6/jws60-openjdk17-openshift-rhel8:latest
    $ podman tag registry.redhat.io/jboss-webserver-6/jws60-openjdk17-openshift-rhel8 $ACR_REGISTRY/jboss-webserver-6/jws60-openjdk17-openshift-rhel8
    $ podman push $ACR_REGISTRY/jboss-webserver-6/jws60-openjdk17-openshift-rhel8 --remove-signatures

    Staging the manifests and setting up the pipeline

    Now that you have staged your image in ACR, it is time to stage the application and pipeline manifests in Azure Repos. For our Source-to-Image build, we have decided to wrap the assemble and run S2I scripts within a Dockerfile as it provides the necessary integration with Azure Pipelines tasks. Within the Dockerfile, we uploaded the source code for the build to the pod's /tmp/src directory. We also set the .s2i/bin directory permissions to match the running user of the base image, in this case UID 185.

    ADD --chown=1001:0 test-app-service/src /tmp/src
    ADD --chown=185:0 ./.s2i/bin/ ${S2I_HOME}/.s2i/bin

    Let’s start by importing our example Git repository into Azure Repos. Our repository uses source code from the helloworld-ws example from jboss-eap-quickstarts. This example utilizes a custom-made Azure Pipelines YAML file, a Dockerfile containing the S2I scripts, an OpenShift deployment, service, and route deployed with Kustomize, and an .s2i folder which contains the necessary assemble and run files.

    Note: You will need to replace the <acr-registry-name> with your registry name in the following files:

    • test-app-service/app/base/deployment.yaml
    • test-app-service/Dockerfile
    • test-app-service/azure-pipelines.yml

    You also need to replace <cluster-name> and <region> in the following file:

    • test-app-service/app/base/deployment.yaml

    Lastly, you need to replace <docker-connection-name> and <agent-pool> in the following file:

    • test-app-service/azure-pipelines.yml

    Once you have imported the repository, click on Pipelines, and select New pipeline. It will prompt you to select your repository; select the repository that you have just created and press next. In the Configure section, select Existing Azure Pipelines YAML file, select your branch, and select the path of the .yml file—in this case, it is /test-app-service/azure-pipelines.yml. Then press continue.

    You will then be asked to review your pipeline YAML. Here, make any necessary changes to any naming conventions you may have on your side. Now, you have a working pipeline ready to go.

    Running the pipeline

    Now is the time to run your pipeline. On the initial run, you will likely be presented with a message to give permissions needed to run the build and push job (see Figure 2).

    Prompt to add permissions
    Figure 2: This popup message requires the user to give permissions to run the build and push job.

    Click Permit, and then when you receive the confirmation message, select Permit again (see Figure 3):

    Confirmation box
    Figure 3: A confirmation message asks the user to grant the agent pool permission to run the pipeline.

    Follow the build logs if you choose. If there are no errors, navigate to your ACR instance and ensure your image is present. The name of the image produced will be: .azurecr.io/my-dev/helloworld-ws.

    Deploying the image and utilizing Image Updater

    Now that we have our image staged, it is time to get OpenShift GitOps (upstream is Argo CD) and Argo CD Image Updater up and running. Argo CD is a declarative GitOps engine to deploy Kubernetes-based infrastructure and applications. Argo CD Image Updater uses annotations on Argo CD application image tags to regularly query the corresponding container registry and optionally update the application with the new image. Follow the documentation on how to install OpenShift GitOps and Argo CD Image Updater.

    The easiest way to install Argo CD Image Updater is to run the following command. Note that this command should be run in the same project in which your OpenShift GitOps or Argo CD instance is installed, in our case, openshift-gitops project. Refer to the documentation for additional configuration of Argo CD Image Updater.

    $ oc apply -n openshift-gitops -f https://raw.githubusercontent.com/argoproj-labs/argocd-image-updater/stable/manifests/install.yaml

    Note that it is important to set up your pull secret in the project in which you are deploying your application. In this case, the namespace is called helloworld-s2i. To create one, ensure that the Admin user is enabled in your ACR instance, and then enter the following commands in your command prompt:

    $ oc new-project helloworld-s2i
    $ ACR_PW=$(az acr credential show -n $ACR_REGISTRY --query 'passwords[0].value' | jq -r)
    $ ACR_USER=$(az acr credential show -n $ACR_REGISTRY --query 'username' | jq -r)
    $ oc create secret -n helloworld-s2i docker-registry \
        --docker-server=$ACR_REGISTRY \
        --docker-username=$ACR_USER \
        --docker-password=$ACR_PW \
        --docker-email=unused \
        acr
    $ oc secrets link default acr --for=pull -n helloworld-s2i

    Argo CD Image Updater requires a pull secret, as it will need to access the metadata in the ACR registry. Therefore, you will need to repeat these commands in your Argo CD namespace:

    $ oc create secret -n openshift-gitops docker-registry \
        --docker-server=$ACR_REGISTRY \
        --docker-username=$ACR_USER \
        --docker-password=$ACR_PW \
        --docker-email=unused \
        acr
    $ oc secrets link default acr --for=pull -n openshift-gitops

    Now you can create a new OpenShift application. An example GitOps application YAML file is included in the example Git repository. You will need to replace <gitops-argo-namespace>, <acr-registry-name>, and <git-repo-url> in the following file:

    • application.yaml

    Navigate to your Azure Pipeline, and initiate a new pipeline run. You will find that your pipeline now produces a new image, and that image will be pushed to your ACR registry. Argo CD Image Updater will continue to poll the registry for updates on the image you have defined, by default, every 2 minutes. When Argo CD Image Updater has detected a change to your image, it will make the update, prompting Argo CD to deploy a new pod matching your new image.

    Next, check your application:

    $ oc get pods -n helloworld-s2i

    You will find that a new pod has been created, using the newly implemented image.

    Conclusion

    In this tutorial, we have successfully used Source-to-Image builds leveraging Azure DevOps tools, specifically, Azure Repos and Azure Pipelines, for continuous integration. Then, we leveraged OpenShift GitOps (Argo CD) for continuous deployment. In addition to creating a CI/CD pipeline, the CD process was further automated with Argo CD Image Updater to allow for the most seamless transition between container images, allowing for the least possible amount of downtime for your applications.

    Related Posts

    • Build smaller container images using S2I

    • How to add libraries to a Node.js container with S2I

    • Pipenv and S2I: A better way to manage Python dependencies in containers

    • Network observability with eBPF on single node OpenShift

    • Using Kubernetes readiness and liveness probes for health checks with ASP.NET Core 2.2 on OpenShift

    • Deploy Quarkus applications directly to OpenShift using S2I

    Recent Posts

    • How Trilio secures OpenShift virtual machines and containers

    • How to implement observability with Node.js and Llama Stack

    • How to encrypt RHEL images for Azure confidential VMs

    • How to manage RHEL virtual machines with Podman Desktop

    • Speech-to-text with Whisper and Red Hat AI Inference Server

    What’s up next?

    Podman Desktop is a lightweight and efficient tool for managing containers and working with Kubernetes from your Windows, macOS, or Linux machine. This learning path demonstrates how you can go from an initial application to a container to a fully running pod on Kubernetes using Podman Desktop and the no-cost Developer Sandbox for Red Hat OpenShift.

    Start the activity
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue