Trigger pipe via slack command

Geekette
Geek Culture
Published in
3 min readMay 25, 2021

So this will be the start of my fun articles list since I am on Partial unemployment 🍻

Today we will make a quick slash command that will trigger a custom pipe and run it.

Why?

Honestly, I like to play with stuff like this when I am not in the mood, and also in my work, it seems like the developers are too lazy to run custom pipe or manual pipe in bitbucket

How?

It is really easy pips, I want to type /deploy2uat on slack and it triggers a custom pipe in the bitbucket pipeline

/deploy2uat: means slack API

trigger bitbucket pipeline: means bitbucket API

The first puzzle piece is Slack

Let’s create a slack application

  1. go here https://api.slack.com/apps/ and enter your application name and the workspace where you want the command to execute
create slack application

2. Click on the slash command and create one

Slash commands interface
3.Create slash command

Just type the name of the command, the request URL is where your backend API is running which means IP address or endpoint name... the rest is kind of clear what your command will do.

The second piece of the puzzle is the bitbucket API

Let’s see the bitbucket API

We will open the API documentation and as you can see the curl command is really simple to handle

curl -X POST -is -u username:apppassword \
-H 'Content-Type: application/json' \
https://api.bitbucket.org/2.0/repositories/usernameororg/repo/pipelines/ \
-d '
{
"target": {
"ref_type": "branch",
"type": "pipeline_ref_target",
"ref_name": "master"
}
}'

I will translate this command into a primitive python flask app ( 😜 not really )

import requestsimport osfrom flask import Flask, jsonifyapp = Flask(__name__)# The route of the application @app.route('/deploy-2-uat', methods=['POST'])def testmedium():# pattern is the name of your custom pipedata = ' { "target": { "ref_type": "branch", "type": "pipeline_ref_target", "ref_name": "master", "selector": { "type": "custom", "pattern": patternName } } }'response = requests.post('https://api.bitbucket.org/2.0/repositories/username/reponame/pipelines/', headers={'Content-Type': 'application/json',}, data=data, auth=(username, appapssword))return jsonify(text='It is deployed',)if __name__ == '__main__':app.run()

We can pass username, app password ( You can generate it on bitbucket), and repo name as environment variables and for the more standard command, I advise you to pass these variables as an argument of the slack command. ( I was just having fun here that is why I keep it simple)

The last step is to deploy this tiny script on some serverless option or since as I said I was just having fun I run the flask app and i use ngrok to have a public URL that we will enter in picture number 3 as a request URL and the result is

It is a basic really basic example but just thinks what you can do with a tiny first step like this, for example, a complete CI/CD maybe why not 😃

Peace from Tunisia and excuse my English ✌️

--

--

Geekette
Geek Culture

Manal lamine just a simple human ( you can call me geekette )