-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM python:3-slim AS builder | ||
ADD . /app | ||
WORKDIR /app | ||
|
||
# We are installing a dependency here directly into our app source dir | ||
RUN pip install --target=/app packaging | ||
|
||
# A distroless container image with Python and some basics like SSL certificates | ||
# https://github.com/GoogleContainerTools/distroless | ||
FROM gcr.io/distroless/python3-debian10 | ||
COPY --from=builder /app /app | ||
WORKDIR /app | ||
ENV PYTHONPATH /app | ||
CMD ["/app/main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Parse Semver Action | ||
|
||
A [GitHub Action](https://github.com/features/actions) for parsing a semver string into a parts and return if the version is a pre-release. This action uses the Python [`packaging`](https://packaging.pypa.io/en/latest/) library for parsing version string. Version strings can be prefixed be `v` (ex. `v1.4.2`). | ||
|
||
Python semver does not contain dashes (`1.2.3rc1`) while other semver docs do (`1.2.3-rc1`). This returns both, with default being the python compliant version. | ||
|
||
Example usage: | ||
|
||
```yaml | ||
name: Example Workflow for Parse Semver Action | ||
on: push | ||
jobs: | ||
parse: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Parse Semver | ||
id: parse-valid | ||
uses: dbt-labs/actions/parse-semver | ||
with: | ||
version: "1.2.3rc4" | ||
|
||
- name: Use Parsed Output | ||
run: | | ||
echo version: ${{ steps.parse-valid.outputs.version }} | ||
echo base-version: ${{ steps.parse-valid.outputs.base-version }} | ||
echo major: ${{ steps.parse-valid.outputs.major }} | ||
echo minor: ${{ steps.parse-valid.outputs.minor }} | ||
echo patch: ${{ steps.parse-valid.outputs.patch }} | ||
echo pre-release: ${{ steps.parse-valid.outputs.pre-release }} | ||
echo pre-release-version: ${{ steps.parse-valid.outputs.pre-release-version }} | ||
echo pre-release-type: ${{ steps.parse-valid.outputs.pre-release-type }} | ||
echo is-pre-release: ${{ steps.parse-valid.outputs.is-pre-release }} | ||
``` | ||
### Inputs | ||
| Property | Required | Description | | ||
| -------- | -------- | ---------------------- | | ||
| version | yes | Semver string to parse | | ||
### Outputs (with `1.2.3rc4` as an example input) | ||
|
||
| Property | Example | Description | | ||
| ------------------- | ---------- | ----------------------------------------------- | | ||
| version | `1.2.3rc4` | Python Parsed version | | ||
| version-semver | `1.2.3-rc4` | Fully Semver compliant version | | ||
| base-version | `1.2.3` | Base version | | ||
| major | `1` | Major version | | ||
| minor | `2` | Major version | | ||
| patch | `3` | Patch version | | ||
| pre-release | `rc4` | Entire pre-release version | | ||
| pre-release-version | `4` | Version part of pre-release | | ||
| pre-release-type | `rc` | Type of pre-release | | ||
| is-pre-release | `1` | Determines if version is a pre-release (1 \| 0) | | ||
| is-pre-release | `1` | Determines if version is a pre-release (1 \| 0) | | ||
|
||
### Development | ||
|
||
- This action is tested by [this](../.github/workflows/parse-semver.yml) workflow. | ||
- You can run this test workflow locally with [act](https://github.com/nektos/act) by running `act -W .github/workflows/parse-semver.yml` | ||
- Note: `act` does not handle the `continue-on-error` logic properly, so the invalid test case will fail locally. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: "Parse Semver" | ||
description: "Parse a semver string into semantic parts" | ||
inputs: | ||
version: | ||
description: "Semver string to parse" | ||
required: true | ||
outputs: | ||
version: | ||
description: "Parsed version" | ||
semver-compliant-version: | ||
# details: https://peps.python.org/pep-0440/#:~:text=clause%2011)%20are-,not%20compatible,-with%20this%20PEP | ||
description: 'Semver complicant version with dash' | ||
base-version: | ||
description: "Base version" | ||
major: | ||
description: "Major version" | ||
minor: | ||
description: "Minor version" | ||
patch: | ||
description: "Patch version" | ||
pre-release: | ||
description: "Pre-release type and version (ex: rc1, b2)" | ||
pre-release-version: | ||
description: "Pre-release version (ex: 1, 2)" | ||
pre-release-type: | ||
description: "Pre-release type (ex: b, rc)" | ||
is-pre-release: | ||
description: "Is this version a pre-release? (1 or 0)" | ||
runs: | ||
using: "docker" | ||
image: "Dockerfile" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
from packaging.version import parse, Version | ||
|
||
|
||
def main(): | ||
input_version = os.environ["INPUT_VERSION"] | ||
parsed_version = parse(input_version) | ||
assert parsed_version.release, f"Not a valid version: {input_version}" | ||
assert isinstance(parsed_version, Version) | ||
|
||
# ('rc', 2) -> pre_release_type = rc, pre_release_version = 2 | ||
pre_release_type, pre_release_version = ( | ||
parsed_version.pre | ||
if parsed_version.pre else ('', '') | ||
) | ||
assert isinstance(pre_release_type, str),\ | ||
f"Not a valid pre-release type (ex: rc, b): {pre_release_type}" | ||
|
||
pre_release = pre_release_type + str(pre_release_version) | ||
is_pre_release = parsed_version.pre is not None | ||
is_pre_release_truthy = 1 if is_pre_release else 0 | ||
|
||
semver_version = parsed_version.base_version | ||
if pre_release: | ||
semver_version = f"{semver_version}-{pre_release}" | ||
|
||
|
||
print("::group::Parse Semver Outputs") | ||
print(f"version={parsed_version.public}") | ||
print(f"semver-version={semver_version}") | ||
print(f"base-version={parsed_version.base_version}") | ||
print(f"major={parsed_version.major}") | ||
print(f"minor={parsed_version.minor}") | ||
print(f"patch={parsed_version.micro}") | ||
print(f"pre-release-type={pre_release_type}") | ||
print(f"pre-release-version={pre_release_version}") | ||
print(f"pre-release={pre_release}") | ||
print(f"is-pre-release={is_pre_release_truthy}") | ||
print("::endgroup::") | ||
|
||
print(f"::set-output name=version::{parsed_version.public}") | ||
print(f"::set-output name=base-version::{parsed_version.base_version}") | ||
print(f"::set-output name=major::{parsed_version.major}") | ||
print(f"::set-output name=minor::{parsed_version.minor}") | ||
print(f"::set-output name=patch::{parsed_version.micro}") | ||
print(f"::set-output name=pre-release-type::{pre_release_type}") | ||
print(f"::set-output name=pre-release-version::{pre_release_version}") | ||
print(f"::set-output name=pre-release::{pre_release}") | ||
print(f"::set-output name=is-pre-release::{is_pre_release_truthy}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |