Background

Manual packaging and deployment is tedious and error-prone. Introducing Jenkins for automated integration solves this. The new workflow includes: automatic code pull, dependency check, packaging, Docker image build and push, static scanning, and result feedback.

Most projects are now connected to Jenkins and running in a Kubernetes environment, significantly improving development efficiency.

Server Project Configuration

Create a directory to store project files on the server.

Jenkins Project Configuration

Create a New Project

Create a new Freestyle or Pipeline project in Jenkins.

Configure Description

Add a meaningful description to identify the project.

Configure Workspace

Set the workspace directory for the project.

Pull Git Repository

Configure the Git repository URL pointing to your GitLab instance.

Configure Branch

Set the branch to build (e.g., test or main).

Enable WebHook

Enable the Build Trigger via GitLab webhook to allow GitLab to trigger Jenkins builds automatically.

Configure Credentials

Add GitLab access credentials (SSH key or personal access token) in Jenkins credential manager.

Execute Build Script

Add a shell build step with your deployment script:

#!/bin/bash
set -e

# Pull latest code
git pull origin test

# Build Docker image
docker build -t $HARBOR_HOST/$PROJECT/$APP_NAME:$BUILD_NUMBER .

# Push to Harbor
docker push $HARBOR_HOST/$PROJECT/$APP_NAME:$BUILD_NUMBER

# Deploy to Kubernetes
kubectl set image deployment/$APP_NAME $APP_NAME=$HARBOR_HOST/$PROJECT/$APP_NAME:$BUILD_NUMBER

GitLab Project Configuration

Configure Webhook

In GitLab project settings, go to Settings → Webhooks and add the Jenkins trigger URL:

http://<jenkins-host>/project/<job-name>

Select the trigger events (e.g., Push events, Merge request events) and save.

Branch Strategy

  • main / master: production-ready code
  • dev: main development branch, team members branch off from here
  • Personal branches: created from dev for individual features
  • test: integration branch, merge personal branches here for testing
  • Merge requests: used for code review before merging to test

Testing the Full Pipeline

  1. Push code to a personal branch
  2. Create a merge request to test
  3. After approval and merge, GitLab triggers the Jenkins webhook
  4. Jenkins pulls the latest code, builds the Docker image, and deploys to Kubernetes
  5. Verify the deployment is successful

This automated pipeline eliminates manual steps and reduces human error in the deployment process.