GiteeGo 和 Jenkins 有多种相似之处,这使得迁移到 GiteeGo 相对比较简单。
Jenkins 和 GiteeGo 都允许您创建能自动构建、测试、发布、发行和部署代码的工作流程。 Jenkins 和 GiteeGo 的工作流程配置有一些相似之处:
Jenkins 有两种类型的语法用来创建管道:Declarative Pipeline 和 Scripted Pipeline。 GiteeGo使用 YAML 创建工作流程和配置文件。更多信息请参阅“GiteeGo的工作流程搭建”
使用阶段搭建Pipeline
Jenkins 将其 Declarative Pipelines 分为多个阶段。 同样,GiteeGo 也将其工作流程分成单独的部分。 下表比较了Jenkins 阶段与 GiteeGo 工作流程。
| Jenkins指令 | GiteeGo Pipeline | 
|---|---|
| agent | steps.step.hostGroupID | 
| post | |
| stages | stages | 
| steps | steps | 
预定义变量
Jenkins 和 GiteeGo 设置多个环境变量,以便检查持续集成系统的执行环境并与之交互。
| 说明 | Jenkins | GiteeGo Pipelines | 
|---|---|---|
| 当前流水线构建的唯一数字标识符 | BUILD_NUMBER | GITEE_PIPELINE_BUILD_NUMBER | 
| Git 提交 ID | GIT_COMMIT | GIT_COMMIT | 
| Git 仓库路径 | GIT_URL | |
| Git 提交分支 | GIT_BRANCH | GIT_BRANCH | 
| 签出源的位置 | WORKSPACE | GITEE_REPO | 
GiteeGo额外提供以下参数以供使用:
| 说明 | 参数名 | 
|---|---|
| 流水线唯一标识 | GITEE_PIPELINE_NAME | 
| 流水线名字 | GITEE_PIPELINE_DISPLAY_NAME | 
| 流水线触发人 | GITEE_PIPELINE_TRIGGER_USER | 
| 流水线触发人ID | GITEE_PIPELINE_TRIGGER_USER_ID | 
| 阶段唯一标识 | GITEE_STAGE_NAME | 
| 阶段名字 | GITEE_STAGE_DISPLAY_NAME | 
| 阶段触发策略 | GITEE_STAGE_STRATEGY | 
| Push和Tag的message或PR的title | GITEE_COMMIT_MESSAGE | 
| 发起PR时的源分支 | GITEE_SOURCE_BRANCH | 
| 发起PR时的源分支commit | GITEE_SOURCE_COMMIT | 
| 发起PR时的PR号 | GITEE_PULL_ID | 
Jenkins 使用指令来管理 Declarative Pipelines。 这些指令定义工作流程的特性及其执行方式。 下表演示这些指令如何映射到 GiteeGo 中的概念。
| Jenkins指令 | GiteeGo Pipeline | 
|---|---|
| environment | variables | 
| options | 
strategy.stepTimeout   stages.stage.strategy   stages.stage.trigger
 | 
