Triggering Jenkins builds from Slack - jenkins

By following the tutorial below I am attempting to use slash commands in Slack to trigger a build in Jenkins. My jenkins instance which is on an EC2 is utilizing the user's API token to authenticate the POST command from slack.
When I do the curl command locally, it successfully triggers the build, the command is similar to this:
curl -X POST http://slack:c1c54d626f6a11fbc98ed795ec8862bc#10.11.12.13:8080/job/TEST_ATOMATION_GURU_SLACK_JOB_DEMO/build
However when I try to execute the command in slack via a slash command I get: Darn - that slash command didn't work (error message: 403_client_error).
Tutorial I am following: http://www.testautomationguru.com/jmeter-selenium-webdriver-how-to-trigger-automated-test-execution-from-slack/
I have also tried this tutorial with the same result:
https://sonnguyen.ws/how-to-trigger-a-jenkins-build-from-slack/
I also saw this stackoverflow post but it did not help: Using Slack to start Jenkins builds
Things I have tried:
- Opening up the security groups
- In jenkins selecting “Allow anonymous read access”
- In Jenkins deselecting “Prevent Cross Site Request
Forgery exploits”
Any help or guidance would be much appreciated.
enter image description here

The good news is that the setup should work - I also have a Slack slash command triggering a jenkins job, so the issue will be somewhere in your setup.
I see in your curl example you are accessing jenkins on a private IP. I would start there as if you are using the same private IP in the Slack slash command, there is no way Slack would be able to access your Jenkins instance. If you are using private IP in the slash command, I would recommend attaching an Elastic IP to your EC2 instance (assuming it lives in public subnet etc) and then try again the slash command but on the Elastic IP this time.

you can use ngrok for the tunneling and then you should be able to trigger that.
note: Ensure that the URL you are giving in the section should be able to trigger the build if run manually using the curl -X POST command.

Related

How to use an ssh Api Key in Jenkins

I wanted to know if you can help me with this problem, I am currently using Newman to load a collection of test cases that I made in Postman, I am trying to run a JOB in Jenkins, when I run the command:
newman run collection.json -e sendoment.json
It works, the collection runs in Jenkins but it shows me this error:
connect ECONNREFUSED 127.0.0.1:8095
I know that I have to pass the ssh code to it so that it recognizes the port and gives me access, but I don't know what commands to use to send it.
I currently have an id_rsa.pub that I use to perform this action locally.
My question is, how can I send this file or how can I make sure the ports will be there? I do not have privileges to enter the Managment, so I cannot add the variable as a plugin, I have seen this in other blogs either here or in Postman.
It is just a collection in json format with various calls to various EndPoints, where the states they have are validated.
To solve this action I had to create a method that would allow me to log in to the virtual machine that I have in Jenkins, since, when performing the action, this port is blocked. 2 Cookies are sent to perform the Log In action.

Error in triggering jenkins job from Slack

I am integrating Jenkins in Slack to facilitate build from Slack.
I configured the Build Trigger section of my job
Then I added Slash Commands to slack and configured it as below.
Now, I went to slack and typed /dev-backend, and it is throwing the error,
/dev-backend failed with the error "invalid_url"
I tried hitting the URL directly from postman which is giving the Authentication required response.
How can I add the authentication so that I can trigger build from slack? I thought the token provided in the build triggers section of Jenkins was enough.
I know it's kind of outdated question but I was also struggling with this error, so maybe it will help someone in the future. If you are getting /command failed with the error "invalid_url" and you are able to hit your application endpoint via eg. Bash shell:
curl -X POST -H 'Authorization: Bearer your-bearer-token' -H 'Content-type: application/json;charset=utf-8' --data '{"channel":"#test","text":"Hello, Slack!"}' http://127.0.0.1:8000/your/application/endpoint
it means that Slack can't access/see the URL that you passed in Slack API Applications Settings.
In my case the problem was that I was passing http://127.0.0.1:8000/some/endpoint in Slash Commands configuration which was visible only by my computer. The easiest way to make it visible for Slack is to use ngrok.
So if you are getting this error the steps are as follows:
Install ngrok
Run your app on localhost, eg. http://127.0.0.1:8000
In Bash shell type ngrok http 8000
Copy url returned by ngrok, in my case http://c609-91-234-49-229.ngrok.io
Paste copied URL to Slack Slash Commands configuration instead of localhost.

Github jenkins webhook integration failed

I am trying to configure webhooks in github so that it will build every time I do a new push, I have added web hooks in github and given the address of jenkins which is http://localhost:8080/github-webhook/ but it did not work and I found out that we need to find our IP address and I added it as follows: http://"my IPV4":8080/github-webhook/ and it still didn't work. I have even tried using ngrok to port forward, but it didn't work as well. It constantly throws the We couldn’t deliver this payload: Failure when receiving data from the peer error.
You need a public IP address for Github to point to detect a push request. localhost is on your local machine. You can configure jenkins on EC2 if you want to set it up for some testing.
You can use a proxy agent Ngrok.
Command :
ngrok http 8080
It will generate http://{some-number}.ngrok.io, copy the generated public address and put in your github repository webhook http://{some-number}.ngrok.io/github-webhook
At this point when you save it, it will send a post request to ngrok server, got redirected to localhost and you will see a green tick.
You can refer to this document for configuring the whole process.

No route registered for '/docker/hook'

I'm creating an Azure AppService based on a Docker image. The image is in Docker public registry, so I want the service to 'know' when there's a new version of the image (same tag).
I thought the WebHook under Continuous Deployment was to achieve that, but when I call it with curl I get the message from the subject.
I couldn't find the right doc... is that WebHook URL for what I think (hope) it is? is there a specific HTTP verb to use?
EDIT: I mean the WebHook URL found under Continuous Deployment in my Container Settings in Azure
I was stuck on this one for some time as well, until I realized that it requires POST HTTP request on that URL.
Here is an example of the CURL request that I have in my gitlab CI script
curl -X POST "https://\$$AZURE_DEPLOY_USER:$AZURE_DEPLOY_PASSWORD#$AZURE_KUDU_URL/docker/hook" -d -H
It does require to have set the following variables in the environment or you can replace it directly with your URL
$AZURE_DEPLOY_USER
$AZURE_DEPLOY_PASSWORD
$AZURE_KUDU_URL

Jenkins - SSH from Job's shell to jenkins

What i am trying seems quite simple, but i cant really get my head wrapped around what i need to do:
Inside a job, i have a build-step that executes shell-commands. This shell command tries using the Jenkins CLI.
So basically it says "java -jar theCliFile -s jenkinsURL / command
So inside of a job, i am trying to make a SSH connection to the jenkins-server itself.
This fails, console output says that "[WARN] Failed to authenticate with your SSH keys. Proceeding as anonymous
You must authenticate to access this Jenkins.".
I could provide a username and password as clear text inside of the shell, but i would like to avoid that.
What do i need to setup in order to allow an automatic authentication? I don't understand who needs what kind of keys.
The Jenkins CLI Wiki says to configured a /me/configure user. I just don't see how this makes sense. I guess this was meant to be for use cases where someone tries to build a SSH connection from a remote machine, not from the Jenkins itself, as in my case.
Actually, i solved it myself:
I had both a private and public key on the Jenkins-Server (incase you don't have those, just generate them on the server machine that the Jenkins runs on (there are tons of tutorials on how to do so out there)).
To allow what i was trying in my question, i had to take the public key and enter it in some user existing for the jenkins-application.
As far as i understand, entering the Jenkins Servers public key in any user will make the SSH request authenticate as this user and thus the shell will have the rights of said user.

Resources