Jenkins 管道 - 推送、拉取请求和标记事件

Jenkins Pipeline - Push, PullRequest, and Tag events

我需要创建一个 Jenkins 作业,它能够将所有这 3 种事件传送到此示例管道

pipeline {
agent {
    node {
        label 'pipeline-docker-agent'
    }
}
stages {
    stage('Build Branches and PR') {
        /*Test, vet, build and discard*/
        when { anyOf{ branch "*"; changeRequest() } }
        steps {
            echo 'test,vet and build'
        }
    }
    stage('Publish master Artifact') {
        when { branch 'master' }
        steps {
            echo 'Then Push the artifact, since its the master..'
            echo 'Then Tag the SCM as required'
        }
    }
    stage('Deploy to Stage') {
        when { tag "rc-*" }
        steps {
            echo 'Downloading the artifact'
            echo 'Deploying to STAGEing'
        }
    }
    stage('Deploy prod') {
        when { tag "release-*" }
        steps {
            echo 'Downloading the artifact'
            echo 'Deploying to Prod'
        }
    }
}

怎么做,我应该安装哪些插件?

来自现代 CI 工具,例如 drone.io、圆 CI。 Jenkins 管道被宣传为等同于这些现代 CI 工具。试图在 Jenkins 中复制类似的行为。

不幸的是,从 Jenkins 管道迁移到上述 CI 工具之一在我当前的组织中不是一个选项。我们的 platform/Infra 架构师对古老的工具和技术有着浓厚的兴趣。叹!。吐槽完了任何帮助将不胜感激。

一个更具体的用例是,我想触发所有新事件的自动构建,而不是过去的事件。 (尝试过的插件之一甚至触发了封闭 PR 和旧标签等的构建)

GitHub企业就是SCM。

尝试使用多分支管道配置

找到符合我要求的 Jenkins 插件

https://github.com/KostyaSha/github-integration-plugin

这个插件连同 multi-branch 管道作业配置让我可以创建一个可以处理提交、PR 和标记事件的管道

这是配置

我不得不稍微更改我的管道声明语法

pipeline {
    agent {
        node {
            label 'pipes-docker-agent'
        }
    }
    stages {
        stage('Build Branches and PR') {
            /*Test, vet, build */
            when { expression { !env.GITHUB_TAG_NAME } }
            steps {
                echo 'test,vet and build'
            }
        }
        stage('Publish master Artifact') {
            /*Jenkins have a problem with the intuitive syntax */
            when { expression { env.BRANCH_NAME && env.BRANCH_NAME.toString().equals('master') } }
            steps {
                echo 'Then Push the above-built artifact, since its the master. Need to have similar strategy for Hotfix branches'
                echo 'Then Tag the SCM as required'
            }
        }
        stage('Deploy to Stage') {
            when {expression { env.GITHUB_TAG_NAME && env.GITHUB_TAG_NAME.toString().startsWith("rc-") } }
            steps {
                echo 'Downloading the artifact'
                echo 'Deploying to STAGEing'
            }
        }
        /* I think its wise to move this as another Job*/
        stage('Deploy prod') {
            when {expression { env.GITHUB_TAG_NAME && env.GITHUB_TAG_NAME.toString().startsWith("release-") } }
            steps {
                echo 'Downloading the artifact'
                echo 'Deploying to Prod'
            }
        }
    }
}