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

No support for Python packages versioning (PEP 440) #85

Closed
gdubicki opened this issue May 16, 2021 · 12 comments · Fixed by #108
Closed

No support for Python packages versioning (PEP 440) #85

gdubicki opened this issue May 16, 2021 · 12 comments · Fixed by #108

Comments

@gdubicki
Copy link

gdubicki commented May 16, 2021

Hi!

Thanks for the cool action! It works like a charm ...as long as you are using semver.

Unfortunately in my case I wanted to use it for a Python package, which is versioned according to a pythonic standard - PEP440 - which is not semver-compatible.

I couldn't just use type=match as in the case of the pre-release versions we should not add any other tags than the one with just the version itself.

Initially I wanted to create a PR to add support for PEP 440 to this project, but I don't really know Typescript, so I ended up with writing it in, well, Python.

I would like to share this solution here for other people with such problem to find it and use this as a workaround and/or for someone to consider rewriting this in Typescript and making that PR. :)

Anyway: happy hacking!

name: publish 🐳 Docker image

on:
  push:
    tags:
      - "v*"

jobs:

  build-and-push-image:

    runs-on: ubuntu-latest

    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Get tags
        shell: python
        run: |
          import re
          from packaging.version import parse

          version = "${{ github.ref }}".replace("refs/tags/v", "")
          image = "ghcr.io/${{ github.repository }}"

          tags = set()

          # full version
          tags.add(f"{image}:{version}")

          if not parse(version).is_prerelease:
              # only final and post-releases should get the tags
              # used for automatic use of latest *stable* version

              # major_version
              major_version = re.search(r'(\d+?)\.', version).group(1)
              tags.add(f"{image}:{major_version}")

              # major_version.minor_version
              major_and_minor_version = re.search(r'(\d+?\.\d+?)\.', version).group(1)
              tags.add(f"{image}:{major_and_minor_version}")

              tags.add(f"{image}:latest")

          tags = ",".join(sorted(list(tags)))

          print(f"::set-output name=tags::{tags}")

        id: tags

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          platforms: linux/amd64
          push: true
          tags: ${{ steps.tags.outputs.tags }}

(source: voxpupuli/puppetboard@924bb55 + voxpupuli/puppetboard@94052a0)

@gdubicki gdubicki changed the title No support for Python packages versioning (PEP440) No support for Python packages versioning (PEP 440) May 16, 2021
@crazy-max
Copy link
Member

@gdubicki Thanks for your feedback. I think we could add a new type=pep440 or even translate to semver if possible. Will think about that.

@tillsteinbach
Copy link

I have the same request! Would really appreciate having pep440 support! For now I work with sermver + raw for prereleases, but proper support would be awesome!

@crazy-max
Copy link
Member

@gdubicki @tillsteinbach You can test the type=pep440 with crazy-max/docker-metadata-action@pep440. Doc: https://github.com/crazy-max/docker-metadata-action/tree/pep440#typepep440

@tillsteinbach
Copy link

Just tested it with a dev release (this was not working with semver) and a regular release and it works like a charm! Awesome thanks!

@crazy-max
Copy link
Member

@tillsteinbach Great to hear! Is your repository public so I can take a look at your workflow?

@tillsteinbach
Copy link

@crazy-max sure: https://github.com/tillsteinbach/WeConnect-mqtt/blob/9bb8d018475d1628289656ac02fb94ef8d3e7940/.github/workflows/python-docker-publish.yml#L72
if you wonder about the "type=raw,value=edge", it is to have the dev versions included in the edge tag (I don't push to docker hub on every commit)

@crazy-max
Copy link
Member

@tillsteinbach This run has been triggered by a push tag event which is v0.11.2dev3. The metadata action then generates 0.11.2.dev3 and edge which lgtm too:

Run crazy-max/docker-metadata-action@pep440
Context info
  eventName: push
  sha: 0a76359deb20e62645bf1e482f9868ff51c7991b
  ref: refs/tags/v0.11.2dev3
  workflow: Upload Python Package and Docker Image
  action: meta
  actor: ***
  runNumber: 46
  runId: 1004414204
Processing tags input
  type=pep440,pattern={{version}},value=,enable=true,priority=900
  type=raw,value=edge,enable=true,priority=200
Processing flavor input
  latest=auto
  prefix=
  prefixLatest=false
  suffix=
  suffixLatest=false
Docker image version
  0.11.2.dev3
Docker tags
  ***/weconnect-mqtt:0.11.2.dev3
  ***/weconnect-mqtt:edge
  ghcr.io/***/weconnect-mqtt:0.11.2.dev3
  ghcr.io/***/weconnect-mqtt:edge
Docker labels
  org.opencontainers.image.title=WeConnect-mqtt
  org.opencontainers.image.description=MQTT Client that publishes data from Volkswagen WeConnect
  org.opencontainers.image.url=https://github.com/***/WeConnect-mqtt
  org.opencontainers.image.source=https://github.com/***/WeConnect-mqtt
  org.opencontainers.image.version=0.11.2.dev3
  org.opencontainers.image.created=2021-07-06T12:40:39.999Z
  org.opencontainers.image.revision=0a76359deb20e62645bf1e482f9868ff51c7991b
  org.opencontainers.image.licenses=MIT

Also as you can see 0.11.2.dev3 != v0.11.2dev3 because the output is cleaned up to conform with the PEP 440 spec. If you want to preserve the initial Git tag you can use {{raw}} instead.

@tillsteinbach
Copy link

tillsteinbach commented Jul 6, 2021

Ahrg! Yes, I was unconcentrated :) really nice work!
Interestingly I did not notice my mistake because PyPi did the same correction to my version number :D

@gdubicki
Copy link
Author

gdubicki commented Jul 7, 2021

Hey @crazy-max , thanks for your work on this! I'll check it out within a few days.

@gdubicki
Copy link
Author

Unfortunately I cannot make it work, @crazy-max .

Probably I am just using GitHub Actions terribly wrong but I am getting:

Error: Unknown type attribute: pep440

I tried referring specifically to the latest as of now v3.4.1 in gitlabform/gitlabform@6418192 but this failed with:

Unable to resolve action `crazy-max/docker-metadata-action@v3.4.1`, unable to find version `v3.4.1`

Any suggestions?

@crazy-max
Copy link
Member

@gdubicki

crazy-max/docker-metadata-action@v3.4.1

This is my fork, use docker/metadata-action@v3 instead.

@gdubicki
Copy link
Author

🤦‍♂️
Thanks @crazy-max , it helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants