| 1 | --- |
| 2 | tags: development-process |
| 3 | date: 2018-02-27 07:00 |
| 4 | title: The Hypothesis continuous release process |
| 5 | author: alexwlchan |
| 6 | --- |
| 7 | |
| 8 | If you watch [the Hypothesis changelog][changelog], you'll notice the rate of releases sped up dramatically in 2017. |
| 9 | We released over a hundred different versions, sometimes multiple times a day. |
| 10 | |
| 11 | This is all thanks to our continuous release process. |
| 12 | We've completely automated the process of releasing, so every pull request that changes code gets a new release, without any human input. |
| 13 | In this post, I'll explain how our continuous releases work, and why we find it so useful. |
| 14 | |
| 15 | [changelog]: https://hypothesis.readthedocs.io/en/latest/changelog.html |
| 16 | |
| 17 | <!--more--> |
| 18 | |
| 19 | ## How it works |
| 20 | |
| 21 | In the past, Hypothesis was released manually. |
| 22 | Somebody had to write a changelog, tag a new release on GitHub, and run some manual pip commands to publish a new version to PyPI -- and only David had the credentials for the latter. |
| 23 | |
| 24 | This meant that releases were infrequent, and features spent a long time in master before they were available to `pip install`. |
| 25 | The pace of development picked up in 2017 -- partly as new maintainers arrived, and partly groundwork for [David's upcoming (now started) PhD][phd] -- and we wanted to be able to release more frequently. |
| 26 | We decided to automate the entire release process. |
| 27 | |
| 28 | Now, when you create a pull request that changes the Hypothesis code -- anything that gets installed by pip -- you have to include a `RELEASE.rst` file which describes your change. |
| 29 | Here's an example from [a recent pull request][recent]: |
| 30 | |
| 31 | RELEASE_TYPE: patch |
| 32 | |
| 33 | This release changes the way in which Hypothesis tries to shrink the size of |
| 34 | examples. It probably won't have much impact, but might make shrinking faster |
| 35 | in some cases. It is unlikely but not impossible that it will change the |
| 36 | resulting examples. |
| 37 | |
| 38 | The first line says whether this is a major, minor, or patch release (using [semantic versioning][semver]). |
| 39 | The rest is a description of the changes in your patch. |
| 40 | |
| 41 | We have a test in CI that checks for this file -- any change to the core code needs a release file, even [fixing a typo][typo]. |
| 42 | If you need a release file but haven't written one, the tests fail and your pull request won't be merged. |
| 43 | |
| 44 | Sometimes we write a release file even if there aren't changes to the core code, but we think it's worth a release anyway. |
| 45 | For example, changes to the installation code in `setup.py`, or larger changes to our test code for the benefit of downstream packagers. |
| 46 | |
| 47 | Once you've written a release file and the pull request is merged into master, and after all the other tests have passed, our CI uses this file to create a new release. |
| 48 | |
| 49 | First, it works out the new version number, and updates it in [version.py][version.py]. |
| 50 | Then it copies the release description into the changelog, including the new version number and the current date. |
| 51 | For example: |
| 52 | |
| 53 | -------------------- |
| 54 | 3.44.25 - 2018-02-05 |
| 55 | -------------------- |
| 56 | |
| 57 | This release changes the way in which Hypothesis tries to shrink the size of |
| 58 | examples. It probably won't have much impact, but might make shrinking faster |
| 59 | in some cases. It is unlikely but not impossible that it will change the |
| 60 | resulting examples. |
| 61 | |
| 62 | These two changes are saved as a new commit, and that commit gets tagged as the new release. |
| 63 | The tag and the commit are pushed to GitHub, and then CI builds a new package and publishes it to PyPI. |
| 64 | |
| 65 | So with no very little extra work, every code change triggers a new release, and it's usually available within half an hour of merging the pull request. |
| 66 | |
| 67 | This exact system might not scale to larger teams. |
| 68 | In particular, you can't merge new features until the code in master has been released -- you get conflicts around `RELEASE.rst` -- so you can only merge one pull request at a time. |
| 69 | And in Hypothesis, we never backport bugfixes to old major or minor releases -- you'd need some changes to support that. |
| 70 | |
| 71 | But Hypothesis only has one full-time contributor, and everybody else works on it in their free time, we don't create patches fast enough for this to be a problem. |
| 72 | For us, it works exceptionally well. |
| 73 | |
| 74 | [phd]: http://www.drmaciver.com/2017/04/life-changes-announcement-academia-edition/ |
| 75 | [recent]: https://github.com/HypothesisWorks/hypothesis-python/pull/1101 |
| 76 | [semver]: https://semver.org/ |
| 77 | [typo]: https://github.com/HypothesisWorks/hypothesis-python/pull/1069 |
| 78 | [version.py]: https://github.com/HypothesisWorks/hypothesis-python/blob/master/src/hypothesis/version.py |
| 79 | |
| 80 | ## Why bother? |
| 81 | |
| 82 | Moving to continuous releases has been amazing. |
| 83 | |
| 84 | The big benefit is that nobody has to do manual releases any more. |
| 85 | Before we had this system, changelogs had to be assembled and written by hand, which meant reading the commit log since the last release. |
| 86 | This is both boring and prone to error -- in the past, a release might contain multiple changes, and it was easy to overlook or forget something in the changelog. |
| 87 | No more! |
| 88 | |
| 89 | Another benefit is that our releases happen much more quickly. |
| 90 | Every patch is available as soon as our tests confirm it's okay, not when somebody remembers to do a release. |
| 91 | If something's been merged, it's either available for download, or it will be very shortly. |
| 92 | |
| 93 | Releasing more often means each individual release is much smaller, which makes it much easier to find the source of bugs or regressions. |
| 94 | If somebody finds a bug, we can trace it to a specific release (and corresponding pull request), and there's a relatively small amount of code to inspect. |
| 95 | |
| 96 | Automation also makes our release process more reliable. |
| 97 | Manual steps have scope for error, and we've had a few dodgy releases in the past. |
| 98 | This process has cut over 100 releases near flawlessly. |
| 99 | |
| 100 | Finally, every contributor gets to make a release. |
| 101 | If you submit a patch that gets accepted, your change is available immediately, and it's entirely your work. |
| 102 | This may less of tangible benefit, but it gives off nice fuzzy feelings, especially if it's your first patch. |
| 103 | (Speaking of which, we're always looking [for new contributors][contributors]!) |
| 104 | |
| 105 | [contributors]: https://github.com/HypothesisWorks/hypothesis-python/blob/master/CONTRIBUTING.rst |
| 106 | |
| 107 | ## I'm ruined for everything else |
| 108 | |
| 109 | I've become used to code being available almost immediately after it's merged into master -- which isn't true for the vast majority of projects. |
| 110 | When I go to a repo with a bug report, see that a bugfix was merged two weeks ago, but there's yet to be a new release, it's hard not to feel a little impatient. |
| 111 | |
| 112 | I've started using this in my other repos -- both these scripts exactly, and derivatives of the same idea. |
| 113 | |
| 114 | If you'd like to try this yourself (and I'd really encourage you to do so!), all the scripts for this process are under the same MPL license as Hypothesis itself. |
| 115 | Look in the [scripts directory][scripts] of the main repo. |
| 116 | In particular, `check-release-file.py` looks for a release note on pull requests, and `deploy.py` is what actually cuts the release. |
| 117 | The code will probably need tweaking for your repo (it's closely based on the Hypothesis repo), but hopefully it provides a useful starting point. |
| 118 | |
| 119 | [scripts]: https://github.com/HypothesisWorks/hypothesis-python/tree/master/scripts |