From 35b9fefee43015a9a91ca24c0136e61150a1ffd6 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Fri, 12 Jan 2024 00:55:09 +0100 Subject: [PATCH] Add tag based CD trigger --- .github/workflows/cd.yml | 29 ++++++++++++++++++ .github/workflows/tag_validator.py | 49 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 .github/workflows/cd.yml create mode 100644 .github/workflows/tag_validator.py diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..9258f93 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,29 @@ +name: Publish Custom App + +on: + release: + types: [published] + branches: + - main + +jobs: + publish: + runs-on: macos-13 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + path: custom + + - name: Set tag variable as an output + id: vars + run: + | + echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT + + - name: Validate tag + run: + | + cd custom + python .github/workflows/tag_validator.py ${{ steps.vars.outputs.tag }} diff --git a/.github/workflows/tag_validator.py b/.github/workflows/tag_validator.py new file mode 100644 index 0000000..0701e33 --- /dev/null +++ b/.github/workflows/tag_validator.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import argparse +import re +from pathlib import Path +import sys + + +def is_valid(tag): + # Regex verify the tag format + pattern = re.compile( + r'^(?P\w+)_(?P\d+)(?:_(?P\w+))?$') + match = pattern.match(tag) + + if match: + groups = match.groupdict() + brand = groups.get('brand_folder') + build_nr = int(groups.get('build_nr')) + if Path(brand).is_dir(): + print(f"valid tag found: {tag} (brand: { + brand}, build number: {build_nr})") + return True + else: + exist_with_error(f"The directory of the tag: '{ + brand}' doesn't exist") + else: + exist_with_error(f"Invalid tag: {tag}") + return False + + +def exist_with_error(msg): + print(f"Error: {msg}") + sys.exit(1) + + +def main(): + parser = argparse.ArgumentParser( + description="A github tag validator for custom apps") + parser.add_argument( + "tag", + help="The github tag to be verified", + type=str + ) + args = parser.parse_args() + return is_valid(args.tag) + + +if __name__ == "__main__": + main()