Sending a Pushover Notification at the end of a GitLab CI pipeline

Pushover

Yes, GitLab already has the ability to send Pushover notifications after a GitLab CI job, but If you’re like me, you want to have a little more control over those messages.

When I realized the standard Pushover notifications were lacking, I went over to Pushover’s website and researched different ways that notifications can be pushed through their platform. The easiest example they provided was via a simple curl command. This command can be easily integrated into any .gitlab-ci.yml file.

The sample command they provide is below:

curl -s \
  --form-string "token=abc123" \
  --form-string "user=user123" \
  --form-string "message=hello world" \
  https://api.pushover.net/1/messages.json 

Integrating this script into our .gitlab-ci.yml file is simple! But first, we need to login to the Pushover dashboard and grab our user key and API token. First, we will need our user key which is displayed when we first login to the Pushover dashboard. If we scroll down to the bottom of the page, there is a section labeled “Your Applications”. Click “Create an Application/API Token” and after that process, the application token will be displayed.

Once we have our user key and API application token, we can add a stage to our .gitlab-ci.yml file. Below is our new notify stage:

notify_success:
  stage: notify
  image: alpine:latest
  before_script:
  - apk update && apk add curl
  allow_failure: true
  only:
    - master
  script:
    - curl -s --form-string "token=${PO_API_TOKEN}" --form-string "user=${PO_USER_KEY}" --form-string "message=${PO_SUCCESS_MESSAGE}" https://api.pushover.net/1/messages.json

The above job will send a Pushover notification to us when the GitLab CI pipeline finishes running and is successful. The stage uses the Alpine Linux base Docker image because it is small and easy to use with simple shell commands. before_script adds the curl application to the image. The curl command that talks to the Pushover platform is located under script. This command has been modified with the following variables:

VariableDescription
PO_API_TOKENThe API Token that you received when creating a Pushover application.
PO_USER_KEYThe Pushover user token from your Pushover dashboard.
PO_SUCCESS_MESSAGEThis is the message that is pushed to your device when the pipeline is successful.

The above variables need to be defined in the Settings -> CI/CD -> Variables section of your repository. More information about variables here. But what if the pipeline fails? We can add a second job in the stage called notify_fail and the when: on_failure parameter. The below job will send a different notification through pushover when a pipeline fails:

noitfy_fail:
  stage: notify
  image: alpine:latest
  before_script:
  - apk update && apk add curl
  allow_failure: true
  only:
   - master
  when: on_failure
  script:
   - curl -s --form-string "token=${PO_API_TOKEN}" --form-string "user=${PO_USER_KEY}" --form-string "message=${PO_FAIL_MESSAGE}" https://api.pushover.net/1/messages.json

The above job contains the following variables:

VariableDescription
PO_API_TOKENThe API Token that you received when creating a Pushover application.
PO_USER_KEYThe Pushover user token from your Pushover dashboard.
PO_FAIL_MESSAGEThis is the message that is pushed to your device when the pipeline FAILS.

Putting everything all together, the stage looks like this:

notify_success:
  stage: notify
  image: alpine:latest
  before_script:
  - apk update && apk add curl
  allow_failure: true
  only:
    - master
  script:
    - curl -s --form-string "token=${PO_API_TOKEN}" --form-string "user=${PO_USER_KEY}" --form-string "message=${PO_SUCCESS_MESSAGE}" https://api.pushover.net/1/messages.json

noitfy_fail:
  stage: notify
  image: alpine:latest
  before_script:
  - apk update && apk add curl
  allow_failure: true
  only:
   - master
  when: on_failure
  script:
   - curl -s --form-string "token=${PO_API_TOKEN}" --form-string "user=${PO_USER_KEY}" --form-string "message=${PO_FAIL_MESSAGE}" https://api.pushover.net/1/messages.json

This is a great way to receive detailed push notifications from each one of our Gitlab CI pipelines.

Be the first to comment

Leave a Reply

Your email address will not be published.


*