Skip to content

Commit

Permalink
✅ Fix deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Sep 18, 2024
1 parent a261846 commit 17c4846
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
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
44 changes: 21 additions & 23 deletions bin/mfconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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", "[email protected]"])
git(["config", "user.name", "Scott Lahteine"])
def changes(): return git(["status", "--porcelain"]).stdout != ""

# Stash uncommitted changes at the destination?
if changes():
Expand Down Expand Up @@ -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...")
Expand All @@ -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()
Expand All @@ -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", "."])
Expand Down Expand Up @@ -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}"])

Expand Down

0 comments on commit 17c4846

Please sign in to comment.