Skip to content

Commit 47bb7f6

Browse files
authored
automate PyPI publishing with GitHub Actions (#827)
+ version: replace version_num with version for easy re-use, and update its usage in __inti__.py + setup: use sys.path.append() to replace open() to grab version from version.py + add .github/workflows/publish-to-test-pypi.yml: - triggered for push event on insarlab/main branch - use "pip install" and "python -m build" to build the binary wheel and source tarball - use pypa/gh-action-pypi-publish action to push to PyPI and TestPyPI - push to PyPI only for released version and when owner is insarlab + repo setting: add github secrets from pypi, to support the above action
1 parent 2e9e300 commit 47bb7f6

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

.github/workflows/build-docker.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ on:
1212

1313
jobs:
1414
dockerize:
15+
name: Build Docker image and push to GitHub Container Registry
1516
runs-on: ubuntu-latest
1617
steps:
1718
- uses: actions/checkout@v2
@@ -31,7 +32,7 @@ jobs:
3132
- name: Set environment variables for docker build
3233
run: |
3334
# Lowercase repo for Github Container Registry
34-
echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}
35+
echo "REPO=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV}
3536
# Ensure tags are checked out
3637
git fetch origin +refs/tags/*:refs/tags/*
3738
# Version number from tag
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# link: https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
2+
name: publish distributions 📦 to PyPI and TestPyPI
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
tags:
9+
- v*
10+
11+
jobs:
12+
build-n-publish:
13+
if: github.repository_owner == 'insarlab'
14+
15+
name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v3
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Set up Python 3.10
23+
uses: actions/setup-python@v3
24+
with:
25+
python-version: "3.10"
26+
27+
- name: Install pypa/build
28+
run: >-
29+
python -m
30+
pip install
31+
build
32+
--user
33+
34+
- name: Build a binary wheel and a source tarball
35+
run: >-
36+
python -m
37+
build
38+
--sdist
39+
--wheel
40+
--outdir dist/
41+
.
42+
43+
- name: Publish developed version 📦 to Test PyPI
44+
if: github.ref == 'refs/heads/main'
45+
uses: pypa/gh-action-pypi-publish@release/v1
46+
with:
47+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
48+
repository_url: https://test.pypi.org/legacy/
49+
skip_existing: false
50+
verbose: true
51+
52+
- name: Publish released version 📦 to PyPI
53+
if: startsWith(github.ref, 'refs/tags/v')
54+
uses: pypa/gh-action-pypi-publish@release/v1
55+
with:
56+
password: ${{ secrets.PYPI_API_TOKEN }}
57+
verbose: true
58+

mintpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# get version info
22
from mintpy.version import (
3-
version_num as __version__,
3+
version as __version__,
44
logo as __logo__,
55
)

mintpy/version.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def get_version_info():
6767

6868
###########################################################################
6969

70-
version_num, version_date = get_version_info()
70+
version, version_date = get_version_info()
7171
version_description = """MintPy version {v}, date {d}""".format(
72-
v=version_num,
72+
v=version,
7373
d=version_date,
7474
)
7575

@@ -90,7 +90,7 @@ def get_version_info():
9090
Miami InSAR Time-series software in Python \______/
9191
MintPy {v}, {d}
9292
___________________________________________________________
93-
""".format(v=version_num, d=version_date)
93+
""".format(v=version, d=version_date)
9494

9595
website = 'https://github.com/insarlab/MintPy'
9696

setup.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,20 @@
55
# Author: Zhang Yunjun, Nov 2020 #
66
############################################################
77

8-
8+
import os
9+
import sys
910
# Always prefer setuptools over distutils
1011
from setuptools import setup, find_packages
1112

13+
# Grab version and description from version.py
14+
# link: https://stackoverflow.com/questions/53648900
15+
sys.path.append(os.path.dirname(__file__))
16+
from mintpy.version import version, description
1217

13-
# Grab from README file: long_description
18+
# Grab long_description from README.md
1419
with open("docs/README.md", "r") as f:
1520
long_description = f.read()
1621

17-
# Grab from version.py file: version and description
18-
with open("mintpy/version.py", "r") as f:
19-
lines = f.readlines()
20-
# version
21-
line = [line for line in lines if line.strip().startswith("Tag(")][0].strip()
22-
version = line.replace("'",'"').split('"')[1]
23-
# description
24-
line = [line for line in lines if line.startswith("description")][0].strip()
25-
description = line.replace("'",'"').split('"')[1]
26-
27-
2822
setup(
2923
name="mintpy",
3024
version=version,

0 commit comments

Comments
 (0)