-
Notifications
You must be signed in to change notification settings - Fork 63
/
readme.md
166 lines (135 loc) · 8.49 KB
/
readme.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
![Build](https://github.com/PaulHatch/semantic-version/workflows/Build/badge.svg)
# Git-Based Semantic Versioning
This action produces a [semantic version](https://semver.org) for a repository
using the repository's git history without ever requiring a human to choose or
manually assign the version number.
This action is designed to facilitate assigning version numbers during a build
automatically while publishing version that only increment by one value per
release. To accomplish this, the next version number is calculated along with
a commit increment indicating the number of commits for this version. The
commit messages are inspected to determine the type of version change the next
version represents. Including the term `(MAJOR)` or `(MINOR)` in the commit
message alters the type of change the next version will represent.
# Background
Automatic versioning during a build presents a chicken-and-egg problem--we
want the version to increase by a single value between each release, but we
usually do not know at build time whether a new build will be released or not.
Generally a build is tagged as part of a release step after passing testing and
other quality controls, so if we want to use the version number in the build
itself, especially for a build triggered by a commit, we cannot rely on having
a proper tag for the build. Most CI systems offer a "build number", but this
does not correspond to our semantic version and relies on the state of the CI
tool. It is with this in mind that this tool was developed with the following
goals:
- Allow the version to be injected into the build
- Derive the version only from the git repository itself
- Do not require the version to be maintained by hand
- Resolve the version deterministically for a given commit (see caveats below)
- Provide an easy mechanism for incrementing major and minor versions by developers
To solve this problem, this action calculates the next _implied_ version based on
the most recently tagged version and the commit messages. An additional value called
the "increment" tracks the count of commits since the last version change, allowing
a label to be created to mark pre-release versions. The version produced by this
action is always the_implied version (unless `bump_each_commit` is set to `true`).
Subsequently tagging a commit that is chosen as the implied version is what bumps
the version for future commits.
![Commits Graph](versioning.drawio.svg?raw=true)
_Unless the current commit is already tagged, the version produced by this action will be one value ahead of the last tag._
## Major and Minor Versions
The commit messages for the span of commits from the last tag are checked for the
presence of the designated terms (`(MAJOR)` or `(MINOR)` by default), if a term
is encountered that commit is treated as the start of a major or minor version
instead of the default patch level. As with normal commits the implied version
will only increment by one value since the last tag regardless of how many major
or minor commits are encountered. Major commits override minor commits, so a set
of commits containing both major and minor tags will result in a major version
increment.
![Commits Graph](minor.drawio.svg?raw=true)
## Tags on Previous Commits
Adding a tag to an older commit changes the implicit version of commits since the
tagged commit. If a tag is assigned to an older commit, the commits that come after
it will be given the new version if the build were to be retriggered, for example:
![Commits Graph](tagging.drawio.svg?raw=true)
# Usage
<!-- start usage -->
```yaml
- uses: paulhatch/[email protected]
with:
# The prefix to use to identify tags
tag_prefix: "v"
# A string which, if present in a git commit, indicates that a change represents a
# major (breaking) change, supports regular expressions wrapped with '/'
major_pattern: "(MAJOR)"
# Same as above except indicating a minor change, supports regular expressions wrapped with '/'
minor_pattern: "(MINOR)"
# A string to determine the format of the version output
format: "${major}.${minor}.${patch}-prerelease${increment}"
# Optional path to check for changes. If any changes are detected in the path the
# 'changed' output will true. Enter multiple paths separated by spaces.
change_path: "src/my-service"
# Named version, will be used as suffix for name version tag
namespace: my-service
# Indicate whether short tags like 'v1' should be supported. If false only full
# tags like 'v1.0.0' will be recognized.
short_tags: true
# If this is set to true, *every* commit will be treated as a new version.
bump_each_commit: false
# If true, the body of commits will also be searched for major/minor patterns to determine the version type.
search_commit_body: false
# The output method used to generate list of users, 'csv' or 'json'.
user_format_type: "csv"
```
## Outputs
- *major*, *minor*, and *patch* provide the version numbers that have been determined for this commit
- *increment* is an additional value indicating the number of commits for the current version, starting at zero. This can be used as part of a pre-release label.
- *version* is a formatted version string created using the format input. This is a convenience value to provide a preformatted representation of the data generated by this action.
- *version_tag* is a string identifier that would be used to tag the current commit as the "released" version. Typically this would only be used to generate a Git tag name.
- *changed* indicates whether there was a change since the last version if change_path was specified. If no `change_path` was specified this value will always be true since the entire repo is considered. (It is possible to create a commit with no changes, but the Git cli rejects this by default and this case is not considered here)
- *authors* is a list of authors that have committed to this version, formatted as either csv or json.
There are two types of "version" string, one is the semantic version output that can be used to identify a build and can include prerelease data and metadata specific to the commit such as `v2.0.1-pre001+cf6e75` (you would produce this string yourself using the version information from this action plus whatever metadata you wanted to add), the other is the tag version string, which identifies a specific commit as being a specific version.
## Using Multiple Versions in the Same Repository
It is possible to create additional versions for multiple project co-existing
in one repository, for example you may have a Helm chart, database migration,
or simply be hosting multiple projects in the same repository and want them to
be versioned independently. There are a few settings that can be used to
accomplish this:
First, you can set the `change_path` input to specify a path that will be
inspected for changes. Commits which do no change any files in this path will
not increase the `increment` output. In addition, if there are no changes in
a given commit with this path specified, the `changed` value will be false.
Second, the input `namespace` can be set to create an additional named version.
If this value is set, it will be appended (separated by a hyphen) to the end of
tags for the version, and only tags with this value appended will be considered
when determining the version. The namespace will be pruned from the string
output as "version" within the action.
Finally, set different values for `major_pattern` and `minor_pattern` than the
other projects in order to be able to mark these commits independently.
To use secondary versions in a workflow, simply create additional steps in a
job referencing semantic version multiple times. For example, a project tagged
like `v1.2.3+0-db` could be configured like this:
```yaml
- name: Application Version
id: version
uses: paulhatch/[email protected]
with:
change_path: "src/service"
- name: Database Version
id: db-version
uses: paulhatch/[email protected]
with:
major_pattern: "(MAJOR-DB)"
minor_pattern: "(MINOR-DB)"
change_path: "src/migrations"
namespace: db
```
## Important Note Regarding the Checkout action
Beginning in v2, `actions/checkout` [does not include tags/history by default](https://github.com/actions/checkout/issues/100).
This history is required to determine the version correctly. To include the history
and tags, specify the fetch-depth parameter in your checkout action declaration. Specify
zero to pull the full history and tags.
```
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
```