From 76d69072b6abbf6b63ca9bb903ece6cea78863bc Mon Sep 17 00:00:00 2001 From: IsRza Date: Tue, 3 Oct 2023 21:01:38 +0400 Subject: [PATCH] CI/CD implemented for SRP --- .github/workflows/build.yml | 26 +++++++++++++++ .github/workflows/create_tag.yml | 50 +++++++++++++++++++++++++++++ .github/workflows/version_check.yml | 34 ++++++++++++++++++++ CI/check_version.py | 42 ++++++++++++++++++++++++ CI/config_parser.py | 11 +++++++ CI/echo_current_version.py | 4 +++ CI/echo_release_notes.py | 4 +++ CI/models.py | 6 ++++ Config.json | 4 +++ 9 files changed, 181 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/create_tag.yml create mode 100644 .github/workflows/version_check.yml create mode 100644 CI/check_version.py create mode 100644 CI/config_parser.py create mode 100644 CI/echo_current_version.py create mode 100644 CI/echo_release_notes.py create mode 100644 CI/models.py create mode 100644 Config.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..0a81adb --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,26 @@ +name: Build + +on: + push: + paths-ignore: + - "**/*.md" + - ".gitignore" + branches: + - master + pull_request: + paths-ignore: + - "**/*.md" + - ".gitignore" + branches: + - master + - develop + +jobs: + build: + runs-on: macos-latest + + steps: + - name: Build + uses: actions/checkout@v3 + + - run: xcodebuild -scheme SRP -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=16.2' diff --git a/.github/workflows/create_tag.yml b/.github/workflows/create_tag.yml new file mode 100644 index 0000000..2b95a7e --- /dev/null +++ b/.github/workflows/create_tag.yml @@ -0,0 +1,50 @@ +name: Create tag and release +on: + push: + branches: + - master + paths-ignore: + - "**/*.md" + - ".gitignore" +jobs: + create-tag: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.11" + + - name: Get version number and release notes + id: version_and_notes + run: | + current_version=$(python CI/echo_current_version.py) + release_notes=$(python CI/echo_release_notes.py) + echo "::set-output name=tag::$current_version" + echo "::set-output name=notes::$release_notes" + + - name: Create tag and release + uses: avakar/tag-and-release@v1 + id: tag_and_release + with: + tag_name: ${{ steps.version_and_notes.outputs.tag }} + body: ${{ steps.version_and_notes.outputs.notes }} + release_name: "v${{ steps.version_and_notes.outputs.tag }}" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Create branch from tag + uses: peterjgrainger/action-create-branch@v2.2.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + branch: "release/v${{ steps.version_and_notes.outputs.tag }}" + + - run: | + echo "Tag already present: ${{ env.TAG_EXISTS }}" + + - run: | + echo "Tag already present: ${{ steps.tag_create.outputs.tag_exists }}" diff --git a/.github/workflows/version_check.yml b/.github/workflows/version_check.yml new file mode 100644 index 0000000..933f7c1 --- /dev/null +++ b/.github/workflows/version_check.yml @@ -0,0 +1,34 @@ +name: Version Check + +on: + pull_request: + paths-ignore: + - "**/*.md" + - ".gitignore" + branches: + - master + +jobs: + version-check: + runs-on: ubuntu-latest + + steps: + - name: Get Latest Version + id: latest_release_version + uses: pozetroninc/github-action-get-latest-release@v0.7.0 + with: + owner: PB-Digital + repo: srp-ios + + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.11" + + - name: Compare Versions + run: | + release_version=${{ steps.latest_release_version.outputs.release }} + python CI/check_version.py $release_version diff --git a/CI/check_version.py b/CI/check_version.py new file mode 100644 index 0000000..8223fdc --- /dev/null +++ b/CI/check_version.py @@ -0,0 +1,42 @@ +from models import Config +from config_parser import get_raw_config +from typing import Any +from sys import argv +import json + +def parse_version(version_string: str) -> list[int]: + return [ int(i) for i in version_string.split('.') ] + +def get_current_version(config: Config) -> list[int]: + return parse_version(config.version) + +def compare_versions(release: list[int], current: list[int]) -> bool: + if len(release) != len(current): + print('\n\033[1;31m' + '❌ Version formats does not match!\n') + exit(1) + + for r, c in zip(release, current): + if c > r: + return True + elif c < r: + return False + + return False + +def main(): + release_version_string: str = argv.pop() + + release_version: list[int] = parse_version(release_version_string) + current_version: list[int] = get_current_version(get_raw_config()) + + is_valid: bool = compare_versions(release_version, current_version) + + if is_valid: + print('\n\033[1;32m' + '✅ Valid\n') + else: + print('\n\033[1;31m' + '❌ Not valid\n') + exit(2) + + +if __name__ == '__main__': + main() diff --git a/CI/config_parser.py b/CI/config_parser.py new file mode 100644 index 0000000..4d9ad2f --- /dev/null +++ b/CI/config_parser.py @@ -0,0 +1,11 @@ +import json +from typing import Any +from models import Config + +def get_raw_config() -> Config: + with open('Config.json', 'r') as file: + config = json.load(file) + return Config( + version = config['version'], + release_notes = config['release_notes'] + ) diff --git a/CI/echo_current_version.py b/CI/echo_current_version.py new file mode 100644 index 0000000..059896a --- /dev/null +++ b/CI/echo_current_version.py @@ -0,0 +1,4 @@ +from config_parser import get_raw_config + +if __name__ == '__main__': + print(get_raw_config().version) diff --git a/CI/echo_release_notes.py b/CI/echo_release_notes.py new file mode 100644 index 0000000..acffb9f --- /dev/null +++ b/CI/echo_release_notes.py @@ -0,0 +1,4 @@ +from config_parser import get_raw_config + +if __name__ == '__main__': + print(get_raw_config().release_notes) diff --git a/CI/models.py b/CI/models.py new file mode 100644 index 0000000..1cd91ea --- /dev/null +++ b/CI/models.py @@ -0,0 +1,6 @@ +from dataclasses import dataclass + +@dataclass +class Config: + version: str + release_notes: str diff --git a/Config.json b/Config.json new file mode 100644 index 0000000..8478d35 --- /dev/null +++ b/Config.json @@ -0,0 +1,4 @@ +{ + "version": "3.8.2", + "release_notes": "CI/CD integrated" +}