From f1221b0b1cac47e93461dbbb3e67c94ec9475a97 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen <43357585+CasperWA@users.noreply.github.com> Date: Tue, 27 Oct 2020 10:12:59 +0100 Subject: [PATCH] New release workflow (#15) Add a GH Actions workflow that runs when a GitHub release is published. It will run the new invoke task "update-version" with the tag name, updating `metadata.json`'s `"version"` value with the correct version. Then it will update the tag to the new commit and push it all to `master`. --- .github/dependabot.yml | 5 +++ .github/static/release_tag_msg.txt | 4 +++ .github/workflows/publish-on-pypi.yml | 49 +++++++++++++++++++++++++++ requirements_dev.txt | 1 + setup.py | 4 +++ tasks.py | 41 ++++++++++++++++++++++ 6 files changed, 104 insertions(+) create mode 100644 .github/static/release_tag_msg.txt create mode 100644 .github/workflows/publish-on-pypi.yml create mode 100644 requirements_dev.txt create mode 100644 tasks.py diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 89565e1..29ead1d 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,3 +5,8 @@ updates: schedule: interval: daily target-branch: master +- package-ecosystem: github-actions + directory: "/" + schedule: + interval: daily + target-branch: master diff --git a/.github/static/release_tag_msg.txt b/.github/static/release_tag_msg.txt new file mode 100644 index 0000000..4c2a13a --- /dev/null +++ b/.github/static/release_tag_msg.txt @@ -0,0 +1,4 @@ +TAG_NAME + +This tag was created automatically through the GH Actions +"Release new version" workflow. diff --git a/.github/workflows/publish-on-pypi.yml b/.github/workflows/publish-on-pypi.yml new file mode 100644 index 0000000..3585fe7 --- /dev/null +++ b/.github/workflows/publish-on-pypi.yml @@ -0,0 +1,49 @@ +name: Release new version + +on: + release: + types: + - published + +jobs: + release: + runs-on: ubuntu-latest + if: github.repository == 'aiidalab/aiidalab-optimade' && 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 -r requirements_dev.txt + + - name: Update version + run: invoke update-version --version="${GITHUB_REF#refs/tags/}" + + - name: Commit and update tag + run: | + git config --local user.email "aiidalab@materialscloud.org" + git config --local user.name "AiiDAlab 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: master diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..1f2899e --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1 @@ +invoke~=1.4 diff --git a/setup.py b/setup.py index 7d3c514..b2ca578 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,9 @@ with open(TOP_DIR.joinpath("requirements.txt")) as handle: REQUIREMENTS = handle.read() +with open(TOP_DIR.joinpath("requirements_dev.txt")) as handle: + DEV = handle.read() + setup( name="aiidalab-optimade", version=METADATA["version"], @@ -21,4 +24,5 @@ url="https://github.com/aiidalab/aiidalab-optimade", packages=find_packages(), install_requires=REQUIREMENTS, + extras_require={"dev": DEV} ) diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..0518f5f --- /dev/null +++ b/tasks.py @@ -0,0 +1,41 @@ +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 to given version""" + 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 adhere to SemVer standards." + ) + else: + sys.exit("Error: version not supplied. It should adhere to SemVer standards.") + + update_file( + TOP_DIR.joinpath("metadata.json"), (r'"version": ".+"', f'"version": "{version}"') + ) + + print(f"Bumped version to {version} !")