From 89236d8ed6b7e04bf62e2d4a2e507f2c0125f694 Mon Sep 17 00:00:00 2001 From: Lev Vereshchagin Date: Thu, 16 Dec 2021 19:53:24 +0300 Subject: [PATCH] Use python script to get latest tags in CI --- .github/scripts/WIP_generate_pwd.py | 43 +++++++++++++++++ .github/scripts/get-latest-tags.sh | 18 ------- .github/scripts/get_latest_tags.py | 74 +++++++++++++++++++++++++++++ .github/workflows/main.yml | 4 +- TODO.md | 1 - 5 files changed, 118 insertions(+), 22 deletions(-) create mode 100644 .github/scripts/WIP_generate_pwd.py delete mode 100755 .github/scripts/get-latest-tags.sh create mode 100644 .github/scripts/get_latest_tags.py diff --git a/.github/scripts/WIP_generate_pwd.py b/.github/scripts/WIP_generate_pwd.py new file mode 100644 index 00000000..31d87dee --- /dev/null +++ b/.github/scripts/WIP_generate_pwd.py @@ -0,0 +1,43 @@ +import shutil +import subprocess + +from get_latest_tags import get_latest_tag, update_env + + +def prepare_env(): + version = "13" + frappe_tag = get_latest_tag("frappe", version) + erpnext_tag = get_latest_tag("erpnext", version) + shutil.copyfile("example.env", ".env") + update_env(".env", frappe_tag, erpnext_tag) + + +def generate_compose_file(): + output = subprocess.check_output( + ( + "docker-compose", + "-f", + "compose.yaml", + "-f", + "overrides/compose.erpnext.yaml", + "--env-file", + ".env", + "config", + ), + encoding="UTF-8", + ) + text = f'version: "3.9"\n\n{output}' + with open("pwd.yml", "a+") as f: + f.seek(0) + f.truncate() + f.write(text) + + +def main() -> int: + prepare_env() + generate_compose_file() + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.github/scripts/get-latest-tags.sh b/.github/scripts/get-latest-tags.sh deleted file mode 100755 index 3459da68..00000000 --- a/.github/scripts/get-latest-tags.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -set -e -set -x - -get_tag() { - tags=$(git ls-remote --refs --tags --sort='v:refname' "https://github.com/$1" "v$2.*") - tag=$(echo "$tags" | tail -n1 | sed 's/.*\///') - echo "$tag" -} - -FRAPPE_VERSION=$(get_tag frappe/frappe "$VERSION") -ERPNEXT_VERSION=$(get_tag frappe/erpnext "$VERSION") - -cat <>"$GITHUB_ENV" -FRAPPE_VERSION=$FRAPPE_VERSION -ERPNEXT_VERSION=$ERPNEXT_VERSION -EOL diff --git a/.github/scripts/get_latest_tags.py b/.github/scripts/get_latest_tags.py new file mode 100644 index 00000000..e4a7b1dc --- /dev/null +++ b/.github/scripts/get_latest_tags.py @@ -0,0 +1,74 @@ +from __future__ import annotations + +import argparse +import json +import os +import re +import subprocess +import sys +from typing import Literal + +Repo = Literal["frappe", "erpnext"] +MajorVersion = Literal["12", "13", "develop"] + + +def get_latest_tag(repo: Repo, version: MajorVersion) -> str: + if version == "develop": + return "develop" + regex = rf"v{version}.*" + refs = subprocess.check_output( + ( + "git", + "ls-remote", + "--refs", + "--tags", + "--sort=v:refname", + f"https://github.com/frappe/{repo}", + str(regex), + ), + encoding="UTF-8", + ).split()[1::2] + + if not refs: + raise RuntimeError(f'No tags found for version "{regex}"') + ref = refs[-1] + matches: list[str] = re.findall(regex, ref) + if not matches: + raise RuntimeError(f'Can\'t parse tag from ref "{ref}"') + return matches[0] + + +def update_env(file_name: str, frappe_tag: str, erpnext_tag: str | None = None): + text = f"\nFRAPPE_VERSION={frappe_tag}" + if erpnext_tag: + text += f"\nERPNEXT_VERSION={erpnext_tag}" + + with open(file_name, "a") as f: + f.write(text) + + +def _print_resp(frappe_tag: str, erpnext_tag: str | None = None): + print(json.dumps({"frappe": frappe_tag, "erpnext": erpnext_tag})) + + +def main(_args: list[str]) -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--repo", choices=["frappe", "erpnext"], required=True) + parser.add_argument("--version", choices=["12", "13", "develop"], required=True) + args = parser.parse_args(_args) + + frappe_tag = get_latest_tag("frappe", args.version) + if args.repo == "erpnext": + erpnext_tag = get_latest_tag("erpnext", args.version) + else: + erpnext_tag = None + + file_name = os.getenv("GITHUB_ENV") + if file_name: + update_env(file_name, frappe_tag, erpnext_tag) + _print_resp(frappe_tag, erpnext_tag) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv[1:])) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eeb64547..d71e0962 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -65,9 +65,7 @@ jobs: uses: ndeloof/install-compose-action@4a33bc31f327b8231c4f343f6fba704fedc0fa23 - name: Get latest versions - run: ./.github/scripts/get-latest-tags.sh - env: - VERSION: ${{ matrix.version }} + run: ./.github/scripts/get_latest_tags.py --repo erpnext --version ${{ matrix.version }} - name: Build uses: docker/bake-action@v1.6.0 diff --git a/TODO.md b/TODO.md index a81d8c79..1fbff205 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ - [ ] Docs - [ ] Test with helm chart -- [ ] CI (also: build only published tag) - [ ] Play With Docker generation - [ ] Custom app compose and dockerfile examples