Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update commit status outside pull request #79

Open
bpoland opened this issue Apr 21, 2020 · 3 comments
Open

Update commit status outside pull request #79

bpoland opened this issue Apr 21, 2020 · 3 comments

Comments

@bpoland
Copy link

bpoland commented Apr 21, 2020

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
Copy link

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
Copy link

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
Copy link

l3ender commented Aug 27, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants