Skip to content

China 前端 Web 项目 Pipeline 全流程

项目地址:git@gitlab.测试.cn:gitlab-instance-42ff40f0/chinadocker.git


A. 推送代码到 GitLab

  1. 在本地初始化或进入项目目录:
bash
cd /path/to/chinadocker
git init
git remote add origin git@gitlab.oldboylinux.cn:gitlab-instance-42ff40f0/chinadocker.git
git add .
git commit -m "初始提交"
git push -u origin master

在 GitLab 上新建仓库 china-pipeline-max 并确认推送成功。 devdev

B. Jenkins 配置 Git

  1. 在 Jenkins「凭证管理」中添加 SSH 私钥凭证(对应你的 GitLab 访问秘钥)。
  2. 在新建的 Jenkins 任务中,Source Code Management 选择 Git,填写:
    • Repository URL: git@gitlab.oldboylinux.cn:gitlab-instance-42ff40f0/chinadocker.git
    • Credentials: 选择上一步添加的 SSH Key
  3. Test Connection 确认连通无误。

dev

C. SonarQube 安全分析

注意:请提前在本机 /etc/hosts 中解析 10.0.0.73 sonar.local(或你的域名)。

  1. 在 SonarQube UI 新建项目 Key=china-pipeline;生成令牌(Token):
plain
复制编辑
5a15e9aae800497be8c82158585e55682bf76d94
  1. 在本地执行扫描:
plain
bash


复制编辑
sonar-scanner \
  -Dsonar.projectKey=china-pipeline \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://10.0.0.73:9000 \
  -Dsonar.login=5a15e9aae800497be8c82158585e55682bf76d94

D. 登录阿里云 ACR

plain
docker login \
  --username=aliyun9085543940 \
  crpi-3vo80rqglo621of4.cn-hangzhou.personal.cr.aliyuncs.com
# 输入 Access Key Secret,登录成功后可推镜像

E. Jenkinsfile 编写

plain
pipeline {
  agent any
  environment {
    PATH = "/app/tools/sonar-scanner/bin:/app/tools/node/bin:/usr/bin:/bin:/usr/sbin:/sbin"
  }
  stages {
    stage('1. 代码安全分析') {
      steps {
        sh '''
          sonar-scanner \
            -Dsonar.projectKey=china-pipeline \
            -Dsonar.sources=. \
            -Dsonar.host.url=http://10.0.0.73:9000 \
            -Dsonar.login=5a15e9aae800497be8c82158585e55682bf76d94
        '''
      }
    }
    stage('2. 编译') {
      steps {
        sh '''
          npm install
          npm run build
        '''
      }
    }
    stage('3. 构建镜像') {
      steps {
        sh 'docker build -t china:pipeline .'
      }
    }
    stage('4. 推送镜像到阿里云') {
      steps {
        sh '''
          docker tag china:pipeline crpi-3vo80rqglo621of4.cn-hangzhou.personal.cr.aliyuncs.com/linuxpath/redhat:china-pipeline
          docker push crpi-3vo80rqglo621of4.cn-hangzhou.personal.cr.aliyuncs.com/linuxpath/redhat:china-pipeline
        '''
      }
    }
    stage('5. 运行到 Web 服务器') {
      steps {
        sh '''
          docker ps -a -f name=china-pipeline --format '{{.ID}}' | xargs -r docker rm -f
          docker run -d --name china-pipeline -p 1949:80 crpi-3vo80rqglo621of4.cn-hangzhou.personal.cr.aliyuncs.com/linuxpath/redhat:china-pipeline
        '''
      }
    }
    stage('6. 运行成功') {
      steps {
        echo 'hello success'
      }
    }
  }
  post {
    success {
      script {
        qyWechatNotification failNotify: true,
          webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e287fd4e-682f-4c58-8586-af9f446770f2'
      }
    }
    failure {
      script {
        qyWechatNotification failNotify: true,
          webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e287fd4e-682f-4c58-8586-af9f446770f2'
      }
    }
  }
}

F. Pipeline 运行效果

  • Jenkins Console Output 将显示每个阶段的日志,包括 SonarQube 分析报告、构建、推送和运行结果。
  • 成功时会打印 hello success

dev

G. 企业微信机器人告警效果

当流水线成功或失败时,Jenkins 会调用企业微信机器人 Webhook,将结果推送到对应群组。


dev

dev

H. 安全分析效果


dev

I. 阿里云镜像仓库效果

dev

感谢阅读,欢迎交流!