Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blog post Simple versioning with git tags #197

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions blog/2024-04-27-simple-versioning-with-git-tags/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
authors: [grihabor]
tags: []
---

# Simple versioning with git tags

A frequently asked question on [Pants Slack](/community/getting-help) is how to
set a version - such as for a docker image, helm chart or Python distribution -
based on git state.

Pants does have various solutions for this, but they tend to be too
complicated, or too limited:

- [Generating version tags from Git](../../docs/docs/using-pants/generating-version-tags-from-git.mdx)
- [Custom `python_artifact()` kwargs](../../docs/docs/writing-plugins/common-plugin-tasks/custom-python-artifact-kwargs.mdx)
- [vcs_version](../../reference/targets/vcs_version.mdx) target
- [Using env vars to include dynamic data in tags](../../docs/docs/docker/tagging-docker-images.mdx#using-env-vars-to-include-dynamic-data-in-tags)

It turns out there is a hack that elegantly solves this problem. To make it
work we need one more pants feature [hidden in the
docs](../../docs/docs/using-pants/key-concepts/options.mdx#pantsbootstrap-file)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this link will work

— the `.pants.bootstrap` file. It makes it possible to automatically set the
`VERSION` env var right before pants starts:

```bash title=".pants.bootstrap"
#!/bin/sh

VERSION="${VERSION:-$(git describe --tags --dirty --match "[0-9\.]*" || echo 0.0.1)}"
export VERSION
```

Now we can use the env var everywhere!

To set [docker_image](../../reference/targets/python_distribution.mdx) tag:

```python file="BUILD"
docker_image(
name="image",
image_tags=[env("VERSION")],
)
```

To pass [docker_image](../../reference/targets/python_distribution.mdx) build arg:

```python file="BUILD"
docker_image(
name="image",
extra_build_args=["VERSION=" + env("VERSION")],
)
```

To set [python_distribution](../../reference/targets/python_distribution.mdx) version:

```python file="BUILD"
python_distribution(
name="mydist",
provides=python_artifact(
name="mydist",
version=env("VERSION"),
),
)
```

To set [helm_chart](../../reference/targets/helm_chart.mdx) version:

```python file="BUILD"
helm_chart(
version=env("VERSION"),
)
```
5 changes: 5 additions & 0 deletions blog/authors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ eric:
title: Pants Maintainer
url: https://github.com/Eric-Arellano
image_url: https://github.com/Eric-Arellano.png
grihabor:
name: Gregory Borodin
title: Pants Contributor
url: https://github.com/grihabor
image_url: https://github.com/grihabor.png
joshua:
name: Joshua Cannon
title: Pants Maintainer
Expand Down
Loading