Integrating slack with github to get notifications on push happening to specific branch - github

I am looking for a way how i can get a message to slack channel when a push happens in specific branch of github repo.
I tried doing it with below mentioned steps -
1. Installed github app in slack.
2. Added github to slack channel using /github.
After that when i try connecting to repository, github asked to authenticate. When i tried to do so it is navigating me to github.com instead of custom github domain of the company.
Please help me here.

You could add to your repository a .github/workflows in order to use a GitHub Action (which is also available for GitHub Enterprise, not just github.com)
For example:
pullreminders/slack-action
An action which wraps the Slack chat.postMessage API method for posting to channels, private groups, and DMs.
This action sends messages using Slack bot tokens, which have two main advantages compared to user tokens and incoming webhooks:
Bots can't be disabled inadvertently when a Slack user is disabled or removed. Slack has written about this in a recent announcement, and
Bots offer a powerful range of capabilities that can be leveraged to perform more functions.

If you have a Drone continuous integration system in place you can use a webhook to trigger sending a Slack notification. This would work for the repository .drone.yml file:
- name: slack
image: plugins/slack
settings:
webhook: https://hooks.slack.com/services/...
channel: my_awesome_channel
username: Github Bot
when:
event: [ pull_request ]
branch: [ dev, master ]
template: |
New Pull Request Opened By: {{build.author}} ({{repo.name}} / {{build.branch}})
{{build.link}}
This assumes that drone is already enabled and the webhook configured in Github. If it isn't you can enable it through the Drone web dashboard by navigating to Repositories and clicking the green switch next to your repo name.

For custom github domain of company you need to use slack app (Githhub Enterprise Server) instead of Github App.
Once you are done with installation of Githhub Enterprise Server App on slack , Under configuration you will se payload url. Copy payload URL and go to github repository settings add webhooks sections and add payload url there and keep content type application/json

Related

Send github actions->CI result to Slack

I want to receive result of CI test in github actions to Slack.
Something like this:
Anybody knows how to do this or which tool should be used?
.github/workflows/verify.yml:
name: Verify CI
on: [push]
jobs:
tests:
name: Tests
...
You could use the slack-send GitHub Action to send data into Slack from your GitHub Workflow.
This Action can:
Send data to Slack's Workflow Builder (requires a paid Slack instance).
Send data via a Slack app to post to a specific channel (use an existing custom app or create a new one).
Send data via a Slack Incoming Webhook URL (use an existing custom app or create a new one).
There are a bunch of similar actions, for example, slack - GitHub Actions Slack integration - also simple and flexible Slack integration with GitHub Actions.

Repository-Webhooks vs Github Apps - which to use?

UseCase: My current use case is to react on new Issues and post automatic comments. This is mainly specific to one or two repositories.
Thus I guess webhooks on a repository as well as Github Apps can be used. Although Github has some really good documentation on both, I'm unsure which one to use for my scenario.
What advantages / disadvantages do they have when compared?
One advantage of Github Apps is that they don't need to add a WebHook to the repository according to this answer. - Not sure why that is an advantage.
Obviously a GitHub App can be installed in different repositories and also published in the marketplace. But I could also use the same URL for Webhooks in different repositories and thus mimmic that behavior with repository-webhooks to a certain extend.
What else?
The main difference is that while webhooks generate events and send them to a given URL Github. Apps have the right to access the repositories where you install them based on the permissions you give them.
If you have a webhook that fires every time an issue is updated you will receive an event at the URL you specified, but you will not have any rights to e.g. modify that issue in Github (e.g. through an API call).
If you instead have a Github App installed in your repository and the webhook URL set in the App settings, then the event will be sent to the URL as well. However, here's the difference: behind the webhook URL you will probably have a service that uses the Github App credentials (private key or temporary tokens) to make some modifications to your Github repo.
An example:
Webhook -> logging of Pull Request changes
Github App -> automate tagging of new pull requests
What else?
You can also use GitHub Actions. For your use case, there is the issues event (which is actually a webhook). It has several activity types, including opened:
on:
issues:
types: opened
This workflow only runs when an issue is created. Afterwards, you can use the REST API (e.g. via curl) to GET the newest issue and POST a comment.
As you said this is only relevent for "one or two repositories", I think it is fine to just copy & paste that workflow. If the number of repos grows, you may want to create a dedicated action.
Speaking of custom actions, there is also an existing Auto Comment action:
on: issues
jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: bubkoo/auto-comment#v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
issuesOpened: >
Your issue comment goes here.

