Convert CI script to python

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2022-11-15 19:57:26 +05:30
parent 9638af9515
commit c707ddea26
Signed by: baalajimaestro
GPG Key ID: F93C394FE9BBAFD5
3 changed files with 49 additions and 31 deletions

46
.ci.py Normal file
View File

@ -0,0 +1,46 @@
# Imports
from git import Repo
import subprocess
import os
from time import time
from pathlib import Path
# All the env vars
base_repo_user = os.environ.get("BASE_REPO_USERNAME")
base_repo_token = os.environ.get("BASE_REPO_TOKEN")
content_repo_git = os.environ.get("CONTENT_REPO_GIT")
content_repo_username = os.environ.get("CONTENT_REPO_USERNAME")
content_repo_token = os.environ.get("CONTENT_REPO_TOKEN")
out_dir = os.environ.get("OUT_DIR")
# Find absolute path of current directory
cwd = os.getcwd()
path = Path(cwd)
base_dir = str(path.parent.absolute())
current_time = str(int(time()))
# Clone our content
content_repo = Repo.clone_from(
f"https://${content_repo_username}:${content_repo_token}@git.baalajimaestro.me/baalajimaestro/${content_repo_git}.git",
f"{base_dir}/content",
)
# Initialise the repo for our out directory and add the base repo as remote
os.mkdir(out_dir)
os.chdir(out_dir)
repo = Repo.init(out_dir)
repo.create_remote(
"origin",
f"https://${base_repo_user}:${base_repo_token}@git.baalajimaestro.me/baalajimaestro/personal-website.git",
)
# Build the binaries
os.chdir(base_dir)
process = subprocess.run(["hugo", "--gc", "--minify", "-d", out_dir])
# Switch to out directory and push it up
os.chdir(out_dir)
repo.git.add(".")
repo.index.commit(f"[MaestroCI]: Binaries as of {current_time}")
repo.git.push("origin", "master", force=True)

View File

@ -5,11 +5,11 @@ image:
entrypoint: [""]
before_script:
- apk update && apk add curl git go
- apk update && apk add python3 py3-pip git go
- git config --global user.name baalajimaestro
- git config --global user.email me@baalajimaestro.me
- pip3 install GitPython
build-and-push:
stage: build-and-push
script:
- sh push.sh
- python3 .ci.py

28
push.sh
View File

@ -1,28 +0,0 @@
#!/bin/sh
BASE_DIR=$(pwd)
REPO="https://${CONTENT_REPO_USERNAME}:${CONTENT_REPO_TOKEN}@git.baalajimaestro.me/baalajimaestro/${CONTENT_REPO_GIT}"
git clone "$REPO" -b $(git ls-remote --tags --refs "$REPO" | tail -n1 | cut -d/ -f3) content
rm -rf content/.obsidian
rm -rf content/.gitlab-ci.yml
mkdir /public
cd /public
git init
git remote add origin https://${REPO_DEPLOY_USERNAME}:${REPO_DEPLOY_TOKEN}@git.baalajimaestro.me/baalajimaestro/personal-website.git
echo -e "Deploying updates to GitHub..."
cd $BASE_DIR
hugo --gc --minify -d /public
# Go To Public folder
cd /public
# Add changes to git.
git add .
# Commit changes.
git commit -m "[MaestroCI]: Binaries as of $(date +%Y-%m-%d_%H:%M:%S)" --signoff
# Push source and build repos.
git push -f origin master