A notification has to be sent to the developer who made a commit to a configuration file, such that the retrofit of the file to the other repos would not be missed and there by avoid issues upfront. At present in git there is a feature for notification by using the “GIT Integrator service call email on push”. However, this service is not able to restrict the mail notification to a particular file change which is creating the issue as the inboxes are flooded with emails for every check-in.
Steps to achieve
- To configure an email notification for any specific file changes during git commit, we can use git tag “only”. Following is an example for the git script which is used for the same.
send_email: —-Git Stage
only: —-Git Command Only
– echo `git log -p -2 *.prop | head -50 > commit_history.txt`
if [[ -z “$REPO”]]; then
curl -s –user “api:$API_KEY” “https://api.mailgun.net/v3/$DOMAIN/messages” -F from=’Gitlab <firstname.lastname@example.org>’ -F to=$GITLAB_USER_EMAIL -F to=$GITLAB_USER_TLD -F to=$GITLAB_USER_NLD -F cc=$GITLAB_USER_TWO -F cc=$GITLAB_USER_PM -F subject=’Gate Property file Modified’ -F text=’Retrofit property files to other Repos’ -F attachment=’@commit_history.txt’
echo “It’s not allowed to trigger”
- In the above yml code snippet, it’s the “only” tag where we have condition for the execution of the script. Whenever there is a change in the property files then immediately it recognizes the change and triggers this job.
- An Email is sent using the mail gun service provider for the particular property file change. Without having our own SMTP Service provider, we can create an account in the Mail gun, it provides an API which can be hit with the required parameter such that it does the job of sending notification for us. It’s a pretty cool feature and easy to use. The service is ‘Free’ for a couple of months that is accompained with 5000 emails per month.
- Sample curl command to hit the email API:
curl -s –user “api:$API_KEY” “https://api.mailgun.net/v3/$DOMAIN/messages” -F from=’Gitlab < email@example.com >’ -F to=$GITLAB_USER_EMAIL -F to=$GITLAB_USER_TLD -F to=$GITLAB_USER_NLD -F cc=$GITLAB_USER_TWO -F cc=$GITLAB_USER_PM -F subject=’Property file Modified’ -F text=’Retrofit property files to other Repos’ -F attachment=’@commit_history.txt’
- Private Key and service domain from Mailgun are required to put them in yml. Below steps help us to find the following things from mail gun.
- Create an account with mail gun using the below link:
1. https://signup.mailgun.com/new/signup (Don’t click on the Add payment info now, we can sign up to the site without credit/debit card details.)
2. Provide all the sign-up details like email and name
3. Once login you will be able to see the below screen:
4. Once login you will be able to see the below screen:
5. Click on the domain then you will be able to see the domain name, also, in the right side we will have the recipients where we have to add the mail id of the person to whom the notification should be sent, currently, using free account we can only send it to 5 people.
6. Click on the setting and then API keys to get the private key:
7. All the variables are given in the GITLAB-UI under settings/ci-cd/variables as a global part.
- We can also use the smtp parameters instead of the API: sample for the same is below:
./swaks –auth \
–server smtp.mailgun.org \
–au firstname.lastname@example.org \
–ap 583627bfa8d4a292c4aa779f9b61aafb-ba042922-a9cb1219 \
–to email@example.com \
–h-Subject: “Alert!!! Property file changed” \
–body ‘check the commits’