From b3184fbebce405bdf35ccf5f4e29757ae6484246 Mon Sep 17 00:00:00 2001 From: Miku <26039249+xMikux@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:01:04 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E6=96=B0=E5=A2=9E=E9=A9=97=E8=AD=89?= =?UTF-8?q?=E6=B8=AC=E8=A9=A6=E7=89=88=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20(#358)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/scripts/modrinth_verify_exist.py | 34 +++++++++++++++++++ .github/workflows/CI-Pack-Beta.yml | 11 ++++-- .../workflows/Reusable-VerifyExistVersion.yml | 26 ++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 .github/scripts/modrinth_verify_exist.py create mode 100644 .github/workflows/Reusable-VerifyExistVersion.yml diff --git a/.github/scripts/modrinth_verify_exist.py b/.github/scripts/modrinth_verify_exist.py new file mode 100644 index 000000000..8e84d9820 --- /dev/null +++ b/.github/scripts/modrinth_verify_exist.py @@ -0,0 +1,34 @@ +import os +import sys +import json +import requests + +def main(version): + modrinth_api = f"https://api.modrinth.com/v2/project/cF5VXmkW/version/{version}" + github_project = os.environ["GITHUB_REPOSITORY"] + headers = {"User-Agent": f"{github_project} (Verify Exist Script)"} + + response = requests.get(modrinth_api, headers=headers) + + if response.status_code == 200: + json_data = json.loads(response.text) + version_number = json_data["version_number"] + date_published = json_data["date_published"] + + # Set step output + with open(os.environ["GITHUB_OUTPUT"], "w") as f: + f.write("version_exist=true") + + # Print a warning message + print(f"::warning::版本 {version_number} 已存在!發佈時間 {date_published}。") + else: + print("版本未存在!") + # Set step output + with open(os.environ["GITHUB_OUTPUT"], "w") as f: + f.write("version_exist=false") + +if __name__ == "__main__": + git_version = sys.argv[1] + if len(sys.argv) < 2: + sys.exit(1) + main(git_version) diff --git a/.github/workflows/CI-Pack-Beta.yml b/.github/workflows/CI-Pack-Beta.yml index 52bb56741..78bcf950b 100644 --- a/.github/workflows/CI-Pack-Beta.yml +++ b/.github/workflows/CI-Pack-Beta.yml @@ -40,6 +40,12 @@ jobs: MSG=$(git show -s --format=%s) echo "msg=$MSG" >> "$GITHUB_OUTPUT" + verify-exist: + name: Verify Version Exist + if: github.repository == 'xMikux/ModsTranslationPack' + + uses: ./.github/workflows/Reusable-VerifyExistVersion.yml + pack-beta: name: Pack ${{ matrix.version }} needs: [ matrix-setup ] @@ -59,12 +65,13 @@ jobs: modrinth-publish: name: Modrinth Beta ${{ matrix.version }} - needs: [ pack-beta, matrix-setup, changelog-commit ] + needs: [ pack-beta, matrix-setup, changelog-commit, verify-exist ] + if: ${{ needs.verify-exist.outputs.version_exist != 'true' }} strategy: max-parallel: 1 matrix: ${{ fromJson(needs.matrix-setup.outputs.matrix) }} - + uses: ./.github/workflows/Reusable-ModrinthPublish.yml with: matrix_version: ${{ matrix.version }} diff --git a/.github/workflows/Reusable-VerifyExistVersion.yml b/.github/workflows/Reusable-VerifyExistVersion.yml new file mode 100644 index 000000000..6a49d7200 --- /dev/null +++ b/.github/workflows/Reusable-VerifyExistVersion.yml @@ -0,0 +1,26 @@ +name: Reusable | Verify Exist Version + +on: + workflow_call: + outputs: + version_exist: + description: "Version Exist" + value: ${{ jobs.verify_exist.outputs.version_exist }} + +jobs: + verify_exist: + name: Verify Version Exist + runs-on: ubuntu-latest + + outputs: + version_exist: ${{ steps.verify.outputs.version_exist }} + + steps: + - name: Checking Repository + uses: actions/checkout@v3 + + - name: Check version exist + id: verify + run: | + GIT_VERSION=git-$(git rev-parse --short HEAD) + python .github/scripts/modrinth_verify_exist.py $GIT_VERSION