release.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. name: Build and publish Python package
  2. # Least-privilege default for every job; the publish job grants itself id-token below.
  3. permissions:
  4. contents: read
  5. on:
  6. pull_request:
  7. paths:
  8. - '.github/workflows/release.yml'
  9. - 'pyproject.toml'
  10. - 'README.md'
  11. - 'LICENSE.txt'
  12. - 'base_requirements.txt'
  13. - 'requirements.txt'
  14. - 'upgrade.sh'
  15. - 'contrib/**'
  16. - 'docs/**'
  17. - 'mkdocs.yml'
  18. - 'netbox/**'
  19. - 'scripts/packaging/**'
  20. - 'scripts/verify_*.py'
  21. - 'scripts/smoketest_configuration.py'
  22. push:
  23. tags:
  24. - 'v*'
  25. workflow_dispatch:
  26. jobs:
  27. build:
  28. name: Build package artifacts
  29. runs-on: ubuntu-latest
  30. steps:
  31. - name: Check out repository
  32. uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  33. with:
  34. persist-credentials: false
  35. - name: Set up Python
  36. uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
  37. with:
  38. python-version: '3.12'
  39. cache: pip
  40. - name: Install build tooling
  41. run: python -m pip install --upgrade build twine
  42. - name: Install documentation toolchain
  43. run: python -m pip install -r requirements.txt
  44. - name: Render the documentation
  45. # -c = clean cache, -s = strict (abort on warnings); verify_wheel_contents.py
  46. # additionally guards against a partial render reaching the wheel.
  47. run: zensical build -c -s
  48. - name: Build sdist and wheel
  49. run: python -m build
  50. - name: Check package metadata
  51. run: twine check dist/*
  52. - name: Upload package artifacts
  53. uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
  54. with:
  55. name: python-package-distributions
  56. path: dist/
  57. if-no-files-found: error
  58. verify-dependencies:
  59. name: Verify dependency pins are in sync
  60. runs-on: ubuntu-latest
  61. needs: build
  62. steps:
  63. - name: Check out repository
  64. uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  65. with:
  66. persist-credentials: false
  67. - name: Set up Python
  68. uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
  69. with:
  70. python-version: '3.12'
  71. cache: pip
  72. - name: Install packaging
  73. run: python -m pip install packaging
  74. - name: Verify requirements.txt is consistent with base_requirements.txt
  75. run: python scripts/verify_dependencies.py
  76. - name: Download package artifacts
  77. uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
  78. with:
  79. name: python-package-distributions
  80. path: dist/
  81. - name: Verify wheel Requires-Dist matches requirements.txt
  82. run: python scripts/verify_wheel_metadata.py dist/*.whl
  83. - name: Verify wheel excludes live configuration files
  84. run: python scripts/verify_wheel_contents.py dist/*.whl
  85. verify-sdist:
  86. name: Verify the sdist builds a wheel
  87. runs-on: ubuntu-latest
  88. needs: build
  89. steps:
  90. - name: Check out repository
  91. uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  92. with:
  93. persist-credentials: false
  94. - name: Set up Python
  95. uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
  96. with:
  97. python-version: '3.12'
  98. cache: pip
  99. - name: Install tooling
  100. run: python -m pip install --upgrade pip packaging
  101. - name: Download package artifacts
  102. uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
  103. with:
  104. name: python-package-distributions
  105. path: dist/
  106. - name: Verify the sdist contents
  107. run: |
  108. python scripts/verify_sdist_contents.py dist/*.tar.gz
  109. - name: Build a wheel from the sdist
  110. run: |
  111. python -m pip wheel --no-deps dist/*.tar.gz -w sdist-wheel/
  112. - name: Verify the sdist-built wheel
  113. run: |
  114. python scripts/verify_wheel_metadata.py sdist-wheel/*.whl
  115. python scripts/verify_wheel_contents.py sdist-wheel/*.whl
  116. cli-smoke-test:
  117. name: Smoke test wheel CLI (no dependencies)
  118. runs-on: ubuntu-latest
  119. needs: build
  120. # The pre-configuration CLI paths are stdlib-only, so a --no-deps install suffices.
  121. # Unlike smoke-test, this job also runs on pull requests.
  122. steps:
  123. - name: Set up Python
  124. uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
  125. with:
  126. python-version: '3.12'
  127. - name: Download package artifacts
  128. uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
  129. with:
  130. name: python-package-distributions
  131. path: dist/
  132. - name: Install wheel without dependencies
  133. run: |
  134. python -m venv "$RUNNER_TEMP/netbox-cli-venv"
  135. "$RUNNER_TEMP/netbox-cli-venv/bin/python" -m pip install --no-deps dist/*.whl
  136. - name: Exercise the pre-configuration CLI
  137. run: |
  138. "$RUNNER_TEMP/netbox-cli-venv/bin/netbox" --version
  139. "$RUNNER_TEMP/netbox-cli-venv/bin/netbox" version
  140. "$RUNNER_TEMP/netbox-cli-venv/bin/python" -m netbox --version
  141. "$RUNNER_TEMP/netbox-cli-venv/bin/netbox" secret-key | grep -Eq '^.{50}$' || { echo "secret-key not 50 chars"; exit 1; }
  142. - name: Smoke-test netbox setup from the wheel
  143. run: |
  144. "$RUNNER_TEMP/netbox-cli-venv/bin/netbox" setup --target "$RUNNER_TEMP/nbroot"
  145. for f in "$RUNNER_TEMP/nbroot/conf/__init__.py" "$RUNNER_TEMP/nbroot/conf/configuration.py" "$RUNNER_TEMP/nbroot/local_requirements.txt"; do
  146. test -f "$f" || { echo "missing $f"; exit 1; }
  147. done
  148. for f in apache.conf gunicorn.py netbox-rq.service netbox.env netbox.service nginx.conf uwsgi.ini; do
  149. test -s "$RUNNER_TEMP/nbroot/contrib/$f" || { echo "missing or empty contrib/$f"; exit 1; }
  150. done
  151. smoke-test:
  152. name: Smoke test wheel install
  153. runs-on: ubuntu-latest
  154. needs: build
  155. # The wheel install + database migration is expensive; only run it for tag
  156. # pushes and manual dispatch, not on every packaging-related pull request.
  157. # cli-smoke-test provides lightweight, dependency-free CLI coverage on every PR instead.
  158. if: github.event_name != 'pull_request'
  159. services:
  160. postgres:
  161. image: postgres:17
  162. env:
  163. POSTGRES_DB: netbox
  164. POSTGRES_USER: netbox
  165. POSTGRES_PASSWORD: netbox
  166. ports:
  167. - 5432:5432
  168. options: >-
  169. --health-cmd "pg_isready -U netbox -d netbox"
  170. --health-interval 10s
  171. --health-timeout 5s
  172. --health-retries 5
  173. redis:
  174. image: redis:7
  175. ports:
  176. - 6379:6379
  177. options: >-
  178. --health-cmd "redis-cli ping"
  179. --health-interval 10s
  180. --health-timeout 5s
  181. --health-retries 5
  182. env:
  183. NETBOX_CONFIGURATION: smoketest_configuration
  184. POSTGRES_DB: netbox
  185. POSTGRES_USER: netbox
  186. POSTGRES_PASSWORD: netbox
  187. POSTGRES_HOST: 127.0.0.1
  188. POSTGRES_PORT: 5432
  189. REDIS_HOST: 127.0.0.1
  190. REDIS_PORT: 6379
  191. steps:
  192. - name: Check out repository
  193. uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  194. with:
  195. persist-credentials: false
  196. - name: Set up Python
  197. uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
  198. with:
  199. python-version: '3.12'
  200. cache: pip
  201. - name: Download package artifacts
  202. uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
  203. with:
  204. name: python-package-distributions
  205. path: dist/
  206. - name: Install system build dependencies for psycopg
  207. run: sudo apt-get update && sudo apt-get install -y libpq-dev
  208. - name: Install wheel into a clean virtual environment
  209. run: |
  210. python -m venv "$RUNNER_TEMP/netbox-wheel-venv"
  211. "$RUNNER_TEMP/netbox-wheel-venv/bin/python" -m pip install --upgrade pip
  212. "$RUNNER_TEMP/netbox-wheel-venv/bin/python" -m pip install dist/*.whl
  213. - name: Run NetBox smoke checks
  214. env:
  215. # STATIC_ROOT is not a configuration parameter; NETBOX_ROOT places it under the scratch base.
  216. NETBOX_ROOT: ${{ runner.temp }}/netbox-smoketest
  217. NETBOX_SMOKETEST_BASE: ${{ runner.temp }}/netbox-smoketest
  218. PYTHONPATH: ${{ github.workspace }}/scripts
  219. run: |
  220. "$RUNNER_TEMP/netbox-wheel-venv/bin/netbox" check
  221. "$RUNNER_TEMP/netbox-wheel-venv/bin/netbox" upgrade --no-input
  222. test -f "$NETBOX_SMOKETEST_BASE/static/docs/index.html" || { echo "bundled documentation was not collected to STATIC_ROOT"; exit 1; }
  223. test -f "$NETBOX_SMOKETEST_BASE/static/docs/models/dcim/device/index.html" || { echo "model documentation page was not collected"; exit 1; }
  224. - name: Smoke-test netbox setup from the wheel
  225. run: |
  226. "$RUNNER_TEMP/netbox-wheel-venv/bin/netbox" setup --target "$RUNNER_TEMP/nbroot"
  227. diff -q "$RUNNER_TEMP/nbroot/conf/configuration.py" netbox/netbox/configuration_example.py
  228. for f in apache.conf gunicorn.py netbox-rq.service netbox.env netbox.service nginx.conf uwsgi.ini; do
  229. diff -q "$RUNNER_TEMP/nbroot/contrib/$f" "contrib/$f"
  230. done
  231. publish-testpypi:
  232. name: Publish package to Test PyPI
  233. runs-on: ubuntu-latest
  234. needs: [smoke-test, cli-smoke-test, verify-dependencies, verify-sdist]
  235. # Publishing always requires a v* tag ref: a tag push publishes to Test PyPI
  236. # automatically, and a manual dispatch does the same when the chosen ref is a v* tag.
  237. # Branch dispatches still run the build, verify, and smoke-test jobs (a useful dry run)
  238. # but the publish job is skipped. Production PyPI publishing is intentionally absent
  239. # during the v4.6.x preview; it arrives with the v4.7.0 feature branch.
  240. # startsWith() only routes to this job (workflow `if:` expressions cannot regex-match);
  241. # the exact tag format (v<release.yaml version>) is enforced below by the "Enforce
  242. # release tag format" step and scripts/verify_release_tag.py before any upload.
  243. if: startsWith(github.ref, 'refs/tags/v') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
  244. environment:
  245. name: testpypi
  246. url: https://test.pypi.org/p/netbox
  247. permissions:
  248. contents: read
  249. id-token: write
  250. steps:
  251. - name: Enforce release tag format
  252. env:
  253. TAG: ${{ github.ref_name }}
  254. run: |
  255. [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$ ]] || {
  256. echo "Ref '$TAG' is not a release tag of the form vX.Y.Z[-designation]"
  257. exit 1
  258. }
  259. - name: Check out repository
  260. uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
  261. with:
  262. persist-credentials: false
  263. - name: Set up Python
  264. uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
  265. with:
  266. python-version: '3.12'
  267. - name: Install tooling
  268. run: python -m pip install --upgrade pip packaging
  269. - name: Download package artifacts
  270. uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
  271. with:
  272. name: python-package-distributions
  273. path: dist/
  274. - name: Verify the git tag matches the built version
  275. run: python scripts/verify_release_tag.py "${{ github.ref_name }}" dist/*.whl
  276. - name: Publish package distributions to Test PyPI
  277. uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0
  278. with:
  279. repository-url: https://test.pypi.org/legacy/