How to send notification to Telegram from GitLab pipeline? - notifications

In our small startup we use GitLab for development and Telegram for internal communication between developers and PO. Since the PO would like to see the progress immediately, we have set up the GitLab Pipeline so that the preview version is deployed on the web server after each commit. Now we want to expand the pipeline. So that after the deployment a notification is sent via the Telegram group.
So the question - is that possible, and if so, how?
EDIT: since I've already implemented that, that's not a real question. I wanted to post the answer here so that others can use it as well.

So, we'll go through it step by step:
Create a Telegram bot
Add bot to Telegram group
Find out Telegram group Id
Send message via GitLab Pipeline
1. Create a Telegram bot
There are enough good instruction from Telegram itself for this:
https://core.telegram.org/bots#6-botfather
The instructions do not say anything explicitly, but to generate it, you have to go into the chat with the BotFather.
At the end you get a bot token, something like 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
2. Add bot to Telegram group
Switch to the Telegram group, and add the created bot as a member (look for the bot by name).
3. Find out Telegram group Id
Get the update status for the bot in browser:
https://api.telegram.org/bot<YourBOTToken>/getUpdates
Find the chat-id in the response:
... "chat": {"id": <YourGroupID>, ...
see for more details: Telegram Bot - how to get a group chat id?
4. Send message via GitLab Pipeline
Send message with a curl command. For example, an existing stage in gitlab pipeline can be extended for this purpose:
upload:
stage: deploy
image: alpine:latest
script:
- 'apk --no-cache add curl'
- 'curl -X POST -H "Content-Type: application/json" -d "{\"chat_id\": \"<YourGroupID>\", \"text\": \"CI: new version was uploaded, see: https://preview.startup.com\"}" https://api.telegram.org/bot<YourBOTToken>/sendMessage '
only:
- main
Remember to adapt the YourBOTToken and YourGroupID, and the text for the message.
*) we use the alpine docker image here, so curl has to be installed - 'apk --no-cache add curl'. With other images this may have to be done in a different way.

