How to control your deployments and versioning with semantic-release & friends
Tags: semanticversioning, versioncontrol, webdev, git
Published: 2019-07-08 17:39:46

---
title: How to control your deployments and versioning with semantic-release & friends
published: true
tags: semantic-versioning,version-control,web-development,git
canonical_url: https://blog.logrocket.com/never-guess-about-project-history-again-31f65091f668
---

_Never guess about project history again_

![](https://cdn-images-1.medium.com/max/1024/1*-0hawDLsHJI19yFsuGHU9Q.jpeg)

When it comes to productivity in software development, knowledge is power. The industry of project management tools — helping a development team to know where they stand — is vast. But software developers have one underused tool that can be leveraged to provide much more information —  **version control.**

A life of a software project is punctuated by _releases_. In open-source, they can be publishings to a package manager. In a software shop, typically we’re talking about deployments to a production server.

Whatever they are, the knowledge that is of great use when it comes to releases is _when_ they have happened and _what_ they included. In a big enough project it’s common to keep a change log and tag releases, but who has time for that with when you have to [release early and release often](https://en.wikipedia.org/wiki/Release_early,_release_often)?

Usually, it looks something like this:

The last release happened a week ago, and now that we’re done with a new, necessary feature, let’s do another one.

The question of what else changed and the size/impact of how big the overall changes remains unanswered. In essence, we simply hope that we did not forget about anything important.

[![](https://cdn-images-1.medium.com/max/1024/1*GPjaPKNNUYHU8EsA3Z0JGA.png)](https://logrocket.com/signup/)

### So let’s just automate all that

It all starts with a **commit message**. Structured commit messages will be the very basis of the automation process. Another git entity as use will be a **tag**  — which will mark each release. Based on the information in commit messages since last release, the next one will be prepared — or not, if the changes since last release won’t call for it. Neat, right?

_All that follows is available in this repository:_

[adekbadek/semantic-release-demo](https://github.com/adekbadek/semantic-release-demo)

#### [Step 1](https://github.com/adekbadek/semantic-release-demo/commit/7de4583e2f3288fcc55ec10190d40525988a660e): Structured commit messages

First off, a helping hand in structuring the commit messages: [commitizen](https://commitizen.github.io/cz-cli/). Install it with $ npm i -D commitizen and then set it up to use the [conventional changelog](https://github.com/conventional-changelog/conventional-changelog): $ npx commitizen init cz-conventional-changelog -D -E . Then add an npm script that will call git-cz (the binary for commitizen) and after staging the changes in git, run it. Now you should see this welcoming prompt:

![](https://cdn-images-1.medium.com/max/814/1*i-C_2JpIMgaRbVeaL3qa8g.png)

#### Step 2: Ensuring compliance with git hooks

Good. But working with other people and your future self means that someone might forget to use commitizen and wreck the whole setup. Fortunately, git provides a thing called [hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)! If you look into .git/hooks of a fresh repository, you’ll see a bunch of \*.sample files — hooks are simply bash scripts that will fire when certain actions occur. But who codes in bash when you have npm, right? Just $ npm i -D husky and watch that hooks folder populate with scripts that you don’t have to write. [Husky](https://github.com/typicode/husky) is a tool that does the hooks business based on config in package.json.

Now let’s get a linter for those commit messages. Firstly, install it — $ npm i -D [@commitlint](http://twitter.com/commitlint)/{config-conventional,cli}. Then, create the commitlint.config.js config file:

```
module.exports = {extends: ['[@commitlint/config-conventional](http://twitter.com/commitlint/config-conventional)']}
```

After that, provide some information for husky in package.json:

```
“husky”: {
  “hooks”: {
    “commit-msg”: “commitlint -E HUSKY\_GIT\_PARAMS”
  } 
}
```

That’s it! Try to add the files to git staging and commit with a gibberish commit message — husky’s gonna bite!

#### Step 3: Continuous Integration

Now we have a guarantee that the commit messages will be meaningful (or at least formatted). Time for the main protagonist of this tutorial: enter semantic-release: $ npm i -D semantic-release . Along with that, add an npm script that will invoke it (just “semantic-release”: “semantic-release”).

This tool is by default meant to publish on npm — if that’s not in your plans then some default-config-overriding will have to be performed. Add a “release” key in package.json with the following content:

```
"plugins": [
  "[@semantic](http://twitter.com/semantic)-release/commit-analyzer",
  "[@semantic](http://twitter.com/semantic)-release/release-notes-generator",
  [
    "[@semantic](http://twitter.com/semantic)-release/npm",
    {
      "npmPublish": false
    }
  ],
  "[@semantic](http://twitter.com/semantic)-release/github"
]
```

Semantic-release will do its work in GitHub (and npm), operating from a CI server — CircleCI in this case. It will thus need access to GitHub and npm via _tokens_, which have to be added to CI’s settings:

![](https://cdn-images-1.medium.com/max/1024/1*UU1v3xMShYEaK0jq2zix3w.png)<figcaption>(ignore the NPM_TOKEN if you don’t plan to publish on npm)</figcaption>

As you can see, the tokens need be available as environment variables named GITHUB\_TOKEN and NPM\_TOKEN. Now let’s add a config for CircleCI, called .circleci/config.yml— which is a bit bloated so you may just [copy it from here](https://github.com/adekbadek/semantic-release-demo/blob/master/.circleci/config.yml).

Everything is ready now! If CI sees a commit message that should trigger a release (like those starting with feat or fix), all will happen automatically. A release and tag will be published on GitHub and — unless configured differently — a new package version on npm.

Nice.

#### Step 4: Generating a _change log_

But, there is one problem. Two, actually. After a release, the version field in package.json has remained the same, and where is that change log that will be so helpful down the road? Don’t worry, it’s all two changes away:

First, install some additional packages: $ npm i -D [@semantic](http://twitter.com/semantic)-release/changelog [@semantic](http://twitter.com/semantic)-release/git. Then, add this config in “release” section of package.json:

```
"prepare": [
   "[@semantic](http://twitter.com/semantic)-release/changelog",
   "[@semantic](http://twitter.com/semantic)-release/npm",
   {
    "path": "[@semantic](http://twitter.com/semantic)-release/git",
     "assets": [
      "package.json",
      "package-lock.json",
      "CHANGELOG.md"
    ],
    "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
  }
]
```

This will tell semantic-release to generate a change log and then to add a commit after it’s done with the version releasing. You can tweak many more settings in this section, visit [semantic-release docs](https://semantic-release.gitbook.io/semantic-release/) for more info.

With that setup, a release will happen not on a whim, but based on the changes in code. And when it does, a change log will be generated so everyone knows _what_ landed in the subsequent releases and _when_ they happened.

### What do these version numbers mean?

As you probably noticed, a release is marked by _a version_. With semantic-release, the numbers in the version follow a system called [Semantic Versioning](https://semver.org/) (aka SemVer). In short, these denote **major** , **minor** , and **patch** versions. A patch number is incremented when backwards-compatible bugfixes are added, minor: backwards-compatible features, and major: breaking changes (incompatible with the versions before).

Semantic versioning is important for your users (or a client) — this way they know what can they expect with a new release and what has changed since they last used/seen the project. If the last version someone used is 1.0.1 and the current version is 1.1.42 then they know that there were many more bug fixes than features added in the meantime.

Semantic versioning can be opposed to [sentimental versioning](http://sentimentalversioning.org/), in which the version numbers are incremented in an incoherent manner, and in effect they don’t mean anything.

### Why not?

All that setup might seem like an overkill, and for a small project maintained by a single person it might be. But I have thanked myself many times for setting it up at the beginning and I’m sure I’ll use it again whenever I can. It’s just very reassuring to have that much information in git, where it won’t become outdated and is strictly tied to the code.

As for other qualms:

> I just need a glance at the git log diff and I’m good.

Well, a log is by nature not meant to be read in entirety. It has a very weak signal to noise ratio.

> Structuring commit messages is just annoying!

Having an unreadable commit history is even more. Try debugging with no information whatsoever about when a bug could have been introduced.

> I’m pretty used to meaningless commit messages…

Having to specify what the commit is about makes a developer think twice about the committed changes. If you’re unsure about the type of a commit, maybe the changes aren’t so clear either?

Thanks for reading!

### Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps

[![](https://cdn-images-1.medium.com/max/1024/1*s_rMyo6NbrAsP-XtvBaXFg.png)](https://logrocket.com/signup/)<figcaption><a href="https://logrocket.com/signup/">https://logrocket.com/signup/</a></figcaption>

[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

[Try it for free.](https://logrocket.com/signup/)

* * *