Send Gitlab-CI artifacts via e-mail - gitlab

Our Gitlab pipeline generates some performance graphs, which I would like to be sent to every team member via e-mail.
So far, they are marked as artifacts so Gitlab keeps them. Is there any way within Gitlab to achive this? Or should I do that within the job script?

There is no way currently to send artifacts via email from the gitlab interface. You will indeed have to send them from your job scripts.
Gitlab can send an email after a pipeline is finished (see in Settings>Integrations>Pipeline emails), but it doesn't attach artifacts.
Another way to share them would be to publish them in gitlab pages from your job script (doc here : https://docs.gitlab.com/ee/user/project/pages/index.html), but it wouldn't send an email.

It seems that a few years down the road nothing has changed yet (or I do not know about it).
send_email:
stage: notify
when: on_failure
script: curl -s --user "api:$MAILGUN_API_KEY"
"https://api.mailgun.net/v3/$MAILGUN_DOMAIN/messages"
-F from='Gitlab <gitlab#example.com>'
-F to=$GITLAB_USER_EMAIL
-F subject='Test results + report'
-F text='Testing some Mailgun awesomeness!'
-F attachment='#reports/report.html'
There are a few things you need to get this to work:
generate an artifact in another job (the file you want to upload; mine is reports/report.html)
define the variables MAILGUN_API_KEY and MAILGUN_DOMAIN
I needed something similar so here is a snippet from my pipeline.
I have also documented everything in a blog post. https://medium.com/#vdespa/send-gitlab-ci-reports-artifacts-via-e-mail-86bc96e66511
I hope this helps a bit.

Related

Gitlab webhook for sending notifications when image pulled from container registry?

Requirement - In my self hosted gitlab instance there are multiple projects maintained by different users which are all using one particular tag of an image from the container registry of my project. That tag is now outdated and I have created a new tag for the image and I would like to notify all the users to use the new tag
Is there any webhook available in gitlab which can be enabled for all PULL request of image:tag to send notifications (email,slack) to the authors of ci/cd pipelines?
Or maybe configure the pipeline to detect the image and tag being used and if it is the one in question then send notifications?
P.S. - Gitlab instance is using docker container registry
An approach that involves custom scripting. Less elegant than VonC's suggestion ;-)
… detect the image and tag being used and if it is the one in question then send notifications?
You could try tailing the logs while pulling the old tag manually.
Searching for the image & tag name in your log slice should help determine how the usernames of associated events can be parsed out. Probably with jq.
A custom script could then be added to regularly repeat that parsing and for example send an email to users who trigger those events.
"Webhook" ("custom HTTP callbacks") means a local listener to GitLab events.
Considering you are managing your GitLab instance, a better option would be to create a pipeline for external pull requests (since GitLab 12.3, Aug. 2019)
on-pull-requests:
script: echo 'this should run on pull requests'
only:
- external_pull_requests
This pipeline can check if a Dockerfile is being merged, and if that Dockerfile uses the wrong tag.
If it does, it can deny said pull request.

Using $CI_JOB_TOKEN Gives 404 Error On Triggering Pipelines

I am trying to trigger a pipeline using $CI_JOB_TOKEN. But it gives a 404 error everytime. Is there somebody could block CI_JOB_TOKEN from triggering a pipeline ?? at access levels ??
curl --request POST --form "token=$CI_JOB_TOKEN" --form ref=master https://gitlab.eample.com/api/v4/projects/73237/trigger/pipeline
For me using the CI_JOB_TOKEN also returned a 404 error for a private repository. When I instead executed the same command using a pipeline trigger token (Settings > CI/CD/ Pipeline triggers) it works as expected.
A similar problem is described in this issue https://gitlab.com/gitlab-org/gitlab/-/issues/17511
Just for clarification: You have to generate the token in the other project and then set it as a custom ci variable e.g. PIPELINE_TRIGGER_TOKEN in the project where you want to use it. Then in the curl request within .gitlab-ci.yml replace CI_JOB_TOKEN with PIPELINE_TRIGGER_TOKEN.
Could you please make sure the ref=master is correct? Recently master was changed to main your API call might be hitting a non-existent branch hence 404
Check also your GitLab version:
With GitLab 14.1 (July 2021), you have:
Default branch name redirect
Default branch name redirect
As part of the larger effort to rename Git’s default initial branch in a GitLab project from master to main, we are adding an automatic redirect to streamline the transition. Previously, when projects renamed the default branch, current URLs would result in 404 Not Found. This is a frustrating experience when you are trying to navigate between branches. Now, if you navigate to a file or directory path whose default branch was renamed, you will be redirected to the updated path automatically.
See Documentation and Issue.
So your problem might not exist with 14.1.

Is there a command or a "git log" option to retrieve comments/discussion logged for a merge request?

We have a GITlab(8.14) running for collaboration within the company.
I am working on a python script to collect information about merge requests being raised by developers across projects. I can very easily isolate the merge requests using 'git log'
git log --merges
However, I haven't been able to locate the correct command or option to retrieve all the discussion/comments taking place in the Merge Request.
Solution 1: use Gitlab Log System
Have you thought to use the Gitlab Log System instead of using a Git command?
It contains information about all performed requests.... Also you can see all
SQL request that have been performed and how much time it took.
Please take a look here https://docs.gitlab.com/ee/administration/logs.html
So in your Python script of collecting information, you can use queries like that:
SELECT <things> FROM "merge_requests" WHERE <condition>
Solution 2: use Gitlab API
Another way is to directly request Gitlab API to get a list of all notes for a single merge request.
Notes are comments on snippets, issues or merge requests.
like this:
GET /projects/:id/merge_requests/:merge_request_id/notes
The complete API reference for merge request notes is available here.
Does this help you?

Automatically create github repo, cool. But how to automatically do first commit

I'm writing a bash script to automatically setup a private github repo, as well as setup a deployHQ project for the same. This works well. However I can not add the GitHub repository to deployhq (via bash script using curl api calls) seemingly because I figure that there hasn't been an initial commit yet.
The idea of the script is to set everything up initially. From GitHub to deployhq to uploading deployhq ssh-rsa keys to the github repository - For each client website we begin to work on.
I'm using API keys, and in particular I'm using a personal access token to send commands to GitHub.
So my question is, how do I setup a new repository on GitHub with an initial commit, before the project has even started? (in order to satisfy deployhq)
I figure I need to use the API key to do this, as to avoid any user prompts.
It could just be a simple README.md file.
This is what I'm using so far (which works).
Note: obviously this is just part of the script.
SETUP_GITHUB_PROJECT_JSON='{
"name": "'$DHQ_PROJECT_PERMALINK'",
"description": "This project is a test",
"private": "true"
}'
SETUP=`curl -X POST -d "$SETUP_GITHUB_PROJECT_JSON" -H "$HEADER_ACCEPT" -H "$HEADER_CONTENT_TYPE" -u $GITHUB_API_KEY:x-oauth-basic "$GITHUB_BASE_URL"user/repos`
Happy to clarify as needed.

standard way of setting a webserver deploy using webhooks

I am working on code for a webserver.
I am trying to use webhooks to do the following tasks, after each push to the repository:
update the code on the webserver.
restart the server to make my changes take effect.
I know how to make the revision control run the webhook.
Regardless of the specifics of which revision control etc. I am using, I would like to know what is the standard way to create a listener to the POST call from the webhook in LINUX.
I am not completely clueless - I know how to make a HTTP server in python and I can make it run the appropriate bash commands, but that seems so cumbersome. Is there a more straightforward way?
Setup a script to receive the POST request ( a PHP script would be enough )
Save the request into database and mark the request as "not yet finished"
Run a crontab and check the database for "not yet finished" tasks, and do whatever you want with the information you saved into database.
This is definately not the best solution but it works.
You could use IronWorker, http://www.iron.io, to ssh in and perform your tasks on every commit. And to kick off the IronWorker task you can use it's webhook support. Here's a blog post that shows you how to use IronWorker's webhooks functionality and the post already has half of what you want (it starts a task based on a github commit): http://blog.iron.io/2012/04/one-webhook-to-rule-them-all-one-url.html

Resources