One easy way to send notifications (particularly if you're using multiple services or chats) is to use apprise.
To send to one telegram channel:
apprise -vv --body="Notify telegram chat" \
tgram://bottoken/ChatID1 \
This makes it easy to notify many services from your pipeline all at once without needing to write code against the API of each service (apprise handles this for you).
image: python:3.9-slim # or :3.9-alpine if you prefer a smaller image
before_script:
- pip install apprise # consider caching PIP_CACHE_DIR for performance
script: |
# Set a notification to multiple telegram chats, a yahoo email account,
# Slack, and a Kodi Server with a bit of added verbosity:
apprise -vv --body="Notify more than one service" \
tgram://bottoken/ChatID1/ChatID2/ChatIDN \
mailto://user:password#yahoo.com \
slack://token_a/token_b/token_c \
kodi://example.com

Related

Automatically add tasks to new Btibucket Pull request

We have several default tasks that we would like to add to every new pull request in Bitbucket. Since tasks are not supported by the Bitbucket API, the best thing I can think of is to automate it through a Selenium script, or other GUI tester. I'm thinking about kicking it off using a Jenkins job that watches for new pull requests. Can anyone think of a better way to do this?
I found that the tasks endpoint is not documented but is however active.
You can do the following
export PROJECT=yourProject
export REPO=yourRepo
export PR_ID=pullRequestId
export TASK_NAME=theNameOfTheTask
curl -H "Content-Type:application/json" -X POST --user "${TB_USER}:${TB_PASSWORD}" -d '{"content":{"raw":"${TASK_NAME}"}}' "https://api.bitbucket.org/2.0/repositories/${PROJECT}/${REPO}/pullrequests/${PR_ID}/tasks/"
You can also send a GET to get ALL tasks and their status
Thanks for the find!
Quick note, Bitbucket stopped supporting basic auth using your standard user/pass, you'll need to setup an AppPassword and use your normal Bitbucket username (no email).
https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/
Ex:
export PROJECT=yourProject
export REPO=yourRepo
export PR_ID=pullRequestId
export TASK_NAME=theNameOfTheTask
curl -H "Content-Type:application/json" -X POST --user "bt-username:bt-apppassword" -d "{\"content\":{\"raw\":\"${TASK_NAME}\"}}" "https://api.bitbucket.org/2.0/repositories/${PROJECT}/${REPO}/pullrequests/${PR_ID}/tasks/"

Is it possible to get a gcloud build logs using api call?

Is it possible to get build logs using api call ?
gcloud builds log BUILD_ID
I have to do it using my nodejs app
Thanks,
Yes.
The CLI command would be of the form:
BUILD_ID=[[SOME-BUILD-ID]]
gcloud logging read "resource.type=\"build\" resource.labels.build_id=\"${BUILD_ID}\" " \
--project=${PROJECT} ...
NB If you augment the above command with the global --log-http, the output will include details of the underlying API methods. This is a good way to map gcloud commands to APIs.
The underlying API is logging.googleapis.com/v2
A good approach is to build the filter using Logs Viewer:
https://console.cloud.google.com/logs/viewer?project=${PROJECT}&advancedFilter=resource.type%3D%22build%22
Or, if like me, you like playing with jq:
BUILD_ID=...
gcloud logging read "resource.type=\"build\" resource.labels.build_id=\"${BUILD_ID}\" " \
--project=${PROJECT} \
--limit=50 \
--format="json" \
| jq -r .[].textPayload
You may interact with any Google API using the wonderful and understated APIs Explorer. Here's API Explorer pre-selected with logging:
https://developers.google.com/apis-explorer/#search/logging/logging/v2/logging.entries.list
You mentioned using Node.JS, Google provides SDKs for all its services using a bunch of popular languages and runtimes, here's a page describing the Logging API with Node.JS examples:
https://cloud.google.com/logging/docs/reference/libraries#client-libraries-install-nodejs

Azure Container Instances stuck in "Creating" state

Whether I have the azure agent plugin for Jenkins make my container, or if I do it manually, it seems like either way it never enters a running state.
az container create \
--os-type Windows \
--location eastus \
--registry-login-server SERVER.azurecr.io \
--registry-password PASSWORD \
--registry-username USERNAME \
--image namespace/image \
--name jenkins-permanent \
--resource-group devops-aci \
--cpu 2 \
--memory 3.5 \
--restart-policy Always \
--command-line "-jnlpUrl http://host:8080/computer/NAME/slave-agent.jnlp -secret SECRET -workDir \"C:\\jenkins\""
I've gone through all the troubleshooting steps that apply, tried a different region, but to no avail.
Here's a current event that I got which seems to be the most progress I've had yet:
{
"count": 1,
"firstTimestamp": "2017-12-07T03:02:56+00:00",
"lastTimestamp": "2017-12-07T03:02:56+00:00",
"message": "Failed to pull image \"MYREPO.azurecr.io/my-company/windows-agent:latest\": Error response from da
emon: {\"message\":\"Get https://MYREPO.azurecr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout
exceeded while awaiting headers)\"}",
"name": "Failed",
"type": "Warning"
}
The funny thing is, this event happens before and after one case of the instance working (but unfortunately my entrypoint command was wrong, so it never started).
I really feel like Azure is punting on this and I just have no way to change the order I do anything. It's simply one command.
Alexander, here's a lead to actually check what could be causing the delay, or if the deployment has failed in the background, this information would be critical to narrow down what the issue is: https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-troubleshoot-tips#determine-error-code
From the article above check on deployment logs:
Enable debug logging:
PowerShell
In PowerShell, set the DeploymentDebugLogLevel parameter to All, ResponseContent, or RequestContent.
New-AzureRmResourceGroupDeployment -ResourceGroupName examplegroup -TemplateFile c:\Azure\Templates\storage.json -DeploymentDebugLogLevel All
or Azure CLI:
az group deployment operation list --resource-group ExampleGroup --name vmlinux
Check Also Check deployment sequence:
Many deployment errors happen when resources are deployed in an unexpected sequence. These errors arise when dependencies are not correctly set. When you are missing a needed dependency, one resource attempts to use a value for another resource but the other does not yet exist.
The above link contains more details. Let me know if this helps.
Figured it out, turns out the backslashes in the command in my executable path were not having their escapes honoured. Either because I was calling az from bash, or because something Azure side isn't handling the escaping correctly, or not escaping them itself.
My solution has been to just use forward slashes in the paths. Windows seems to be handling them correctly, and I prefer to not be bothered with its odd preference for backslashes.
Related to my issue is that the speed of the service makes troubleshooting very difficult. It takes a long time to go round trip with any fixes. So if you're using Azure Container Instances and want better performance, go upvote this feedback item that I've created.
How big is your image? You can always debug with 2 steps.
Run az container show -g devops-aci -n jenkins-permanent. It should contain a list of container events in the container json object. The event message should give you hint what's going on.
Run az container logs -g devops-aci -n jenkins-permanent. It should give you the logs of your container. If it's a problem within your image, you should be able to see some error output.

How to delete sensu client from web monitoring

Hi I am trying to delete sensu client from monitoring, I have removed the sensu pack and folder from client, still its not reflecting in web monitor, I tried curl and direct removed from the web monitoring still no go.
I have googled and tried deleting redis keys from redis-cli and still no go, the particular client is showing in web monitor, please guide me on how to delete it from monitoring.
curl -s -i -X DELETE http://<sensu-server>:4567/clients/<client-name>
The above will delete the client from sensu. This information was obtained here: Clients API - Sensu Docs - https://docs.sensu.io/sensu-core/1.4/api/clients/
may have a look at the documentation:
http://sensuapp.org/docs/0.11/faq#how-do-i-delete-a-client

Invoking bamboo plan remotely

I know it is possible to take down a bamboo artifact remotely, I was wondering is it possible to take start a bamboo from a remote box by sending an appropriate HTTP request?
Thanks,.
Here is an example:
curl --user un:pwd -X POST -d "stage&executeAllStages" -d "bamboo.variable.TEST=WORKS" http://10.0.0.0/rest/api/latest/queue/CAP-BR.json
As you can see I am also passing an optional value to Bamboo bamboo.variable.TEST=WORKS
If I understood your question right, then what you want to do is to trigger a build via the rest api of bamboo.
This could offer you some help:
https://answers.atlassian.com/questions/65517/trigger-bamboo-plan-via-rest-call