jenkinsci / pipeline-github-plugin

Pipeline: GitHub
https://plugins.jenkins.io/pipeline-github/
MIT License
159 stars 73 forks source link

Update commit status outside pull request #79

Open bpoland opened 4 years ago

bpoland commented 4 years ago

Does anyone know if there's a way to update a commit status when not building a pull request?

Our deployment infrastructure uses the commit status on master to determine whether it's safe to deploy. We're using https://github.com/jenkinsci/pipeline-githubnotify-step-plugin to do that right now but it would be nice to only use one plugin :)

aaronjwhiteside commented 4 years ago

This would fit in great with the idea of representing the entire repository and current branch as groovy objects... just like pullRequest is done now.

blundercon commented 3 years ago

Is someone working on this ? This would be a pretty handy feature. Currently, the options to interact with Github's APIs are kind of limited in the sense that we'd need install multiple plugins.

l3ender commented 2 weeks ago

We handle this with a shared pipeline method to use the Github plugin like following:

/* groovylint-disable CouldBeSwitchStatement, DuplicateStringLiteral */
// https://plugins.jenkins.io/github/#plugin-content-setting-commit-status

void call(Map config = [:]) {
    if (!(config.containsKey('context') && config.containsKey('state'))) {
        error "Error - unexpected state encountered when setting git status (expecting context/state): $config"
    }

    String state = config.state.toUpperCase()
    List allowedStates = ['PENDING', 'SUCCESS', 'ERROR', 'FAILURE']
    if (!allowedStates.contains(state)) {
        error "Error - unexpected commit state encountered when setting git status (expecting one of $allowedStates): $state"
    }

    String message = null
    if (config.containsKey('message')) {
        message = config.message
    } else if (state == 'SUCCESS') {
        message = 'This commit looks good'
    } else if (state == 'ERROR') {
        message = 'This commit encountered an error'
    } else if (state == 'FAILURE') {
        message = 'This commit failed'
    } else {
        message = 'This commit is being built'
    }

    String repoUrl = config.containsKey('repoUrl') ? config.repoUrl : env.GIT_URL
    String commit = config.containsKey('commit') ? config.commit : env.GIT_COMMIT

    step([
        $class: 'GitHubCommitStatusSetter',
        reposSource: [$class: 'ManuallyEnteredRepositorySource', url: repoUrl],
        commitShaSource: [$class: 'ManuallyEnteredShaSource', sha: commit],
        contextSource: [$class: 'ManuallyEnteredCommitContextSource', context: config.context],
        errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
        statusResultSource: [
            $class: 'ConditionalStatusResultSource',
            results: [
                [$class: 'AnyBuildResult', state: state, message: message],
            ]
        ]
    ])
}

Usage is like following from within a pipeline file:

setGitStatus(context: 'my-status-name', state: 'pending')
setGitStatus(context: 'my-status-name', state: 'success')

Hope it helps!