Slack message button: Dynamic Action URL (Request URL) - slack-api

I want to create a slack message button and I understand that when the user clicks this button, Slack performs a callback to your app's "action URL" (called "request URL" in app's Interactive Messages settings).
Can I somehow specify this URL dynamically? If not, how do people deal with staging/test servers, etc?

No, you can not provide / set the action URL by script. It has to be entered manually on the config pages for your Slack app.
I have 3 stages (Development, Test, Production) and the way I deal with staging is that I create one clone of my Slack app for each stage. That allows me to specify a different Action URL (and any other request URLs, like slash commands, interactive buttons etc.) for each stage.
Note that if you want to run all clones in the same Slack team you might need to rename some of your artifact, e.g. slash commands or bot users accordingly.
Example: For my Slack app rafflebot I use the slash command /raffle. That becomes /raffle_t for my test environment and /raffle_d for my development environment.

Related

Deploying jenkins jobs for dynamically created gitlab repos

Setup:
1 Jenkins server and 1 gitlab server that I own
Expected usage:
- We provision repositories for users on gitlab using some scripts when a user signs up with our portal.
- As soon as these users submit some code in the repository, we have to run some junit tests on them and email the users with a the test results in a clean way where they can see the passed and failed results with appropriate links which they can click on to get details of the tests.
Requirements for. jenkins:
1. Ability to send html junit test results with clickable test result links
2. Based on whether the junit test fails or passes, call two different python scripts.
Questions:
1. What is the best way to go about 1 and 2 requirements
2. Should I create a job template and create a job per dynamically created repo or can I use same job to run tests on multiple repos
References :
These were the links we referred.But as a newbie to jenkins couldn't come up with clear solutions.
1.https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin#Email-extplugin-Jellycontent
2.https://gist.github.com/nightspotlight/7cfa6af7c7989857f336f742cdcb443b
3.https://learn2automate.blog/2017/05/19/how-to-set-jenkins-job-status-based-on-test-case-pass/
Send some links in your mail using jenkins is simple
Install https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin
Install https://wiki.jenkins.io/display/JENKINS/Build+With+Parameters+Plugin in order to expose a job as link with params
Create some job in jenkins and in the post-build section add editable email notification
[![enter image description here][1]][1]
in In Default Content of editable email notification you must write the email body that will be sent to your users. If your requirement is send a link to allow user to open another jenkins page, you could write the following :
In order to execute python scripts click in this link
http://your-jenkins.com/job/my-job-python/parambuild/?param1=test1&param2=etc
Your mail provider will render this text as html.
Finally you need to create some job with name my-job-python with parameters param1, param2 or whatever. This job will execute your python scripts when user clicks on email links sent by jenkins.

Associate an external application url to jenkins build

I am trying to link or associate an external application url to jenkins job build.
So for every builds, this link or url should be available at the left side and any user can simply click on it to launch the external application.
So far I explored the following plugins for this:
https://wiki.jenkins.io/display/JENKINS/Associated+Files+Plugin - this associates a file or directory to the build , something similar to the html publisher plugin... not a url
https://wiki.jenkins.io/display/JENKINS/DocLinks+Plugin - similar to above , associate a doc from the build artifact directories
https://wiki.jenkins.io/pages/viewpage.action?pageId=52298023 - this Side bar link plugin can associate a external link to the jenkin project, but not to every builds
for me the external link which I am trying to associate to the build , dynamically changes for each build , basically some of the query parameter in the external link, so it's critical to associate it to every builds for tracking and other purposes . ex: the external link will be a like a small web app
http:hostnamexxx:port/somepage?para1=xxx&para2=yyyy
Please let me know if there is any plugin for this specific purpose or any other ideas
This would be the Anchor Chain plugin. I've played around with it a bit:
The docs state that the icon can be omitted. Then however you need to insert a tab character after the URL.
When inserting a link to an icon, it is always relative to Jenkins base URL.

Restrict slack-slash command access

I have automated the deployments in Jenkins from slack by slash commands.
I need give permission for slash commands or restrict the slash command access only to particular users (i.e) some members in the channel can deploy the dev environment by using /deploy_dev but they should not able to deploy to staging and production environments.
In order to restrict access to a custom slash command just check which user invoked the slash command in your script and then either execute the command or deny it (and reply with an appropriate message).
Slack is always providing the user ID and user name with the request, so that information is available in your scripts. See the example below for a command request from Slack: (from the official documentation)
token=gIkuvaNzQIHg97ATvDxqgjtO
team_id=T0001
team_domain=example
enterprise_id=E0001
enterprise_name=Globular%20Construct%20Inc
channel_id=C2147483705
channel_name=test
user_id=U2147483697
user_name=Steve
command=/weather
text=94070
response_url=https://hooks.slack.com/commands/1234/5678
To manage who has access I would recommend using private channels, so that a user has to be member of a specific private channel if he wants to execute a specific slash command. You can call groups.info to get the user IDs of all members of a private channel in your script.
Note that slash commands are always accessible to all users and there is no global configuration option in Slack, which would allows you to grant access to specific slash commands. So you have to do this in your script as detailed above.

Not able to access custom parameters passed to the jenkins build script

I want to add some parameters from the web hooks into the mail sent from the Jenkins. I tried solutions provided in StackOverflow and elsewhere. No success yet.
I have the following in place:
Checked This is a parameterized build
The build script looks like http://JENKINS_URL/job/android02/buildWithParameters?token=<My Token>&PARAM=<My Custom Params>
In the mail content, I try to access the custom parameters by ${PARAM}, $PARAM.
In the mail, however, I'm not getting the values I'm setting. If I set default values for PARAM, it is correctly displayed in the mail I receive. I tried http://JENKINS_URL/job/android02/build?token=<My Token>&PARAM=<My Custom Params> URL as well thinking just in case if it works. The mail is sent as Editable email Notification config.
Basically, everything is working except that I'm not able to access the custom parameters I have passed via Trigger builds remotely option.
Edit1:
If I keep a default value, say, test for the parameter, PARAM, in the mail received, I can see test displayed. But I need to get the value I'm passing in the build script.
From the "tool tip" that you get when you click on the question mark next to "":
${ENV,var="VARIABLENAME"}
Expands to an environment variable (specified here as VARIABLENAME) from the build environment. Note that this does not include any variables set by the build scripts themselves, only those set by Jenkins and other plugins.
So, use ${ENV,var="PARAM"}.

Modify notifications on a running build in jenkins

I occasionally want to get notified when a particular jenkins job that is building finishes. Is there any way to do this?
Scripting it through the API would be fine. I already have the jenkins IRC bot that notifies me of many things, so if I could just dynamically modify the running job build, that would be enough to do what I want -- I'm just having a hard time finding how to accomplish that.
AFAIK, you cannot change a job's config while it's running.
Here is an idea: Use a post-build step to check for an external resource status (like a file containing an action by text) and running an action based on the content of the file.
The external file can be modified while the build is running, so when the post-build is executed, it will follow the logic defined based on the content of the file.
I hope this helps.
You can use email notifier, It will send you an email
https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin

Resources