Generate and Send changelog to your mail list after gitlab tags created

Mohammad Ranjbar Z
4 min readJul 17, 2020

There is always a concern for Tech Leads or Product owners to know what features (or bug fixes) deployed after every release, so for solving this problem I have decided to write a script to generate a changelog from commits ( if commits starts with these prefixes , [Feature], [Bug], [Enhancement]) .

For generating changelog I forked from this package https://github.com/CookPete/auto-changelog because this package gives you the Markdown file but I needed pdf file with my commit subject conventions, so I created this package https://github.com/mohammadranjbar/auto-changelog that generate the pdf of your changelog.

So this is not important whats is the stack of your project ( Python, JS, Java, Typescript, ….) you just need to add this script to root of your project with name : changelogMailer.js

you can see the latest version of changelogMailer.js here https://gitlab.com/mranjbar.z2993/gitlab-release-changelog/-/blob/master/changelogMailer.js

const nodemailer = require("nodemailer")
const moment = require("moment")
const {generateChangelogWithOptions} = require("adanic-auto-changelog")

/**
* There is something you should know that all variables you define in
* CI/CD variables automatically added to perocess.env so you can dont pass them
* from gitlab-ci to here because the will be here automatically
*/
const {
recipientsMailList, tag, deployerEmail,
appName, smtpHost, smtpPort,
smtpUser, smtpPassword, senderEmail,

//optional: issueUrl should be like https://issues.apache.org/jira/browse/{id}
issueUrl,

//optional: issue pattern should be like this for jira "[A-Z]+-\d+"
issuePattern

} = process.env;
console.log("environment variables", process.env)

if (!senderEmail || !recipientsMailList || !deployerEmail
|| !appName || !smtpHost || !smtpPort
|| !smtpUser || !smtpPassword || !senderEmail) {
throw new Error("These fields are required in process.env ," +
"recipientsMailList, tag,deployerEmail,\n" +
" appName, smtpHost, smtpPort,\n" +
" smtpUser, smtpPassword, senderEmail ")
}
const date = moment().format("YYYY/M/D HH:mm:ss")

const template = `
<div class="container">
<p>Hi everyone</p>
<p>The new version of ${appName} released</p>
<p><b>${tag}</b></p>
<p>Deployed by ${deployerEmail} at ${date}</p>
<p>Best regards</p>
</div>
`

const transport = nodemailer.createTransport({
host: smtpHost,
port: smtpPort,
secure: true,
auth: {
user: smtpUser,
pass: smtpPassword
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
});


async function mailChangelog() {
try {
const fullChangelogPath = __dirname + "/full-changelog.pdf"
const tagChangelogPath = __dirname + `/${tag}-changelog.pdf`
await generateChangelogWithOptions({
issueUrl,
issuePattern,
output: fullChangelogPath,
unreleased: true
})

await generateChangelogWithOptions({
issueUrl,
issuePattern,
output: tagChangelogPath,
unreleased: true,
tagPattern: tag
})

const subject = `${appName} ${tag} released`

const message = {
from: senderEmail, // Sender address
to: recipientsMailList,// List of recipients comma separated like "x@google.com, y@google.com"
subject,
html: template,
attachments: [
{path: `${tagChangelogPath}`},
{path: `${fullChangelogPath}`}

]
};

const result = await transport.sendMail(message)
console.log("Send email result ", result)
} catch (e) {
console.log("mailChangelog error ", e)
throw e
}
}

mailChangelog()

The above script uses my package that published on npm, after that you need to setup the CI/CD process to trigger above command on tags, so you should add the .gitlab-ci.yml

with below contents to the root of your project:

you can see the latest version of .gitlab-ci file here https://gitlab.com/mranjbar.z2993/gitlab-release-changelog/-/blob/master/.gitlab-ci.yml

ps: if you didn’t work with gitlab CI/CD you can check this https://docs.gitlab.com/ee/ci/quick_start

image: ubuntu

stages:
- changelog

changelog:
stage: changelog
only:
# Our release tags pattern is release-x.x.x if your tag pattern is different you can change the below regex
- /^release-[0-9]{1}.[0-9]{1}.[0-9]{1}$/
script:
- apt-get update
- apt-get install -y git
- apt-get install -y curl
#libfontconfig is a secret dependency for phantomjs and adanic-auto-changelog package
- apt-get install -y libfontconfig
- curl -sL https://deb.nodesource.com/setup_10.x | bash
- apt-get install -y nodejs
- npm i adanic-auto-changelog
- npm i nodemailer
- npm i moment
# Definitely you Should pass your country timezon as TZ variable in below line
- TZ=Asia/Tehran tag=$CI_COMMIT_TAG deployerEmail=$GITLAB_USER_EMAIL appName=$appName issueUrl=$issueUrl issuePattern=$issuePattern smtpHost=$smtpHost smtpPort=$smtpPort smtpUser=$smtpUser smtpPassword=$smtpPassword senderEmail=$senderEmail recipientsMailList=$recipientsMailList node changelogMailer.js

When you add this file to your project ,after every tag that match with this pattern release-x.x.x the above job will trigger and in this job the changelogMailer.js should be call and changelog will generate and send to your mailList,, but you should consider some things for setting this up:

  • Email have been sent to me from this way

If you have any question you can ask me from email mrajnbar.z2993@gmail.com , or my Linkedin https://www.linkedin.com/in/mohammad-ranjbar-z-74166b110

--

--