From 17c4846dda19c72996ede369715bb16e6aea8b9c Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 18 Sep 2024 11:44:48 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Fix=20deploy=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 17 ++++++++++++-- bin/mfconfig | 44 +++++++++++++++++------------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8257eacdfa5..75b41a5342a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -21,7 +21,20 @@ jobs: steps: - name: Check out uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch all history for all branches and tags + token: ${{ secrets.GITHUB_TOKEN }} # Use the built-in token for authentication - # Run the mfconfig script with CI action - - name: Deploy bugfix-2.1.x + - name: Fetch all branches + run: git fetch --all + + - name: Set up Git identity + run: | + git config --global user.email "github-actions@github.com" + git config --global user.name "GitHub Actions" + + - name: Build bugfix-2.1.x run: bin/mfconfig CI + + - name: Push bugfix-2.1.x + run: git push -f origin WORK:bugfix-2.1.x diff --git a/bin/mfconfig b/bin/mfconfig index 537eda305b3..4e3f3e9cfa0 100755 --- a/bin/mfconfig +++ b/bin/mfconfig @@ -38,7 +38,7 @@ IMPORT = sys.argv[2] if len(sys.argv) > 2 else 'import-2.1.x' EXPORT = sys.argv[3] if len(sys.argv) > 3 else 'bugfix-2.1.x' # Get repo paths -CI = False +CI = os.environ.get('GITHUB_ACTIONS') == 'true' if ACTION == 'CI': _REPOS = "." REPOS = Path(_REPOS) @@ -68,20 +68,23 @@ if not CONFIGREPO.exists(): sys.exit(1) # Run git within CONFIGREPO -GITSTDERR = None if DEBUG else subprocess.DEVNULL +GITSTDERR = subprocess.PIPE if DEBUG else subprocess.DEVNULL def git(etc): - if DEBUG: - print(f"> git {' '.join(etc)}") - if etc[0] == "push": - info("*** DRY RUN ***") - return subprocess.run(["echo"]) - return subprocess.run(["git"] + etc, cwd=CONFIGREPO, stdout=subprocess.PIPE, stderr=GITSTDERR) + if DEBUG: print(f"> git {' '.join(etc)}") + + result = subprocess.run(["git"] + etc, cwd=CONFIGREPO, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + + if result.returncode != 0: + print(f"Git command failed: git {' '.join(etc)}") + print(f"Error output: {result.stderr}") + + return result # Get the current branch name def branch(): return git(["rev-parse", "--abbrev-ref", "HEAD"]) # git add . ; git commit -m ... -def commit(msg, who="."): git(["add", who]) ; return git(["commit", "-m", msg]) +def commit(msg, who="."): git(["add", who]) ; return git(["commit", "-m", f'"{msg}"']) # git checkout ... def checkout(etc): return git(["checkout"] + ([etc] if isinstance(etc, str) else etc)) @@ -90,11 +93,7 @@ def checkout(etc): return git(["checkout"] + ([etc] if isinstance(etc, str) else def gitbd(name): return git(["branch", "-D", name]).stdout # git status --porcelain : to check for changes -def changes(): return git(["status", "--porcelain"]).stdout.decode().strip() - -# Configure git user -git(["config", "user.email", "thinkyhead@users.noreply.github.com"]) -git(["config", "user.name", "Scott Lahteine"]) +def changes(): return git(["status", "--porcelain"]).stdout != "" # Stash uncommitted changes at the destination? if changes(): @@ -179,9 +178,9 @@ if ACTION == "init": f.writelines(outlines) # Create a fresh 'WORK' as a copy of 'init-repo' (README, LICENSE, etc.) - gitbd("WORK") - if CI: git(["fetch", "origin", "init-repo"]) - checkout(["init-repo", "-b", "WORK"]) + if not CI: gitbd("WORK") + REMOTE = "origin" if CI else "upstream" + checkout([f"{REMOTE}/init-repo", "-b", "WORK"]) # Copy default configurations into the repo info("Create configs in default state...") @@ -193,7 +192,7 @@ if ACTION == "init": shutil.copy(TEMPCON / "default" / fn.name, CONFIGCON / relpath) # DEBUG: Commit the reset for review - if DEBUG: commit("[DEBUG] Create defaults") + if DEBUG > 1: commit("[DEBUG] Create defaults") def replace_in_file(fn, search, replace): with open(fn, 'r') as f: lines = f.read() @@ -203,7 +202,7 @@ if ACTION == "init": replace_in_file(CONFIGREPO / "README.md", "%VERSION%", EXPORT.replace("release-", "")) # Commit all changes up to now; amend if not debugging - if DEBUG: + if DEBUG > 1: commit("[DEBUG] Update README.md version", "README.md") else: git(["add", "."]) @@ -234,16 +233,15 @@ if ACTION == "init": shutil.rmtree(TEMP) # Push to the remote (if desired) - if CI: - PUSH_YES = 'Y' - else: + PUSH_YES = 'N' + if not CI: print() PUSH_YES = input(f"Push to upstream/{EXPORT}? [y/N] ") print() REMOTE = "origin" if CI else "upstream" - if PUSH_YES in ('Y','y'): + if PUSH_YES.upper() in ('Y','YES'): info("Push to remote...") git(["push", "-f", REMOTE, f"WORK:{EXPORT}"])