Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an email from GitLab CI pipeline's job?

I am trying to set up a GitLab CI configuration that sends an email after a pipeline's job completes with a link of the artifacts to the upload site. The pipeline builds based upon pom.xml, then tests with sonarqube and then uploads the artifacts using curl to a specific artifactory location. The folder structure and link of the artifact directory depends upon the CI_PIPELINE_ID. After all of these succeeds, I need to send this link for downloading the artifacts to a list of people via mail. My .gitlab-config.yml looks like the following:

image: maven:3.3.9-jdk-8

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "-U --batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  REPO_NAME: "<artifactory url>"

cache:
  paths:
    - .m2/repository
    - ./target/

stages:
  - build

compile_commit:
  stage: build
  only:
    - cr_integrate
  before_script:
    - git submodule sync --recursive
    - git submodule update --init --recursive --remote
  script:
    - mvn -f pom.xml -s settings.xml $MAVEN_CLI_OPTS clean install $MAVEN_OPTS
    - curl -i -u<username>:<token> -T "target/<artifact-1>.zip" "${REPO_NAME}/${CI_PIPELINE_ID}/<artifact-1>.zip"
    - curl -i -u<username>:<token> -T "target/<artifact-1>.zip" "${REPO_NAME}/${CI_PIPELINE_ID}/<artifact-2>.zip"
    - - curl -i -u<username>:<token> -T "target/<artifact-1>.zip" "${REPO_NAME}/${CI_PIPELINE_ID}/<artifact-3>.zip"
  tags:
    - <tagname>

How do I send a mail to some people after this with the link?

like image 991
Avi Bis Avatar asked Jan 12 '20 11:01

Avi Bis


People also ask

How do I find my GitLab email?

Click on Logs . Ensure that mg.gitlab.com is set as the domain above the activity graph. Enter the email address to be checked into the search bar, search, and then scan the results to see if mail is being delivered to that address. If email is delayed, respond to the user and ask them to wait.

How do I trigger a specific job in GitLab?

Adding a new triggerThe Add trigger button will create a new token which you can then use to trigger a rerun of this particular project's pipeline. Every new trigger you create, gets assigned a different token which you can then use inside your scripts or . gitlab-ci. yml .

How do I stop GitLab from sending emails?

Opt out of all GitLab emailsGo to the Notifications settings page. Clear the Receive product marketing emails checkbox. Set your Global notification level to Disabled. Clear the Receive notifications about your own activity checkbox.


1 Answers

I built a solution for this, sharing it here.

The following tools were used for this:

  1. GitLab release api
  2. Python-GitLab api
  3. Docker
  4. Microsoft Teams
  5. Sharepoint

The process flow can be outlined as follows:

  • A new pipeline is triggered
  • After successful build, codescan and publish, a release job is run
  • The release job uses a python script written with the help of python-gitlab api to create a release using gitlab release api. It inserts external artifactory links for downloading artifacts under release assets and adds links to release note and other documents.
  • GitLab sends a release mail to the appropriate notification channel, a group email id created by Microsoft Teams and Sharepoint, so that the entire team receives the release mail.

The python script is given below:

import os
import gitlab
from datetime import datetime

if __name__ == '__main__':
    access_token = os.environ['RELEASE_TOKEN']
    gitlab_url = os.environ['GITLAB_URL']
    project_id = int(os.environ['CI_PROJECT_ID'])
    
    tag_name = os.environ['CI_PIPELINE_ID']
    ref = os.environ['CI_COMMIT_REF_NAME']
    
    # artifactory_links
    artifactory_link = os.environ['ARTIFACTORY_PATH']
    group_name = os.environ['GROUP_NAME']
    project_name = os.environ['CI_PROJECT_NAME']
    directory = f'{datetime.now():%Y%m%d}'
    artifact_name = os.environ['ARTIFACT_NAME']
    package_type = os.environ['PACKAGE_TYPE']
        
    # artifacts_links
    artifacts_links = f'{artifactory_link}/{group_name}/{project_name}/{directory}/{artifact_name}-{tag_name}.{package_type}'
        
    # release note
    release_note = os.environ['RELEASE_NOTE']
    
    # authenticate with gitlab
    gl = gitlab.Gitlab(gitlab_url, private_token=access_token)
    gl.auth()
    
    # obtain the project object by id
    project = gl.projects.get(project_id)
    
    # creating the project tags
    project.tags.create({'tag_name': tag_name, 'ref': ref})
    
    # creating the project releases
    release = project.releases.create(
        {
            'name': f'Release for Pipeline ID {ref}',
            'tag_name': tag_name,
            'description': release_note,
            'assets': {
                'links': [{'name': artifact_name, 'url': artifacts_links}],
            }
        }
    )

The script requires the following environment variables:

  1. RELEASE_TOKEN – GitLab access token
  2. GITLAB_URL – GitLab base URL.
  3. ARTIFACTORY_PATH – Artifactory base URL.
  4. GROUP_NAME – In case the project is under a group.
  5. ARTIFACT_NAME – The artifact name
  6. PACKAGE_TYPE – Artifact package type
  7. RELEASE_NOTE – Link to release note and any other document.

These variables can be provided as GitLab CI variables. If there are more than one artifacts, the python script can be modified accordingly.

Since the python script needs to be called during the pipeline event and adding the script in the project would be modifying the project codebase, dockerizing the script is the best solution. That way, it can be pulled directly from docker hub. The dockerfile contents for this are as follows:

FROM python:3.7-alpine
COPY release_api.py /bin
RUN pip install python-gitlab
ENTRYPOINT ["/bin/release_api.py"]
CMD ["/bin/bash"]

In order to send a release mail to every member of the team, irrespective of their individual GitLab notification and subscription preferences, a team needs to be set up using Microsoft Teams. When a team is created in Teams application, a corresponding sharepoint site is created, along with a team email id. This set up takes some time. Once a team is created, under Files section, there’s an option to open it in sharepoint (screenshot below).

Open In SharePoint

The sharepoint site has a link in the left sidebar called Conversations. Once the sharepoint site is fully ready, clicking this link will open the inbox of the Teams email. Under the settings for the group, the option Edit Group can be found and there the group email id can be found. This group email id will be used to send the release mail to everyone in the team.

Under user settings of GitLab, the group email needs to be added. Once the mail is added and verified, the notification channel can be set up under Notifications. Once this is done, all notifications for that group (or project) will go to the group mail, and everyone in the team will get them. The last activity left is to set up notification preference to send a notification when a new release is available.

GitLab Notification Preference

like image 159
Avi Bis Avatar answered Sep 30 '22 18:09

Avi Bis