diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index bc835efb98..9b405ec5e1 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -2,7 +2,6 @@ # This workflow runs under any of the following conditions: # # - Push to the master branch -# - Push to the stable branch # - Publish release # # The following actions are performed: @@ -21,7 +20,6 @@ on: push: branches: - 'master' - - 'stable' jobs: @@ -29,12 +27,15 @@ jobs: build: runs-on: ubuntu-latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: - name: Check out repo uses: actions/checkout@v2 - name: Version Check run: | - python3 ci/check_version_number.py + python3 ci/version_check.py echo "git_commit_hash=$(git rev-parse --short HEAD)" >> $GITHUB_ENV echo "git_commit_date=$(git show -s --format=%ci)" >> $GITHUB_ENV - name: Run Unit Tests @@ -65,5 +66,12 @@ jobs: platforms: linux/amd64,linux/arm64,linux/arm/v7 push: true target: production - tags: inventree/inventree:${{ env.docker_tag }} - build-args: commit_hash=${{ env.git_commit_hash }},commit_date=${{ env.git_commit_date }},commit_tag=${{ env.docker_tag }} + tags: ${{ env.docker_tags }} + build-args: commit_hash="${{ env.git_commit_hash }}", commit_date="${{ env.git_commit_date }}" + - name: Push to Stable Branch + uses: ad-m/github-push-action@master + if: env.stable_release == 'true' + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: stable + force: true diff --git a/.github/workflows/qc_checks.yaml b/.github/workflows/qc_checks.yaml index 9bc0484fa0..7ff9315851 100644 --- a/.github/workflows/qc_checks.yaml +++ b/.github/workflows/qc_checks.yaml @@ -91,9 +91,6 @@ jobs: cache: 'pip' - name: Run pre-commit Checks uses: pre-commit/action@v2.0.3 - - name: Check version number - run: | - python3 ci/check_version_number.py python: name: Tests - inventree-python diff --git a/ci/check_version_number.py b/ci/version_check.py similarity index 52% rename from ci/check_version_number.py rename to ci/version_check.py index 39886e55bf..c9f05a1a13 100644 --- a/ci/check_version_number.py +++ b/ci/version_check.py @@ -4,20 +4,79 @@ Ensure that the release tag matches the InvenTree version number: master / main branch: - version number must end with 'dev' -stable branch: - - version number must *not* end with 'dev' - - version number cannot already exist as a release tag - tagged branch: - version number must match tag being built - version number cannot already exist as a release tag """ +import json import os import re import sys +import requests + + +def get_existing_release_tags(): + """Request information on existing releases via the GitHub API""" + + response = requests.get('https://api.github.com/repos/inventree/inventree/releases') + + if response.status_code != 200: + raise ValueError(f'Unexpected status code from GitHub API: {response.status_code}') + + data = json.loads(response.text) + + # Return a list of all tags + tags = [] + + for release in data: + tag = release['tag_name'].strip() + match = re.match(r"^.*(\d+)\.(\d+)\.(\d+).*$", tag) + + if len(match.groups()) != 3: + print(f"Version '{tag}' did not match expected pattern") + continue + + tags.append([int(x) for x in match.groups()]) + + return tags + + +def check_version_number(version_string): + """Check the provided version number. + + Returns True if the provided version is the 'newest' InvenTree release + """ + + print(f"Checking version '{version_string}'") + + # Check that the version string matches the required format + match = re.match(r"^(\d+)\.(\d+)\.(\d+)(?: dev)?$", version_string) + + if not match or len(match.groups()) != 3: + raise ValueError(f"Version string '{version_string}' did not match required pattern") + + version_tuple = [int(x) for x in match.groups()] + + # Look through the existing releases + existing = get_existing_release_tags() + + # Assume that this is the highest release, unless told otherwise + highest_release = True + + for release in existing: + if release == version_tuple: + raise ValueError(f"Duplicate release '{version_string}' exists!") + + if release > version_tuple: + highest_release = False + print(f"Found newer release: {str(release)}") + + return highest_release + + if __name__ == '__main__': here = os.path.abspath(os.path.dirname(__file__)) @@ -49,24 +108,12 @@ if __name__ == '__main__': print(f"InvenTree Version: '{version}'") + highest_release = check_version_number(version) + # Determine which docker tag we are going to use - docker_tag = None + docker_tags = None - if GITHUB_REF_TYPE == 'branch' and ('stable' in GITHUB_REF or 'stable' in GITHUB_BASE_REF): - print("Checking requirements for 'stable' release branch:") - - pattern = r"^\d+(\.\d+)+$" - result = re.match(pattern, version) - - if result is None: - print(f"Version number '{version}' does not match required pattern for stable branch") - sys.exit(1) - else: - print(f"Version number '{version}' matches stable branch") - - docker_tag = 'stable' - - elif GITHUB_REF_TYPE == 'tag': + if GITHUB_REF_TYPE == 'tag': # GITHUB_REF should be of th eform /refs/heads/ version_tag = GITHUB_REF.split('/')[-1] print(f"Checking requirements for tagged release - '{version_tag}':") @@ -77,7 +124,10 @@ if __name__ == '__main__': # TODO: Check if there is already a release with this tag! - docker_tag = version_tag + if highest_release: + docker_tags = [version_tag, 'stable'] + else: + docker_tags = [version_tag] elif GITHUB_REF_TYPE == 'branch': # Otherwise we know we are targetting the 'master' branch @@ -92,7 +142,7 @@ if __name__ == '__main__': else: print(f"Version number '{version}' matches development branch") - docker_tag = 'latest' + docker_tags = ['latest'] else: print("Unsupported branch / version combination:") @@ -102,13 +152,20 @@ if __name__ == '__main__': print("GITHUB_REF:", GITHUB_REF) sys.exit(1) - if docker_tag is None: + if docker_tags is None: print("Docker tag could not be determined") sys.exit(1) print(f"Version check passed for '{version}'!") - print(f"Docker tag: '{docker_tag}'") + print(f"Docker tags: '{docker_tags}'") # Ref: https://getridbug.com/python/how-to-set-environment-variables-in-github-actions-using-python/ with open(os.getenv('GITHUB_ENV'), 'a') as env_file: - env_file.write(f"docker_tag={docker_tag}\n") + + # Construct tag string + tags = ",".join([f"inventree/inventree:{tag}" for tag in docker_tags]) + + env_file.write(f"docker_tags={tags}\n") + + if GITHUB_REF_TYPE == 'tag' and highest_release: + env_file.write("stable_release=true\n")