From 5502a213f54b4b446703cba41bcadd81532658bf Mon Sep 17 00:00:00 2001 From: Dou Date: Tue, 24 Aug 2021 02:59:14 +0200 Subject: [PATCH] add workflows to publish at PyPi --- .github/static/release_tag_msg.txt | 4 ++ .github/workflows/publish-on-pypi.yml | 66 +++++++++++++++++++++++++++ tasks.py | 45 ++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 .github/static/release_tag_msg.txt create mode 100644 .github/workflows/publish-on-pypi.yml create mode 100644 tasks.py diff --git a/.github/static/release_tag_msg.txt b/.github/static/release_tag_msg.txt new file mode 100644 index 0000000..90d74ae --- /dev/null +++ b/.github/static/release_tag_msg.txt @@ -0,0 +1,4 @@ +TAG_NAME + +This tag was created automatically through the GH Actions +"Publish on PyPI" workflow. diff --git a/.github/workflows/publish-on-pypi.yml b/.github/workflows/publish-on-pypi.yml new file mode 100644 index 0000000..d1d1a55 --- /dev/null +++ b/.github/workflows/publish-on-pypi.yml @@ -0,0 +1,66 @@ +name: Publish on PyPI + +on: + release: + types: + - published + +jobs: + publish: + runs-on: ubuntu-latest + if: github.repository == 'osscar-org/jupyterlab-mol-visualizer' && startsWith(github.ref, 'refs/tags/v') + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install Python dependencies + run: | + python -m pip install --upgrade pip + pip install -U setuptools + pip install -U invoke + pip install -U jupyter-packaging + pip install -U jupyterlab + + - name: Install the node + uses: actions/setup-node@v2 + with: + node-version: '14' + - run: npm install + + - name: Update version + run: invoke update-version --version="${GITHUB_REF#refs/tags/}" + + - name: Create new tag + run: | + git config --local user.email "dev@materialscloud.org" + git config --local user.name "Materials Cloud Team" + + git commit -m "Release ${GITHUB_REF#refs/tags/}" -a + + TAG_MSG=.github/static/release_tag_msg.txt + sed -i "s|TAG_NAME|${GITHUB_REF#refs/tags/}|g" "${TAG_MSG}" + + git tag -af -F "${TAG_MSG}" ${GITHUB_REF#refs/tags/} + + - name: Push release commit and new tag + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + force: true + tags: true + branch: develop + + - name: Build source distribution + run: python ./setup.py sdist bdist_wheel + + - name: Publish package to PyPI + uses: pypa/gh-action-pypi-publish@master + with: + user: __token__ + password: ${{ secrets.PYPI_PASSWORD }} diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..9dcc397 --- /dev/null +++ b/tasks.py @@ -0,0 +1,45 @@ +from pathlib import Path +import re +import sys +from typing import Tuple + +from invoke import task + + +TOP_DIR = Path(__file__).parent.resolve() + + +def update_file(filename: str, sub_line: Tuple[str, str], strip: str = None): + """Utility function for tasks to read, update, and write files""" + with open(filename, "r") as handle: + lines = [ + re.sub(sub_line[0], sub_line[1], line.rstrip(strip)) for line in handle + ] + + with open(filename, "w") as handle: + handle.write("\n".join(lines)) + handle.write("\n") + + +@task +def update_version(_, version=""): + """Update package version using SemVer syntax.""" + if version: + if version.startswith("v"): + version = version[1:] + if re.match(r"[0-9]+(\.[0-9]+){2}.*", version) is None: + sys.exit( + f"Error: Passed version ({version}) does to match the SemVer format: " + "Major.Minor.Patch(+ extras)." + ) + else: + sys.exit( + "Please pass --version with a value that complies to the SemVer format: " + "Major.Minor.Patch(+ extras)." + ) + + update_file( + TOP_DIR.joinpath("package.json"), (r'"version":.*,', f'"version": "{version}",') + ) + + print(f"Bumped version to {version} !")