| triggers | `triggers.pr.<branches | 
| triggers { upstreamprojects() } | |
| Jenkins cron syntax | |
| stages | stages | 
| tools | |
| when | 
并行作业处理
Jenkins 可以并行运行 stages 和 steps,而 GiteeGo 目前只能并行运行step。
| Jenkins Parallel | GiteeGo Pipeline | 
|---|---|
| parallel | |
| 使用步骤执行任务 | |
| Jenkins 将 steps 组织在 stages。 每个步骤都可以是脚本、函数或命令等。 GiteeGo 与 Jenkins 组织方式都一致。 | |
| Jenkins 步骤 | GiteeGo Pipeline | 
| --- | --- | 
| script | 
计划与 cron 一起运行的流水线
Jenkins Pipeline
pipeline {
  agent any
  triggers {
    cron('*/1 * * * *')
  }
}
GiteeGo Pipeline
triggers:
    schedule:
      - cron: '*  */1  *  *  *  ?  *'
配置流水线中的环境变量
Jenkins Pipeline
pipeline {
  agent any
  environment {
    MAVEN_PATH = '/usr/local/maven'
  }
}
GiteeGo Pipeline
variables:
  MAVEN_PATH: '/usr/local/maven'
从上游流水线构建
Jenkins Pipeline
pipeline {
  triggers {
    upstream(
      upstreamProjects: 'job1,job2',
      threshold: hudson.model.Result.SUCCESS
    )
  }
}
GiteeGo Pipeline
后期会支持
使用多个阶段构建
Jenkins Pipeline
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}
GiteeGo Pipeline
version: 1.0
name: demo-workflow
displayName: 'demo-workflow'
triggers:
  push:
    branches:
      prefix:
        - ''
stages:
  - name: Build
    displayName: Build
    strategy: naturally
    trigger: auto
    steps:
      - step: build@maven
        name: build_maven
        displayName: Maven 构建
        jdkVersion: 8
        mavenVersion: 3.3.9
        commands:
          - mvn clean package
  - name: Deploy
    displayName: Deploy
    strategy: naturally
    trigger: auto
    steps:
      - step: publish@general_artifacts
        name: publish_general_artifacts
        displayName: 上传制品
        dependArtifact: BUILD_ARTIFACT
        artifactRepository: default
        artifactName: output
Demo完整样例
Jenkins Pipeline
pipeline {
    agent {
        node {
            label "agent-119"
        }
    }
    stages {
        stage('Build') {
            steps {
                sh '''
                    mvn -B clean package -Dmaven.test.skip=true
                '''
            }
        }
        stage('BuildImage') {
            steps {
                sh '''
                    docker build -t docker.io/gitee-go/test-maven:v1.0.0 .
                '''
            }
        }
        stage('Deploy') {
            steps {
                sh '''
                    mv ./target/maven-demo-0.0.1-SNAPSHOT.jar ~/gitee_go/deoloy
                    cd ~/gitee_go/deoloy
                    ls
                    nohup java -jar maven-demo-0.0.1-SNAPSHOT.jar --server.port=7081 & 
                    echo "Deploy Success"
                    sleep 10
                    curl 127.0.0.1:7081
                '''
            }
        }
    }
}
GiteeGo Pipeline
version: '1.0'
name: master-pipeline
displayName: MasterPipeline
triggers:
  push:
    branches:
      include:
        - master
stages:
  - name: compile
    displayName: 编译
    strategy: naturally
    trigger: auto
    steps:
      - step: build@maven
        name: build_maven
        displayName: Maven 构建
        jdkVersion: 8
        mavenVersion: 3.3.9
        artifacts:
          - name: BUILD_ARTIFACT
            path:
              - ./target
        commands:
          - mvn -B clean package -Dmaven.test.skip=true
      - step: publish@general_artifacts
        name: publish_general_artifacts
        displayName: 上传制品
        dependArtifact: BUILD_ARTIFACT
        artifactRepository: default
        artifactName: output
        dependsOn: build_maven
  - name: docker-build
    displayName: 镜像构建
    strategy: naturally
    trigger: auto
    executor: []
    steps:
      - step: build@docker
        name: build_docker
        displayName: 镜像构建
        repository: registry.baidubce.com/gitee-test-repo
        username: 112c6b0ac67e42fe97beb3f15a9be562
        password: OSCccr2021
        tag: test:${GITEE_PIPELINE_BUILD_NUMBER}
        dockerfile: ./Dockerfile
        artifacts:
          - ${BUILD_ARTIFACT_PATH}
        isCache: false
  - name: deploy
    displayName: 部署
    strategy: naturally
    trigger: auto
    steps:
      - step: deploy@agent
        name: deploy_agent
        displayName: 主机部署
        hostGroupID: gitee-go-test
        deployArtifact:
          - source: artifact
            name: output
            target: ~/gitee_go/deoloy
            artifactRepository: release
            artifactName: output
            artifactVersion: latest
        script: |
          cd ~/gitee_go/deoloy
          ls
          tar -zxf output.tar.gz
          cd target
          nohup java -jar maven-demo-0.0.1-SNAPSHOT.jar --server.port=7081 & 
          echo "Deploy Success"
          sleep 10
          curl 127.0.0.1:7081
permissions:
  - role: admin
    members: []