DEV Community

Cover image for Deploying AI Projects Through a Jenkins Pipeline
Jesse Williams for KitOps

Posted on

Deploying AI Projects Through a Jenkins Pipeline

Imagine building the "perfect machine learning model" only to face the tedious, error-prone process of manually packaging your code, pushing container images to a registry, and restarting servers with every change you make. Without Continuous Integration/Continuous Deployment (CI/CD), this would be the daily grind for developers and ML engineers.

CI/CD is a DevOps concept that relieves developers and machine learning engineers of manual pain by automating the build, tests, and deployment of models. By automatically building, testing, and versioning your models and dependencies—datasets, code, and configurations—CI/CD allows large teams to collaborate and deploy seamlessly, minimizing the risk of errors or conflicts.

Jenkins is an open-source CI/CD platform that facilitates building, testing, and deploying AI projects. It also has a user interface that tracks pipeline runs visually. In this article, you will learn how to deploy your AI projects automatically through a Jenkins pipeline.

TL;DR

  • KitOps enables teams to easily unpack artifact components such as models, code, and datasets to different directories.
  • Jenkins lets you track pipeline runs and monitor your AI pipelines through a visual interface.
  • Jenkins and KitOps simplify the process of building and deploying AI projects.

Steps to deploying AI projects through a Jenkins pipeline

Prerequisites

To follow through, you will need the following:

Verify KitOps is installed

After installation, verify KitOps is installed by running the command on your local terminal:

kit version
Enter fullscreen mode Exit fullscreen mode

Create a Jozu Hub repository

Login to your Jozu Hub account and create a repository. Here, I created an empty repository called jenkins_repo.

Jenkins repo JozuHub

You can authenticate your local terminal with JozuHub by running the command:

kit login jozu.ml
Enter fullscreen mode Exit fullscreen mode

This prompts for your username, which is the email address used to create your Jozu Hub account and password.

Unpack a ModelKit

After logging in to Jozu Hub, you can grab any of the available ModelKits from their package registry. Start by unpacking the Qwen model from Jozu Hub.

On your local terminal, run the command:

kit unpack jozu.ml/jozu/qwen2-0.5b:0.5b-instruct-q2_K
Enter fullscreen mode Exit fullscreen mode

Upon unpacking a ModelKit, you will see some files: Kitfile, a Qwen model, a License, and a markdown document.

Currently, the directory structure is as shown below:

|-- Kitfile
|-- qwen2-0_5b-instruct-q2_k.gguf
|-- README.md
|-- LICENSE
Enter fullscreen mode Exit fullscreen mode

The Kitfile created after unpacking the ModelKit is displayed below.

manifestVersion: 1.0.0
package:
  name: qwen2-0.5B
  version: 2.0.0
  description: The instruction-tuned 0.5B Qwen2 large language model.
  authors: [Alibaba Cloud]
model:
  name: qwen2-0_5b-instruct-q2_k
  path: qwen2-0_5b-instruct-q2_k.gguf
  license: Apache 2.0
  description: 0.5B-instruct-q2_k.
code:
  - path: LICENSE
    description: License file.
  - path: README.md
    description: Readme file.
Enter fullscreen mode Exit fullscreen mode

Create some new folders like models and docs, and modify the directory structure to:

|-- Kitfile
|-- models
  |-- qwen2-0_5b-instruct-q2_k.gguf
|-- docs
  |-- LICENSE
  |-- README.md
Enter fullscreen mode Exit fullscreen mode

Modify the Kitfile to reflect the modified directory structure. In this modified Kitfile, the model path was changed to models/qwen2-0_5b-instruct-q2_k.gguf. The LICENSE and README.md path were also changed to docs/LICENSE and docs/README.md respectively.

manifestVersion: 1.0.0
package:
  name: qwen2-0.5B
  version: 2.0.0
  description: The instruction-tuned 0.5B Qwen2 large language model.
  authors: [Alibaba Cloud]
model:
  name: qwen2-0_5b-instruct-q2_k
  path: models/qwen2-0_5b-instruct-q2_k.gguf
  license: Apache 2.0
  description: 0.5B-instruct-q2_k.
code:
  - path: docs/LICENSE
    description: License file.
  - path: docs/README.md
    description: Readme file.
Enter fullscreen mode Exit fullscreen mode

Now that your Kitfile, model, and dependencies are ready, let's build the CI/CD pipeline using Jenkins. Before proceeding, verify the installation of Jenkins.

Verify Jenkins is installed

