I had a breakthrough this morning.
Something funky goes on with Jenkinsfiles/multi-branch builds. I am forcing a configuration and specifying credentials manually. I also needed to generate an HTTPS URL string without the .git ending for a successful connection.
Before I file the issue, I call a function to setup the configuration. This takes a token ID as input.
```
def call(String credentialsID = 'github-oauth-token')
{
repoUrl = gitRepoHttpsUrl()
echo "Creating a GitHubPluginConfig for credential $credentialsID"
// configure github plugin
GitHubPluginConfig pluginConfig = GitHubPlugin.configuration()
GitHubServerConfig serverConfig = new GitHubServerConfig(credentialsID)
pluginConfig.setConfigs([serverConfig])
pluginConfig.save()
echo "Registering GithubProjectProperty for repo URL $repoUrl"
// Set a project property for the Github URL
properties([[$class: 'GithubProjectProperty', projectUrlStr: repoUrl]])
}
```
Here's the HTTPS URL generation function:
```
def call()
{
sh "git config --get remote.origin.url > .git/remote-url"
url = readFile(".git/remote-url").trim()
if(url.startsWith('git@'))
{
url = url - 'git@'
url = 'https://' + url
}
if(url.endsWith('.git'))
{
url = url - '.git'
}
return url
}
```
Then I call the issue notifier:
```
def call(String title = null, String label = null)
{
echo 'Build failed! Filing GitHub issue'
step([$class: 'GitHubIssueNotifier',
issueAppend: true,
issueLabel: '',
issueTitle: "${env.JOB_NAME}: Build failure"])
//$BUILD_DISPLAY_NAME failed'])
}
```
Using these steps, I have successfully created an issue in the Github repo when a build fails.
I had a breakthrough this morning.
Something funky goes on with Jenkinsfiles/multi-branch builds. I am forcing a configuration and specifying credentials manually. I also needed to generate an HTTPS URL string without the .git ending for a successful connection.
Before I file the issue, I call a function to setup the configuration. This takes a token ID as input.
```
def call(String credentialsID = 'github-oauth-token')
{
repoUrl = gitRepoHttpsUrl()
echo "Creating a GitHubPluginConfig for credential $credentialsID"
// configure github plugin
GitHubPluginConfig pluginConfig = GitHubPlugin.configuration()
GitHubServerConfig serverConfig = new GitHubServerConfig(credentialsID)
pluginConfig.setConfigs([serverConfig])
pluginConfig.save()
echo "Registering GithubProjectProperty for repo URL $repoUrl"
// Set a project property for the Github URL
properties([[$class: 'GithubProjectProperty', projectUrlStr: repoUrl]])
}
```
Here's the HTTPS URL generation function:
```
def call()
{
sh "git config --get remote.origin.url > .git/remote-url"
url = readFile(".git/remote-url").trim()
if(url.startsWith('git@'))
{
url = url - 'git@'
url = 'https://' + url
}
if(url.endsWith('.git'))
{
url = url - '.git'
}
return url
}
```
Then I call the issue notifier:
```
def call(String title = null, String label = null)
{
echo 'Build failed! Filing GitHub issue'
step([$class: 'GitHubIssueNotifier',
issueAppend: true,
issueLabel: '',
issueTitle: "${env.JOB_NAME}: Build failure"])
//$BUILD_DISPLAY_NAME failed'])
}
```
Using these steps, I have successfully created an issue in the Github repo when a build fails.