+ ability to generate the docker tags from setup.py

This commit is contained in:
nicobo 2021-01-25 23:22:26 +01:00
parent f3a00a31ce
commit bd389242fd
No known key found for this signature in database
GPG key ID: 2581E71C5FA5285F

View file

@ -2,13 +2,86 @@
# -*- coding: utf-8 -*-
import setuptools
#from setuptools_scm import get_version
import re
with open("README.md", "r") as fh:
long_description = fh.read()
def local_scheme(version):
return ""
"""
Very specific Docker tag naming for this project
This could have been done in Sh, Makefile, ... but here it's integrated
with the setup script.
See https://dankeder.com/posts/adding-custom-commands-to-setup-py/
What variables are available from Githu Actions : https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context
"""
class DockerTagsCommand(setuptools.Command):
description: "Prints a tag list for the given 'base tag'"
user_options = [
('image=', 'i', 'Image name (defaults to nicolabs/nicobot)'),
('variant=', 'v', 'Image variant / base tag : debian|debian-signal|alpine'),
('branch=', 'b', 'The git ref as <branch name> or refs/heads/<branch name>'),
('tag=', 't', 'The git ref as <tag name> or refs/tags/<tag name>'),
('ref=', 'r', 'The git ref as refs/tags|heads/<tag or branch name>'),
('sep=', 's', 'A string to separate each tag in the output (defaults to a new line)'),
]
def initialize_options(self):
self.image = 'nicolabs/nicobot'
self.variant = None
self.branch = None
self.tag = None
self.ref = None
self.sep = '\n'
def finalize_options(self):
"""
ref is the backup value for either tag or branch
final tag & branch values are normalized anyway
"""
stag = self.tag
if not stag and self.ref:
stag = self.ref
try:
# If semver, we don't retain the 'v' prefix
self.tag = re.search('refs/tags/v?(.*)', stag, flags=re.IGNORECASE).group(1)
except:
pass
sbranch = self.branch
if not sbranch and self.ref:
sbranch = self.ref
try:
self.branch = re.search('refs/heads/(.*)', sbranch, flags=re.IGNORECASE).group(1)
except:
pass
def run(self):
# # TODO How to get the actual configuration as set up by setuptools ?
# version = get_version(local_scheme='no-local-version')
tags = []
if self.tag:
# When git-tagged, the variant name alone means : the 'latest' for this variant
tags = [ "%s:%s" % (self.image,self.variant) ]
# It is also tagged with the version
tags = tags + [ "%s:%s-%s" % (self.image,self.variant,self.tag) ]
# Only debian-signal is tagged with 'latest' additionaly
if self.variant == "debian-signal":
tags = tags + [ "%s:latest" % (self.image) ]
elif self.branch:
# All non git-tagged commits overwrite the 'dev' tag (only one is useful currently)
tags = tags + [ "%s:%s-dev" % (self.image,self.variant) ]
print( self.sep.join(tags) )
setuptools.setup(
# See https://packaging.python.org/tutorials/packaging-projects/
name="nicobot",
@ -63,4 +136,7 @@ setuptools.setup(
# and https://github.com/pypa/setuptools_scm#user-content-version-number-construction
"local_scheme": 'no-local-version',
},
cmdclass = {
'docker_tags': DockerTagsCommand,
},
)