PYTHON-SEMANTIC-RELEASE(1) | python-semantic-release | PYTHON-SEMANTIC-RELEASE(1) |
python-semantic-release - python-semantic-release Documentation
Automatic Semantic Versioning for Python projects. This is a Python implementation of semantic-release for JS by Stephan Bönnemann. If you find this topic interesting you should check out his talk from JSConf Budapest.
The general idea is to be able to detect what the next version of the project should be based on the commits. This tool will use that to automate the whole release, upload to an artifact repository and post changelogs to GitHub. You can run the tool on a CI service, or just run it locally.
python3 -m pip install python-semantic-release semantic-release --help
Python Semantic Release is also available from conda-forge or as a GitHub Action. Read more about the setup and configuration in our getting started guide.
If you haven't done so already, install Python Semantic Release following the instructions above.
There is no strict requirement to have it installed locally if you intend on using a CI service, however running with --noop can be useful to test your configuration.
Create a variable set to the current version number. This could be anywhere in your project, for example setup.py:
from setuptools import setup __version__ = "0.0.0" setup(
name="my-package",
version=__version__,
# And so on... )
Python Semantic Release is configured using setup.cfg or pyproject.toml. Set version_variable to the location of your version variable inside any Python file:
setup.cfg:
[semantic_release] version_variable = setup.py:__version__
pyproject.toml:
[tool.semantic_release] version_variable = "setup.py:__version__"
SEE ALSO:
We rely on commit messages to detect when a version bump is needed. By default, Python Semantic Release uses the Angular style. You can find out more about this on Parsing of commit logs.
SEE ALSO:
If you already have a CHANGELOG.md, you will need to insert a placeholder tag so we know where to write new versions:
<!--next-version-placeholder-->
If you don't have a changelog file then one will be set up like this automatically.
SEE ALSO:
Some options and environment variables need to be set in order to push release notes and new versions to GitHub / GitLab:
Unless you disable upload_to_repository (or upload_to_pypi), Python Semantic Release will publish new versions to Pypi. Customization is supported using a ~/.pypirc file or config setting and environment variables for username and password/token or a combination of both. Publishing is done using twine.
Print the changelog to stdout.
If the option --post is used and there is an authentication token configured for your vcs provider (GH_TOKEN for GitHub, GL_TOKEN for GitLab, GITEA_TOKEN for Gitea), the changelog will be posted there too.
Figure out the new version number, update and commit it, and create a tag.
This will not push anything to any remote. All changes are local.
Print to standard output the new version number.
If the option --current is used, it will display the current version number.
It can be used to retrieve the next version number in a shell script during the build, before running the effective release, ie. to rename a distribution binary with the effective version:
VERSION=$(semantic-release print-version)
Publish will do a sequence of things:
Some of these steps may be disabled based on your configuration.
Every command understands these flags:
Force a patch release, ignoring the version bump determined from commit messages.
Force a minor release, ignoring the version bump determined from commit messages.
Force a major release, ignoring the version bump determined from commit messages.
Makes the next release a prerelease, version bumps are still determined or can be forced, but the prerelease_tag (see prerelease_tag) will be appended to version number.
No operations mode. Do not take any actions, only print what will be done.
Retry the same release, do not bump.
Override a configuration value. Takes an argument of the format setting="value".
Change the verbosity of Python Semantic Release's logging. See Showing debug output.
Add the following hook to your setup.py and you will be able to run python setup.py <command> as you would semantic-release <command>:
try:
from semantic_release import setup_hook
setup_hook(sys.argv) except ImportError:
pass
Getting a fully automated setup with releases from CI can be helpful for some projects. See Automatic releases.
Configuration options can be given in three ways:
semantic-release <command> -D <option_name>=<option_value>
Each location has priority over the ones listed above it.
The branch to run releases from.
Default: master
The file and variable name of where the version number is stored, for example:
semantic_release/__init__.py:__version__
You can specify multiple version variables (i.e. in different files) by providing comma-separated list of such strings:
semantic_release/__init__.py:__version__,docs/conf.py:version
In pyproject.toml specifically, you can also use the TOML list syntax to specify multiple versions:
[tool.semantic_release] version_variable = [
'semantic_release/__init__.py:__version__',
'docs/conf.py:version', ]
Similar to version_variable, but allows the version number to be identified safely in a toml file like pyproject.toml, using a dotted notation to the key path:
pyproject.toml:tool.poetry.version
Similar to version_variable, but allows the version number to be identified using an arbitrary regular expression:
README.rst:VERSION (\d+\.\d+\.\d+)
The regular expression must contain a parenthesized group that matches the version number itself. Anything outside that group is just context. For example, the above specifies that there is a version number in README.rst preceded by the string "VERSION".
If the pattern contains the string {version}, it will be replaced with the regular expression used internally by python-semantic-release to match semantic version numbers. So the above example would probably be better written as:
README.rst:VERSION {version}
As with version_variable, it is possible to specify multiple version patterns in pyproject.toml.
The way we get and set the new version. Can be commit or tag.
Default: commit
Defined the prerelease marker appended to the version when doing a prerelease.
Default: beta
Whether to create a tag for each new release.
Default: true
If this is set to true, semantic-release will create a new patch release even if there is no tag in any commits since the last release.
Default: false
If this is set to false, semantic-release will create a new minor release instead of major release when current major version is zero.
Quote from Semantic Versioning Specification:
If you do not want to bump version to 1.0.0 from 0.y.z automatically, you can set this option to false.
Default: true.
If this command is provided, it will be run prior to the creation of the release commit.
A comma-separated list of files to be included within the release commit. This can include any files created/modified by the pre_commit_command.
Import path of a Python function that can parse commit messages and return information about the commit as described in Parsing of commit logs.
The following parsers are built in to Python Semantic Release:
The default parser, which uses the Angular commit style with the following differences:
Parser for commits using one or more emojis as tags in the subject line.
If a commit contains multiple emojis, the one with the highest priority (major, minor, patch, none) or the one listed first is used as the changelog section for that commit. Commits containing no emojis go into an "Other" section.
See major_emoji, minor_emoji and patch_emoji. The default settings are for Gitmoji.
The original parser from v1.0.0 of Python Semantic Release. Similar to the emoji parser above, but with less features.
A parser for scipy-style commits with the following differences:
See scipy_parser for details.
Comma-separated list of emojis used by semantic_release.history.emoji_parser() to create major releases.
Default: :boom:
Comma-separated list of emojis used by semantic_release.history.emoji_parser() to create minor releases.
Default: :sparkles:, :children_crossing:, :lipstick:, :iphone:, :egg:, :chart_with_upwards_trend:
Comma-separated list of emojis used by semantic_release.history.emoji_parser() to create patch releases.
Default: :ambulance:, :lock:, :bug:, :zap:, :goal_net:, :alien:, :wheelchair:, :speech_balloon:, :mag:, :apple:, :penguin:, :checkered_flag:, :robot:, :green_apple:
If this is set to true with using the semantic_release.history.emoji_parser(), semantic-release will use human readable ASCII section headings in the changelog instead of the configured emoji.
Default: false
Parses commit messages using scipy tags of the form:
<tag>(<scope>): <subject> <body>
The elements <tag>, <scope> and <body> are optional. If no tag is present, the commit will be added to the changelog section "None" and no version increment will be performed.
While <scope> is supported here it isn't actually part of the scipy style. If it is missing, parentheses around it are too. The commit should then be of the form:
<tag>: <subject> <body>
To communicate a breaking change add "BREAKING CHANGE" into the body at the beginning of a paragraph. Fill this paragraph with information how to migrate from the broken behavior to the new behavior. It will be added to the "Breaking" section of the changelog.
Supported Tags:
API, DEP, ENH, REV, BUG, MAINT, BENCH, BLD, DEV, DOC, STY, TST, REL, FEAT, TEST
Supported Changelog Sections:
breaking, feature, fix, Other, None
Whether or not to commit changes when bumping version.
Default: True if version_source is commit, False for other values of version_source.
Git commit subject line. Accepts the following variables as format fields:
Variable | Contents |
{version} | The new version number in the format X.Y.Z. |
Default: {version}
Git commit message body. Accepts the following variables as format fields:
Variable | Contents |
{version} | The new version number in the format X.Y.Z. |
Default: Automatically generated by python-semantic-release
Author used in commits in the format name <email>.
Default: semantic-release <semantic-release>
NOTE:
Comma-separated list of sections to display in the changelog. They will be displayed in the order they are given.
The available options depend on the commit parser used.
Default: feature, fix, breaking, documentation, performance plus all the default emojis for semantic_release.history.emoji_parser.
A comma-separated list of the import paths of components to include in the changelog.
The following components are included in Python Semantic Release:
Only component displayed by default.
List of commits between this version and the previous one, with sections and headings for each type of change present in the release.
List of commits between this version and the previous one, dsplayed in a table.
Link to view a comparison between this release and the previous one on GitHub. Only appears when running through semantic-release publish.
If you are using a different HVCS, the link will not be included.
It is also possible to create your own components. Each component is simply a function which returns a string, or None if it should be skipped, and may take any of the following values as keyword arguments:
changelog | A dictionary with section names such as feature as keys, and the values are lists of (SHA, message) tuples. There is a special section named breaking for breaking changes, where the same commit can appear more than once with a different message. |
changelog_sections | A list of sections from changelog which the user has set to be displayed. |
version | The current version number in the format X.X.X, or the new version number when publishing. |
previous_version | The previous version number. Only present when publishing, None otherwise. |
You can should use **kwargs to capture any arguments you don't need.
The name of the file where the changelog is kept, relative to the root of the repo.
If this file doesn't exist, it will be created.
Default: CHANGELOG.md.
A placeholder used to inject the changelog of the current release in the changelog_file.
If the placeholder isn't present in the file, a warning will be logged and nothing will be updated.
Default: <!--next-version-placeholder-->.
If set to false, **scope:** (when scope is set for a commit) will not be prepended to the description when generating the changelog.
Default: True.
If set to false commit messages will not be automatically capitalized when generating the changelog.
Default: True.
Deprecated since version 7.20.0: Please use upload_to_repository instead
If set to false the pypi uploading will be disabled.
See Artifact Repository which must also be set for this to work.
Default: true
If set to false the artifact uploading to repository will be disabled.
See Artifact Repository which must also be set for this to work.
Default: true
Deprecated since version 7.20.0: Please use dist_glob_patterns instead
A comma , separated list of glob patterns to use when uploading to pypi.
Default: *
A comma , separated list of glob patterns to use when uploading dist files to artifact repository.
Default: *
The repository (package index) name to upload to. Should be a section in ~/.pypirc. The repositories pypi and testpypi are preconfigured.
Default: pypi
SEE ALSO:
The repository (package index) URL to upload the package to.
See Configuring distribution upload for more about uploads to custom repositories.
If set to false, do not upload distributions to GitHub releases. If you are not using GitHub, this will be skipped regardless.
The relative path to the folder for dists configured for setuptools. This allows for customized setuptools processes.
Default: dist/
Flag for whether the dist folder should be removed after a release.
Default: true
Command to build dists. Build output should be stored in the directory configured in dist_path. If necessary, multiple commands can be specified using &&, e.g. pip install -m flit && flit build. If set to false, build command is disabled and files should be placed manually in the directory configured in dist_path.
Default: python setup.py sdist bdist_wheel
The name of your hvcs. Currently only github and gitlab are supported.
Default: github
The domain url (without https://) of your custom vcs server.
The api url (without https://) of your custom vcs server.
If enabled, the status of the head commit will be checked and a release will only be created if the status is success.
Default: false
Git tag format. Accepts the following variables as format fields:
Variable | Contents |
{version} | The new version number in the format X.Y.Z. |
Default: v{version}
Do not use the default auth token to push changes to the repository. Use the system configured method. This is useful if the auth token does not have permission to push, but the system method (an ssh deploy key for instance) does.
Default: false
Set to * to get a lot of debug information. See Showing debug output for more.
See Environment checks.
Used to check if this is a Circle CI environment.
Used to check if this is a Frigg environment.
Used to check if this is a Semaphore environment.
Used to check if this is a Travis CI environment.
Used to check if this is a GitLab CI environment.
Used to check if this is a Jenkins CI environment.
Host component of the GitLab instance URL, without protocol and port. Example: gitlab.example.com
NOTE:
A personal access token from GitHub. This is used for authenticating when pushing tags, publishing releases etc. See Configuring push to Github for usage.
To generate a token go to https://github.com/settings/tokens and click on Personal access token.
A personal access token from GitLab. This is used for authenticating when pushing tags, publishing releases etc.
A personal access token from Gitea. This is used for authenticating when pushing tags, publishing releases etc.
Deprecated since version 7.20.0: Please use REPOSITORY_PASSWORD instead
Set an API token for publishing to https://pypi.org/.
Deprecated since version 7.20.0: Please use REPOSITORY_PASSWORD instead
Used together with PYPI_USERNAME when publishing to https://pypi.org/.
Deprecated since version 7.20.0: Please use REPOSITORY_USERNAME instead
Used together with PYPI_PASSWORD when publishing to https://pypi.org/.
Used together with REPOSITORY_PASSWORD when publishing artifact.
NOTE:
Used together with REPOSITORY_USERNAME when publishing artifact. Also used for token when using token authentication.
WARNING:
Custom repository (package index) URL to upload the package to. Takes precedence over repository_url
See Configuring distribution upload for more about uploads to custom repositories.
Print the changelog to stdout.
If the option --post is used and there is an authentication token configured for your vcs provider (GH_TOKEN for GitHub, GL_TOKEN for GitLab, GITEA_TOKEN for Gitea), the changelog will be posted there too.
Figure out the new version number, update and commit it, and create a tag.
This will not push anything to any remote. All changes are local.
Print to standard output the new version number.
If the option --current is used, it will display the current version number.
It can be used to retrieve the next version number in a shell script during the build, before running the effective release, ie. to rename a distribution binary with the effective version:
VERSION=$(semantic-release print-version)
Publish will do a sequence of things:
Some of these steps may be disabled based on your configuration.
Every command understands these flags:
Force a patch release, ignoring the version bump determined from commit messages.
Force a minor release, ignoring the version bump determined from commit messages.
Force a major release, ignoring the version bump determined from commit messages.
Makes the next release a prerelease, version bumps are still determined or can be forced, but the prerelease_tag (see prerelease_tag) will be appended to version number.
No operations mode. Do not take any actions, only print what will be done.
Retry the same release, do not bump.
Override a configuration value. Takes an argument of the format setting="value".
Change the verbosity of Python Semantic Release's logging. See Showing debug output.
The semver level that should be bumped on a release is determined by the commit messages since the last release. In order to be able to decide the correct version and generate the changelog, the content of those commit messages must be parsed. By default this package uses a parser for the Angular commit message style:
<type>(<scope>): <subject> <BLANK LINE> <body> <BLANK LINE> <footer>
The body or footer can begin with BREAKING CHANGE: followed by a short description to create a major release.
NOTE:
However, other tools may not do this, so if you plan to use any similar programs then you should try to stick to the official format.
More information about the style can be found in the angular commit guidelines.
See commit_parser.
If you think this is all well and cool, but the angular style is not for you, no need to worry because custom parsers are supported.
A parser is basically a Python function that takes the commit message as the only argument and returns the information extracted from the commit. The format of the output should be a semantic_release.history.parser_helpers.ParsedCommit object with the following parameters:
ParsedCommit(
version level to bump: major=3 minor=2 patch=1 none=0,
changelog section (see: :ref:`config-changelog_sections`),
scope of change: can be None,
(subject, descriptions...),
(breaking change descriptions...) )
The breaking change descriptions will be added to the changelog in full. They can and should also be included within the regular list of description paragraphs. The presence of a breaking change description will not implicitly trigger a major release.
If your parser is unable to parse a commit then it should raise semantic_release.UnknownCommitMessageStyleError.
The parser can be set with the commit_parser configuration option.
The key point with using this package is to automate your releases and stop worrying about version numbers. Different approaches to automatic releases and publishing with the help of this package can be found below. Using a CI is the recommended approach.
On publish, a few environment checks will run. Below are descriptions of what the different checks do and under what condition they will run.
Condition: Environment variable FRIGG is 'true'
Checks for frigg to ensure that the build is not a pull-request and on the correct branch. The branch check, checks against the branch that frigg said it checked out, not the current branch.
Condition: Environment variable SEMAPHORE is 'true'
Checks for semaphore to ensure that the build is not a pull-request and on the correct branch. The branch check, checks against the branch that semaphore said it checked out, not the current branch. It also checks that the thread state is not failure.
Condition: Environment variable TRAVIS is 'true'
Checks for travis to ensure that the build is not a pull-request and on the correct branch. The branch check, checks against the branch that travis said it checked out, not the current branch.
Condition: Environment variable CIRCLECI is 'true'
Checks for circle-ci to ensure that the build is not a pull-request and on the correct branch. The branch check, checks against the branch that circle-ci said it checked out, not the current branch.
Condition: Environment variable GITLAB_CI is 'true'
Checks for gitlab-ci to ensure that the build is on the correct branch. The branch check, checks against the branch that gitlab-ci said it checked out, not the current branch.
Condition: Environment variable JENKINS_URL is set.
Determines the branch name from either the environment variable BRANCH_NAME or the environment variable GIT_BRANCH, and checks to ensure that the build is on the correct branch. Also, if CHANGE_ID is set, meaning it is a PR from a multi-branch pipeline, the build will not be automatically released.
Add python setup.py publish or semantic-release publish as an after success task on your preferred Continuous Integration service. Ensure that you have configured the CI so that it can upload to an artifact repository and push to git and it should be ready to roll.
In order to upload to an artifact repository, Python Semantic Release needs credentials to access the project. You will need to set the environment variables REPOSITORY_USERNAME and REPOSITORY_PASSWORD. Use repository_url or REPOSITORY_URL to set a custom repository url. As an alternative the repository and/or credentials can be configured using the ~/.pypirc file.
WARNING:
SEE ALSO:
In order to push to Github and post the changelog to Github the environment variable GH_TOKEN has to be set. It needs access to the public_repo scope for public repositories and repo for private repositories.
This is for you if for some reason you cannot publish from your CI or you would like releases to drop at a certain interval. Before you start, answer this: Are you sure you do not want a CI to release for you? (high version numbers are not a bad thing).
The guide below is for setting up scheduled publishing on a server. It requires that the user that runs the cronjob has push access to the repository and upload access to an artifact repository.
virtualenv semantic_release -p `which python3`
pip install python-semantic-release
3. Clone the repositories you want to have scheduled publishing. 3. Put the following in publish:
VENV=semantic_release/bin $VENV/pip install -U pip python-semantic-release > /dev/null publish() {
cd $1
git stash -u # ensures that there is no untracked files in the directory
git fetch && git reset --hard origin/master
$VENV/semantic-release publish
cd .. } publish <package1> publish <package2>
/bin/bash -c "cd <path> && source semantic_release/bin/activate && ./publish 2>&1 >> releases.log"
Things to check...
If you are having trouble with semantic-release there is a way to get more information during it's work.
By setting the --verbosity option to DEBUG you can display information from the inner workings of semantic-release.
NOTE:
semantic-release changelog --verbosity=DEBUG
WARNING:
semantic-release changelog -v DEBUG
See #227.
Semantic Release
History
Represent a version number in a particular file.
The version number is identified by a regular expression. Methods are provided both the read the version number from the file, and to update the file with a new version number. Use the load_version_patterns() factory function to create the version patterns specified in the config files.
Because a pattern can match in multiple places, this method returns a set of matches. Generally, there should only be one element in this set (i.e. even if the version is specified in multiple places, it should be the same version in each place), but it falls on the caller to check for this condition.
This method reads the underlying file, replaces each occurrence of the matched pattern, then writes the updated file.
Because a source can match in multiple places, this method returns a set of matches. Generally, there should only be one element in this set (i.e. even if the version is specified in multiple places, it should be the same version in each place), but it falls on the caller to check for this condition.
This method reads the underlying file, replaces each occurrence of the matched pattern, then writes the updated file.
Because a source can match in multiple places, this method returns a set of matches. Generally, there should only be one element in this set (i.e. even if the version is specified in multiple places, it should be the same version in each place), but it falls on the caller to check for this condition.
This method reads the underlying file, replaces each occurrence of the matched pattern, then writes the updated file.
multiple versions are found.
Logs
Angular commit style parser
https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines
Commit parser which looks for emojis to determine the type of commit
When multiple emojis are encountered, the one with the highest bump level is used. If there are multiple emojis on the same level, the we use the one listed earliest in the configuration.
If the message does not contain any known emojis, then the level to bump will be 0 and the type of change "Other". This parser never raises UnknownCommitMessageStyleError.
Emojis are not removed from the description, and will appear alongside the commit subject in the changelog.
Commit parser helpers
Parses commit messages using scipy tags of the form:
<tag>(<scope>): <subject> <body>
The elements <tag>, <scope> and <body> are optional. If no tag is present, the commit will be added to the changelog section "None" and no version increment will be performed.
While <scope> is supported here it isn't actually part of the scipy style. If it is missing, parentheses around it are too. The commit should then be of the form:
<tag>: <subject> <body>
To communicate a breaking change add "BREAKING CHANGE" into the body at the beginning of a paragraph. Fill this paragraph with information how to migrate from the broken behavior to the new behavior. It will be added to the "Breaking" section of the changelog.
Supported Tags:
API, DEP, ENH, REV, BUG, MAINT, BENCH, BLD, DEV, DOC, STY, TST, REL, FEAT, TEST
Supported Changelog Sections:
breaking, feature, fix, Other, None
with descriptions) :raises UnknownCommitMessageStyleError: if regular expression matching fails
Legacy commit parser from Python Semantic Release 1.0
It expects a tag of some sort in the commit message and will use the rest of the first line as changelog content.
CI Checks
CLI
Edit in the source code, commit and create a git tag.
Write the new version number and commit it, unless the noop option is True.
Build and manage distributions
Custom Errors
Decorator which adds debug logging to a function.
The input arguments are logged before the function is called, and the return value is logged once it has completed.
HVCS
Gitea helper class
https://gitea.com/api/swagger#/repository/repoCreateStatus
https://gitea.com/api/swagger#/repository/repoCreateRelease
https://gitea.com/api/swagger#/repository/repoEditRelease
https://gitea.com/api/swagger#/repository/repoGetReleaseByTag
https://gitea.com/api/swagger#/repository/repoCreateReleaseAttachment
Github helper class
https://docs.github.com/rest/reference/repos#get-the-combined-status-for-a-specific-reference
https://docs.github.com/rest/reference/repos#create-a-release
https://docs.github.com/rest/reference/repos#update-a-release
https://docs.github.com/en/enterprise-server@3.5/rest/releases/releases#get-a-release
https://docs.github.com/rest/reference/repos#get-a-release-by-tag-name
https://docs.github.com/rest/reference/repos#upload-a-release-asset
Gitlab helper class
requests Authentication for token based authorization
Run commands prior to the release commit
Helper for using Twine to upload to an artifact repository.
Object that manages the configuration and execution of upload using Twine.
This object needs only one shared argument to be instantiated.
For known repositories (like PyPI), the web URLs of successfully uploaded packages will be displayed.
:returns True if successful, False otherwise.
:returns True if upload is enabled, False otherwise.
:returns Value of environment variable or None if not set.
Helpers to read settings from setup.cfg or pyproject.toml
VCS Helpers
The commit message will be generated from the configured template.
Rolf Erik Lekang
2022, Rolf Erik Lekang
October 23, 2022 | 7.32.2 |