After installing Jenkins, verify your local installation by heading to [http://localhost:8080](http://localhost:8080). You will see a page similar to the one in the image below. Depending on your operating system, the path to your administrator password will be shown to you.

Verify Jenkins

After inputting your administrator password, you will be prompted to create an admin user. When this user is created, you should see the Jenkins home screen:

Jenkins Home page

Create Jenkins credentials

You can create Jenkins credentials to securely enter your Jozu Hub username and password. Navigate to Manage Jenkins → Credentials → Global credentials and create two credentials. The first credential will be of type Secret text and contain the Jozu Hub username.

Creating username credentials

Similarly, create another credential for your Jozu Hub password, as shown in the image below.

Create password credentials

After creating the credentials, build your pipeline.

Build your pipeline

On the Jenkins UI, go to New Item and create a pipeline. The pipeline created in the screenshot below was named kitops_pipeline.

Create Jenkins pipeline

After creation, configure your pipeline and select your GitHub project. In the project url, add a link to your repository created on GitHub.

Set up Jenkins pipeline - build settings

Select the GitHub hook trigger as the build trigger.

Setup Jenkins pipeline - Source Code Management

Under the pipeline definition, configure your Jenkins pipeline to grab the Jenkinsfile from the GitHub repository. Specify your repository URL, the branches you are currently working on, and the name of your Jenkinsfile as your script path.

Setup Jenkins pipeline - Build from Jenkinsfile

Create your GitHub webhook

In your GitHub repository settings, add a webhook to connect with Jenkins. Set the Payload URL to JENKINS_URL/github-webhook/, replacing JENKINS_URL with your Jenkins server’s URL. Then, select Just the push event to trigger notifications only when code is pushed, and save the configuration.

Now that your webhook has been created, anytime you push to GitHub, it searches for Jenkinsfile and then runs your Jenkins pipeline. If you make a push now, it will throw an error. The error is due to the fact that the configuration looks for a jenkinsfile to run the code, which is not present.

Create your Jenkinsfile

Create a file called Jenkinsfile in your root directory. This file contains the steps to log in to Jozu Hub, pack the ModelKit, and push the ModelKit to the remote repository.

Write the following code in your Jenkinsfile:

pipeline {
    agent any 
    environment {
        USERNAME = credentials('USERNAME')
        PASSWORD = credentials('PASSWORD')
    }
    stages {
        stage('install kitops') {
            steps {
                cleanWs()
                git(url: 'https://github.com/Techtacles/kitops-jenkins.git', branch: 'master')
                sh 'ls'
                sh 'wget https://github.com/jozu-ai/kitops/releases/latest/download/kitops-linux-x86_64.tar.gz'
                sh 'tar -xzvf kitops-linux-x86_64.tar.gz'
                sh './kit version'
            }
        }
        stage('Login to JozuHub'){
            steps {
                sh './kit login jozu.ml -u $USERNAME -p $PASSWORD'
                echo 'Successfully logged in to jozuhub'
            }
        }
        stage('Tagging and pushing to remote repository'){
            steps{
                sh './kit unpack jozu.ml/jozu/qwen2-0.5b:0.5b-instruct-q2_K --model -d models/qwen2-0_5b-instruct-q2_k.gguf'
                sh './kit pack . -t jozu.ml/<your-jozu-username>/<your-jozu-repo>:latest'
                sh './kit push jozu.ml/<your-jozu-username>/<your-jozu-repo>:latest'
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The code snippet above outlines a Jenkinsfile with three stages; the first stage installs the latest version of Kitops, the second stage logs in to JozuHub using the username and password credentials configured earlier, and the final stage tags the Modelkit and pushes it to JozuHub.

Once you push your code to GitHub, the Jenkins pipeline starts.

Jenkins pipeline

This script logs in to your Jozu Hub, unpacks the model into the models/ directory, packs the ModelKit, and then pushes it to the remote repository.

Once the pipeline is completed, navigate to Jozu Hub and check your created repository. You will notice that a new ModelKit has been pushed to it.

Final image in JozuHub

Conclusion

It is very frustrating to deploy your AI projects manually every time you make a change. With Jenkins and KitOps, you can automate the building, testing, and deployment of models and their dependencies in production.

KitOps plays a key role in packaging models and managing dependencies. Jenkins helps you automate deployment whenever a change is made, resulting in faster, more reliable deployments and improved team collaboration.

If you have questions about integrating KitOps with your team, join the conversation on Discord and start using KitOps today!

Top comments (1)

Collapse
 
arindam_1729 profile image
Arindam Majumder

Great Share!