Should GitHub Pages https:<moniker>.github.io/<repo> point to Read-the-docs build?

I have a GitHub repo of docs linked to my RTD account. The GitHub/RTD connection appears to be set up correctly with a functional webhook (green checks at both ends). Commits to the repo trigger a successful build on RTD as expected. RTD is an Authorized OAuth App and my RTD account is connected to my GitHub account.
Under Settings -> GitHub pages, I have tried both master branch and master branch/docs. I would expect https://moniker.github.io/repo-name/ to go to the connected RTD docs. But it just shows the repo-name/README.md file, not the repo-name.rtfd.io docs. Am I misunderstanding the linkage or the use of GitHub Pages? Or am I completely missing something? Thanks for any insight.
I put in a help desk question to GitHub and their response was:
GitHub Pages provides its own web hosting and will not integrate with external services. If you're publishing your site elsewhere GitHub Pages will not link to it.
So the answer is "no", adding the webhook only triggers a new build on commits and I do explicitly need a docs/index.html (or .md) that redirects to the RTD documentation.

Using a jenkins pipeline to build github pull requests

This seems to be one of the more important, but less understood and documented features of GH and Jenkins, which seems to defeat the purpose of CI entirely ...
I'm trying to setup a simple, "kick off a build when a pull request is opened" Job in Jenkins. I've scanned though dozens of posts, some several years old, but have yet to find a solution that actually works, start-to-finish. I'm using GH 2.15.4 and Jenkins 2.89.3 with the GH Pull Request Builder plug-in. I've tried setting up a pipeline job, and a freestyle project, following various "how to" posts and docs. Nothing works as expected. Not even close. I've managed to get some functionality working, but not without a lot of trial-and-error experimenting, which doesn't leave me with a warm-fuzzy in using it. Webhooks are equally worthless in accomplishing what is needed, as nothing seems to fire off a job in Jenkins, regardless of how it is configured.
For starters, are there any references or docs that explain what the following status checks are that are automatically created by Jenkins:
continuous-integration/jenkins/branch
continuous-integration/jenkins/pr-head
continuous-integration/jenkins/pr-merge
Apparently there is no way to delete or edit these ... they just "appear" courtesy of Jenkins. I've seen some discussions, but some explanations contradict one another, so they may just be outdated, inaccurate, etc. I want to know "what does what" so I know which should be 'required' as part of the PR status check.
Any pointers would be appreciated!
I agree, this problem is tricky because Jenkins and its plugins have changed so much throughout the years. Let me tell you how I accomplished what you're asking for. I will post all of my version numbers (mostly up to date).
Jenkins Version: 2.176.1 (java -jar /usr/share/jenkins/jenkins.war --version)
Plugins
github 1.29.4
github-api 1.9
github-branch-source 2.5.3
github-pullrequest 0.2.5
pipeline-github-lib 1.0
Also, I use the Blue Ocean interface, which might affect things (who knows)
blueocean 1.17.0
blueocean-git-pipeline 1.17.0
blueocean-github-pipeline 1.17.0
Github Webhooks
First off, set up webhooks for your system. This is a good guide for Github Webhooks
Go to your Github repository and click on the Settings tab. Then select 'Webhooks' in the left menu:
The URL of my Jenkins setup is https://jenkinsci.dorian.com. So, in the 'Payload URL' box, I put https://jenkinsci.dorian.com/github-webhook/
I left the settings as "application/json" and "send me everything" and "active"
The Webhooks area has a handy 'Recent Deliveries' section which can show you if your webhooks are making it to Jenkins. At first, I had the wrong URL so mine has red Xs next to them. Now, they're all green checkmarks.
Github Access Token
Many guides suggest that you provide Jenkins with a personal access token to communicate with your repo. To do that, go to your account avatar in the top right and select Settings -> Developer Settings -> Personal access tokens->Generate Token
Put whatever you want for the description. Under 'select scopes', if you just want it to work, select every checkbox in the list.
I selected:
repo:status
write:repo_hook
read:repo_hook
admin:org_hook
Click save and you'll be shown your secret key. Copy this somewhere safe (we'll use it soon).
Configuring Jenkins
Now for the hard part. Try and install all of the plugins I've listed above.
Go to Jenkins-Manage Jenkins->Configure System
Locate the Github section and click Add Github Server
Name: Github
Api URL: https://api.github.com
Manage Hooks: true
Under credentials, click "Add." You'll be brought to a menu. Select "Secret Text"
Scope: Global
Secret: paste your access token from earlier
ID: (I left this blank)
Description: DorianGithubCreds
Hit save. Then, select DorianGithubCreds from the credentials list.
To test, hit "Test Connection." Mine returns 'Credentials verified for user dnrahamim', rate limit: 4998
Now go down to Github Pull Request Builder
GitHub Server API URL: https://api.github.com
Jenkins URL override: (blank)
Shared Secret: (blank)
Credentials: DorianGithubCreds
Auto-manage webhooks: true
Everything else is blank
Configure Job
Go to the job that should be building your repo
Select 'Configure' in the left menu
Under Projects->Github Organization
Credentials: DorianGithubCreds
Owner: Dorian
Behaviors:
Discover branches
Strategy: Exclude branches that are also filed as PRs
Discover pull requests from origin
Strategy: Merging the pull request with the current target branch revision
Discover pull requests from forks
Strategy: Merging the pull request with the current target branch revision
Trust: From users with Admin or Write permission
Project Recognizers
Pipeline Jenkinsfile
Path: Jenkinsfile (my Jenkinsfile is in the project root)
Build Strategies: (intentionally blank)
Notifications
Now, when I do a pull request, or whenever I post a change to a pull request, Jenkins run a fresh build for that branch.
The Pull Request itself in Github also lists its "Status Checks" at the bottom. When the build is pending, the status check is yellow. If it succeeds it gets a green checkmark. If it fails, it gets a red X.
I initially had a problem because I had an old Jenkins box which was also configured to manage Github webhooks and build pull requests. Its builds were failing so its notifications were the ones getting through to my pull request. I fixed that problem by preventing that old Jenkins box from building my newest branches. Once the old Jenkins box stopped building, the notifications from the new Jenkins box made it through to Github.
Wrapping up
That's it! That's everything I intentionally configured. Please try out my configuration and see if it works for you. Let me know if I left anything out.
The hard part with configuring Jenkins with Github is that, although there are many guides, the suggestions often conflict with each other and very many are out of date (sometimes even official documentation is out of date).
Nonetheless, here are some Resources:
This is a good guide for Github Webhooks
Github's Whitepaper on CI with Jenkins (It doesn't explain much in detail but it gives a good overview)
Github pull request builder plugin
Semi-official SO post for showing build status on Github repo
GitHub has "add webhook" feature which is accessible in the repository settings --> Webhooks section. This has 4 inputs
1. Payload URl - Enter the Jenkins url
2. Content Type - application/json
3. Secret - this is optional if you want to configure secret
4. Which events would you like to trigger this webhook? - select the option "Let me select individual events"
Bunch of checkbox options are visible, check "Pull requests" option. This option will send a request to Jenkins when Pull request is opened
Next step is create a job in Jenkins which will listen to GitHub events and kick off the build

discord webhook for github organization

What I find really useful and awesome about discord is how easy is to setup a webhook. I have channels dedicated just to some repos I have on github, usually inside an organization.
I realised just today that in Github you add a webhook to an entire organization and that would be great for me. like this:
But after having set up the webhook, and successfully sent some test POST (through the github UI), I see no messages in the related channel.
Is it just me with this issue?
N.B.: It doesn't give me a error when I add the webhook for the Organization.
I just don't receive the message on Discord
EDIT: Okay. I restricted the issue to these actions. I don't see any messages for these actions:
Creating a repository
deleting a repository