Jenkins Pipeline Automated Build and Deployment

A record of the steps from installation through automated project deployment.

Contents

Jenkins

A record of the steps from installation to automated project deployment

Installation

Since I want to automate project deployment and have only one server, with all operations taking place on that same server, I’ve chosen to install Jenkins locally rather than run it in Docker. Please choose based on your actual circumstances.

Using Debian-based systems as an example
[Official installation guide] https://www.jenkins.io/zh/download/

  1. Import the Jenkins package repository key

     sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
        https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
    
  2. Import the Repository

     echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
        https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
        /etc/apt/sources.list.d/jenkins.list > /dev/null
    
  3. Installation

    sudo apt-get update
      sudo apt-get install fontconfig openjdk-17-jre
      sudo apt-get install jenkins
    

Installation is now complete. The system will automatically configure user isolation permissions for Jenkins.

【Next steps】https://www.jenkins.io/doc/book/installing/linux/

To change the port, modify the Service file.

vim /lib/systemd/system/jenkins.service

content:
Environment="JENKINS_PORT=20000"

修改后记得重载服务。

Start Jenkins

sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

Access the corresponding port to open the Jenkins dashboard. Enter the key from Status, then continue with the user configuration.

Configure the workspace: Assign ownership of a directory to the Jenkins user. Files used in subsequent Jenkins operations will be stored here.

Installation is now complete.

Configure Automated Tasks

  1. Click New Item to create a task.

  2. Select Freestyle project. This uses tools already installed on the system, avoiding duplicate installations. If you want Jenkins to manage the configuration, you can choose another option.

  3. Prepare the project files in the Jenkins user’s workspace, and configure Git SSH access.

    su jenkins  # 切换到jenkins用户
    ssh-keygen  # 生成密钥,填入Git托管平台
    
  4. Write commands using Execute shell in Build Steps

    【Java backend example】

    #!/bin/bash
    BUILD_ID=NULL
    export JAVA_HOME=/usr/local/jdk17/
    export PATH=$JAVA_HOME/bin:/usr/local/maven3.9.4/bin:$PATH
    
    # pull latest code & build jar file
    cd /home/jenkins/slms
    git pull -f
    cd backend
    mvn clean package -DskipTests
    
    # kill old process and move jar file
    PID=$(cat /home/jenkins/slms-backend/server.pid)
    kill -9 $PID || true
    rm -rf /home/jenkins/slms-backend/*.jar
    mv /home/jenkins/slms/backend/target/*.jar /home/jenkins/slms-backend/
    
    # run new process and note the application pid
    cd /home/jenkins/slms-backend/
    nohup java -jar backend-0.0.1-SNAPSHOT.jar --server.port=20002 > server.log 2>&1 &
    echo $! > server.pid
    

    【Node Front-End Example】

     export PATH=/usr/local/nvm/versions/node/v18.20.5/bin:$PATH
    
    cd /home/jenkins/slms
    git pull -f
    cd frontend
    pnpm i
    pnpm run build
    
    rm -rf /home/jenkins/slms-frontend/dist
    mv /home/jenkins/slms/frontend/dist /home/jenkins/slms-frontend/dist
    
  5. After completing the configuration, click Run and troubleshoot based on the output logs.

Configure the Trigger Mechanism

【Jenkins Pipeline Trigger Methods】

  • Actively poll Git for version changes on a schedule

  • Run at specified intervals

  • If supported by the Git hosting platform, trigger execution by actively notifying Jenkins

  • Or implement a custom remote trigger

  • Run after another pipeline has completed

Here, we’ll use 【actively polling Git for version changes on a schedule】 as an example.

  1. Under Source Code Management, select Git and specify the repository.

  2. Configure the access token or username and password for the Git platform.

  3. Specify the branch to trigger the build.

  4. Under Build Triggers, select Poll SCM.

H/15 * * * * 
MINUTE HOUR DOM MONTH DOW

具体可点击网页上的问号,查看详细解释。

To be continued