Disclaimer : I’m not affiliated with gitlab in anyway, or being compensated from any recommendations I make, in fact this tutorial with minimal tweaking can work with Bitbucket-CI, Travis-CI, Circle-CI and others.
If you you want to skip the discussion below and go straight to the instructions, scroll to where it says Let’s get you started.
So Google is cracking down on android apps background services.
No wonder that the bitcoin app I’m using did not alert me for the recent price dip.
There are many scenarios when you want a service to continuously check for a condition, and alert you when this condition is met. something like a webhook.
The first definition that pops up when you google webhook is:
A WebHook is an HTTP callback: an HTTP POST that occurs when something happens; a simple event-notification via HTTP POST. A web application implementing WebHooks will POST a message to a URL when certain things happen.
In the previous tutorial we have tackled running periodic jobs with gitlab.
In this very similar tutorial we will add conditions, we will create a webhook:
In another article we discuss in details how to connect this same project to IFTTT and have IFTTT alert us with a phone call, email, or tweet when the price of bitcoin reaches a specific range.
IFTTT allows you to connect to hundreds of services and smart appliances, list of IFTTT services
There are many ways for creating webhooks, but this is my favourite way because:
Bitbucket hosted CI free includes 50 min /month build time
5. Gitlab integrates the code repo with the build system, sure you can host your code on Github and build with Travis or Circle-CI, but I would like to login once and manage both the repo and the build process, and I don’t have to mess around with ssh keys to access another service.
6. Gitlab allows private repos : Travis CI allows free unlimited build minutes for personal projects, but the last I’ve checked Travis-CI only supports free builds for public repos.
7. Gitlab uses containers to allow users to create their build stack, so although for this tutorial we’re using a simple bash file, in reality you can specify a container that tests, builds and deploys your Android app, Java application using Maven or Gradle, Ruby, Node , Python, Django and a long list of applications, better yet Gitlab has ready standard templates to automatically set the build stack for you. I’ve forked a list of ready templates here.
So in essence you provide your code, and Gitlab-CI provides the right environment to build your code (you just tweak the YAML file template provided by Gitlab-CI). Here is the official Gitlab-CI examples page.
If you are into browser automation, there’s a cool tutorial by Joyz for using Gitlab-CI to automatically spin up a docker container ready with chrome-headless to run Selenium tests.
8. The project build file .gitlab-ci.yml is very similar to the configuration file used in many other services, including Travis-CI, Bitbucket-CI Circle-CI and others, even Jenkins has a YAML file plugin. this makes migration easy if the need arise.
9. Not that we used an open API to retrieve Bitcoin price, this does not mean that we are limited to APIs that does not require private key or token authentication.
Like other services you can store private keys as variables in Gitlab-CI, for example MY_API_PRIVATE_KEY , and then use ‘$MY_API_PRIVATE_KEY’ in your .gitlab-ci.yml file to authenticate when calling the API.
you can store private keys as variables in Gitlab-CI to authenticate API calls
if you do that make sure that you set your build Pipeline toprivate, otherwise other will be able to see your keys in plain text.
Let’s get you started:
Level : All levels
Requirements : Any web browser
2. Create a new Project : Click on New Project button to create a new repo, in the name field type btc-webhook or any other name. (9 seconds)
Then save it By clicking on Create Project (1 second)
3. Create a btc-price-alert.sh file in this new project : Click on New File, copy and paste the following snippet into the btc-price-alert.sh file then click save (10 seconds)
#!/bin/bashecho 'request Bitcoin price';x=$(curl https://min-api.cryptocompare.com/data/price?fsym=BTC\&tsyms=CAD)
echo 'removing all non digit from the response'x=${x//[^0-9\.]/}
echo 'Bitcoin price is CA$ '"$x"
echo 'removing decimals from the price'x=${x%.*}
echo 'checking if the price within the defined range'
if \[ "$x" -ge 10000 -a "$x" -lt 14000 \]; then
echo 'price is within range, will post an alert'
curl -i -X POST [https://putsreq.com/wkDdMQWhaOyalisaIe49](https://putsreq.com/wkDdMQWhaOyalisaIe49) --data 'price='"$x"
else echo 'Price is not within range, no alert posted this time'
fi
This will check if the price of bitcoin above CA$ 10,000 and less than CA$ 14,000 . feel free to customize it.
4. Create another file, call it .gitlab-ci.yml file : (20 seconds) Click on the home menu icon to go back to your project’s home page.
click on the plus icon next to your project’s name and select New File from the menu
copy and paste the following snippet into the .gitlab-ci.yml file then click save
test:
script:
this is a simple commands, to run the bash file that we’ve created
Click on the Commite changes button, this will trigger it to build and run.
5. Schedule it to check price everyday : click on the CI/CD icon to expand the menu, select Schedules to set up a name and a timer for your webhook to trigger. (11 seconds)
click on New schedule button
Type in a name for the new schedule daily-bitcoin-price-alert, select to run it daily, or whenever you want it to run, then click Save
Your scheduled job has been saved
6. Congrats! you’re done. if the price within the range you’ve specified, the webhook will post an alert. go to to this link in putsreq.com to see it in action. (10 seconds)
This job will run everyday as long as your free 2000/month build minutes do not run out.
You can find the sample code here and previous read the jobs build logs here
we can still kick it up a notch
If still want to integrate your webhook with IFTTT, this can be easily be done
This will be the topic of the coming tutorial