Skip to content

Commit cd2c35a

Browse files
authored
ci: add workflow for releases (#3018)
GitHub Actions workflow to create a release: will upload to PyPI and create a GitHub release with the `sdist` and `bdist_wheel` as well. The version code is switched to `setuptools_scm` to work well with this flow (e.g. avoid needing to write a script that does a `sed` on the version file and commits as part of release). Signed-off-by: Milas Bowman <[email protected]>
1 parent 828d06f commit cd2c35a

File tree

12 files changed

+96
-22
lines changed

12 files changed

+96
-22
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ max_line_length = 80
99

1010
[*.md]
1111
trim_trailing_whitespace = false
12+
13+
[*.{yaml,yml}]
14+
indent_size = 2

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: Python package
22

33
on: [push, pull_request]
44

5+
env:
6+
DOCKER_BUILDKIT: '1'
7+
58
jobs:
69
flake8:
710
runs-on: ubuntu-latest

.github/workflows/release.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
tag:
7+
description: "Release Tag WITHOUT `v` Prefix (e.g. 6.0.0)"
8+
required: true
9+
dry-run:
10+
description: 'Dry run'
11+
required: false
12+
type: boolean
13+
default: true
14+
15+
jobs:
16+
publish:
17+
runs-on: ubuntu-22.04
18+
steps:
19+
- uses: actions/checkout@v3
20+
21+
- uses: actions/setup-python@v4
22+
with:
23+
python-version: '3.10'
24+
25+
- run: python setup.py sdist bdist_wheel
26+
env:
27+
SETUPTOOLS_SCM_PRETEND_VERSION_FOR_DOCKER: ${{ inputs.tag }}
28+
29+
- name: Publish to PyPI
30+
uses: pypa/gh-action-pypi-publish@release/v1
31+
if: ! inputs.dry-run
32+
with:
33+
password: ${{ secrets.PYPI_API_TOKEN }}
34+
35+
- name: Create GitHub release
36+
uses: ncipollo/release-action@v1
37+
if: ! inputs.dry-run
38+
with:
39+
artifacts: "dist/*"
40+
generateReleaseNotes: true
41+
draft: true
42+
commit: ${{ github.sha }}
43+
token: ${{ secrets.GITHUB_TOKEN }}
44+
tag: ${{ inputs.tag }}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ html/*
1313
_build/
1414
README.rst
1515

16+
# setuptools_scm
17+
_version.py
18+
1619
env/
1720
venv/
1821
.idea/
22+
*.iml

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ ARG PYTHON_VERSION=3.10
22

33
FROM python:${PYTHON_VERSION}
44

5-
RUN mkdir /src
65
WORKDIR /src
76

87
COPY requirements.txt /src/requirements.txt
@@ -11,5 +10,6 @@ RUN pip install --no-cache-dir -r requirements.txt
1110
COPY test-requirements.txt /src/test-requirements.txt
1211
RUN pip install --no-cache-dir -r test-requirements.txt
1312

14-
COPY . /src
13+
COPY . .
14+
ARG SETUPTOOLS_SCM_PRETEND_VERSION_DOCKER
1515
RUN pip install --no-cache-dir .

docker/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from .context import Context
55
from .context import ContextAPI
66
from .tls import TLSConfig
7-
from .version import version, version_info
7+
from .version import __version__
88

9-
__version__ = version
109
__title__ = 'docker'

docker/constants.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from .version import version
2+
from .version import __version__
33

44
DEFAULT_DOCKER_API_VERSION = '1.41'
55
MINIMUM_DOCKER_API_VERSION = '1.21'
@@ -28,7 +28,7 @@
2828
IS_WINDOWS_PLATFORM = (sys.platform == 'win32')
2929
WINDOWS_LONGPATH_PREFIX = '\\\\?\\'
3030

31-
DEFAULT_USER_AGENT = f"docker-sdk-python/{version}"
31+
DEFAULT_USER_AGENT = f"docker-sdk-python/{__version__}"
3232
DEFAULT_NUM_POOLS = 25
3333

3434
# The OpenSSH server default value for MaxSessions is 10 which means we can

docker/version.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
version = "6.0.0-dev"
2-
version_info = tuple(int(d) for d in version.split("-")[0].split("."))
1+
try:
2+
from ._version import __version__
3+
except ImportError:
4+
try:
5+
# importlib.metadata available in Python 3.8+, the fallback (0.0.0)
6+
# is fine because release builds use _version (above) rather than
7+
# this code path, so it only impacts developing w/ 3.7
8+
from importlib.metadata import version, PackageNotFoundError
9+
try:
10+
__version__ = version('docker')
11+
except PackageNotFoundError:
12+
__version__ = '0.0.0'
13+
except ImportError:
14+
__version__ = '0.0.0'

docs/conf.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@
6363
# |version| and |release|, also used in various other places throughout the
6464
# built documents.
6565
#
66-
with open('../docker/version.py') as vfile:
67-
exec(vfile.read())
68-
# The full version, including alpha/beta/rc tags.
69-
release = version
70-
# The short X.Y version.
71-
version = f'{version_info[0]}.{version_info[1]}'
66+
# see https://github.com/pypa/setuptools_scm#usage-from-sphinx
67+
from importlib.metadata import version
68+
release = version('docker')
69+
# for example take major/minor
70+
version = '.'.join(release.split('.')[:2])
7271

7372
# The language for content autogenerated by Sphinx. Refer to documentation
7473
# for a list of supported languages.

pyproject.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build-system]
2+
requires = ["setuptools>=45", "setuptools_scm[toml]>=6.2"]
3+
4+
[tool.setuptools_scm]
5+
write_to = 'docker/_version.py'

setup.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
'ssh': ['paramiko>=2.4.3'],
3030
}
3131

32-
version = None
33-
exec(open('docker/version.py').read())
34-
3532
with open('./test-requirements.txt') as test_reqs_txt:
3633
test_requirements = [line for line in test_reqs_txt]
3734

@@ -42,7 +39,9 @@
4239

4340
setup(
4441
name="docker",
45-
version=version,
42+
use_scm_version={
43+
'write_to': 'docker/_version.py'
44+
},
4645
description="A Python library for the Docker Engine API.",
4746
long_description=long_description,
4847
long_description_content_type='text/markdown',
@@ -54,6 +53,7 @@
5453
'Tracker': 'https://github.com/docker/docker-py/issues',
5554
},
5655
packages=find_packages(exclude=["tests.*", "tests"]),
56+
setup_requires=['setuptools_scm'],
5757
install_requires=requirements,
5858
tests_require=test_requirements,
5959
extras_require=extras_require,

tests/Dockerfile

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
# syntax = docker/dockerfile:1.4
12
ARG PYTHON_VERSION=3.10
2-
33
FROM python:${PYTHON_VERSION}
44

55
ARG APT_MIRROR
@@ -29,11 +29,16 @@ RUN curl -sSL -o /opt/docker-credential-pass.tar.gz \
2929
chmod +x /usr/local/bin/docker-credential-pass
3030

3131
WORKDIR /src
32+
3233
COPY requirements.txt /src/requirements.txt
33-
RUN pip install -r requirements.txt
34+
RUN --mount=type=cache,target=/root/.cache/pip \
35+
pip install -r requirements.txt
3436

3537
COPY test-requirements.txt /src/test-requirements.txt
36-
RUN pip install -r test-requirements.txt
38+
RUN --mount=type=cache,target=/root/.cache/pip \
39+
pip install -r test-requirements.txt
3740

3841
COPY . /src
39-
RUN pip install .
42+
ARG SETUPTOOLS_SCM_PRETEND_VERSION=99.0.0-docker
43+
RUN --mount=type=cache,target=/root/.cache/pip \
44+
pip install -e .

0 commit comments

Comments
 (0)