Automating Releases with semantic-release

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

There are so many repeated steps when releasing a new version of a library. The tool semantic-release automates this process by pushing off the responsibility of your releases to continuous integration. Trust us, this will change your workflow for the better.

[00:00] There are a couple of repeated steps when releasing a new version of a library. First you have to develop the new feature or the bug fix, and then you have to commit that and you have to tag that commit, and you have to publish it and you push it up to GitHub.

[00:15] There are a lot of different steps, and as programmers we don't like to repeat ourselves, so there's a tool that was developed called semantic release that makes releasing new versions of your software super, super easy. We're going to do that now with our library, Star Wars names.

[00:32] We'll run npm install -g semantic-release-cli. Now, with that installed, let's go ahead and run semantic-release-cli setup. This is going to take us through a prompt. Our GitHub repository is not private, with a capital N there, that's the default, so we'll just hit enter.

[00:58] Our npm repository or registry is just the normal one, so I'll go with the default there. It read my npm username and my npm email. It got my GitHub username, and now I'm going to choose what continuous integration service I'm using.

[01:17] The way that semantic release works is that it will actually release your software or your library during continuous integration. We haven't set up any continuous integration, but semantic release will actually set it up for us and will initialize things for us.

[01:35] We're going to stick with Travis CI. If you don't want to use Travis CI, you want to use Snap or Codeship or something else, then you can just print the tokens and then you can use those and set those in your continuous integration. We'll go ahead and stick with Travis CI.

[01:51] What semantic release is doing is it's creating a travis job for our library and adding a couple of things to our project. For us, we're just going to stick with a single node version in our travis.yml, and now let's go ahead and look at our project.

[02:11] There are a couple of new things in here. First you'll see the travis.yml is new. We'll look at that in a moment. Then you'll also have package.json. Let's go ahead and look at some of the changes here.

[02:21] The first thing you'll notice is we have removed the version from our package.json. This isn't really that big of a deal because when we actually release, then travis will do the releasing and semantic release will actually add the version to our package.json right before it releases.

[02:43] We don't have to worry about what version our library is in. We're just using some conventions with our commit messages to specify the types of changes that we're making, and then semantic release is able to determine the version based off of the changes that are in our Git history.

[03:00] I'm going to actually restore the version and just set it to 000and say it's semantically released. The reason why I'm doing that is when I run npm install, npm will give me a warning if my package doesn't have a version, so I'm putting that back in there.

[03:18] With the scripts, it's adding a script semantic release, and it's calling semantic release pre, and then it is doing the npm.publish for us, and then semantic release post. Semantic release does a lot of magic under the covers to make sure that things are ready for when this npm.publish happens, like setting the version properly. It's also updating our url to be https url, and it's adding the semantic release dependency to our dev dependencies.

[03:51] Now let's go ahead and take a look at the travis.yml. It's specifying we don't need sudo, it's specifying our language, setting up the cash so we don't install our known modules over and over again. We don't need a notification, or we can set that up if we want to.

[04:09] It's saying before install, go ahead and install the latest version of npm at 2.0Before we run our script we'll npm prune, and then after we run our script, after our script is successful, then we run the semantic release script that semantic release created for us here.

[04:28] Now we're going to go ahead and configure travis to make sure that we run the build before we actually release. We don't want to do any sort of releasing before we verify that our tests are working.

[04:43] Let's go ahead and add a script here with npm.runtest. Now our test will run, and if they fail, then this after success will never run and we won't release a new version. However if they do succeed, then we will release a new version, and semantic release will find out what version it should be based off of our git commits, and we'll talk about that in the next lesson.

[05:08] Just to review, what you need to do is you run npm.install -g globally, semantic-release-cli, and then setup. That'll take you through this prompt here. Depending on what CI you decide to use, it will create a travis.yml if you choose travis.

[05:29] Here we have the travis.yml here. We added this script here to make sure that we are running our test so we don't push out a new release if our tests are breaking. Semantic release added this script which it's using in the after success.

[05:44] That is how you set up your repository to be released with semantic release.

Juanmnl Cornejo
Juanmnl Cornejo
~ 9 years ago

I'm having a problem on travis build. It stalls while launching the tests. I'm sure the -w flag is responsible so i removed it. I've pushed the changes to github, but when relaunching the build on travis, the test command still has the -w flag. How do i update the github project on travis. (tried removing and adding again w/ no success)

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

I actually recommend that your test script not have the -w flag (even though that's what I originally did in the video) and in your .travis.yml, make sure that your script runs npm run test. I do in angular-formly

Good luck!

Juanmnl Cornejo
Juanmnl Cornejo
~ 9 years ago

Hey! Thanks for the quick answer! Everything worked after waiting a bit (the new commit arrived and everything went green). Guess i'll wait for the commit to propagate next time :)

Leo
Leo
~ 8 years ago

Hi Kent, thanks a lot for sharing this amazing library. I just have 2 steps that I'm struggling with.

  1. I use gulp-header that reads my package.json to insert the correct version as annotations in my .min.js, however, semantic-release don't let any version into the package.json, do you have an alternative, for it? Thanks?

  2. How would publish bower? Do I need to create a tag on my own? Thanks

Kent C. Dodds
Kent C. Doddsinstructor
~ 8 years ago

Hi Leo! So... Two things

  1. Just run gulp-header after semantic-release pre. You'll see that I changed the semantic-release script in angular-formly here. semantic-release pre will set the version in package.json.
  2. Don't publish to bower. It's dead. However, semantic-release does create a tag for you, so it should just work out of the box.
Leo
Leo
~ 8 years ago

Hi Kent,

Many thanks for your reply.

  1. Allright, I'll run semantic-release pre, than gulp-header. That's really cool. I just need to make sure all my PR helpers use the correct commit messages convention. (I found out you wrote a lib to force that, I'll try it)

  2. I've read about replace bower to npm, however this won't be done to quickly. I'm still have many users from bower in my lib. I realized that created a tag. It should be fine though.

Awesome video btw!

Jakub Antolak
Jakub Antolak
~ 6 years ago

Hi! Each time I run semantic-realease with --publish flag, I get the GitHub token error although the token is set. However, the script without this flag creates correct releases on GitHub while running in CI. But it misses to publish the package on npm so I have to set the version in package.json manually...

Joonas Kallunki
Joonas Kallunki
~ 5 years ago
Babs Craig
Babs Craig
~ 5 years ago

In case anyone runs into this error: ERR! semantic-release Error: Unable to find repo id for "{REPO_NAME_HERE}" after selecting TravisCI in the setup, you might want to sign up for TravisCI first and then add the repo to your TravisCI dashboard.

Markdown supported.
Become a member to join the discussionEnroll Today