Put microcicd.py & redeploy.sh in your git project.
Run git config credential.helper store
as the (systemd) user that will run the script.
Edit the files with user/repo info and move them in to possision.
Enable --now microcicd.service and update your master brach.
Disclaimer, security or tests have not been considered here, I needed something fast and dirty. Webhooks is the prefered method. As well as tokens for AUTH.
import sys
import subprocess
from time import sleep
from github import Github
# Log in details
g = Github('USER', 'PASSWORD.')
def main():
global g
# Getting current commit SHA
commit = g.get_repo("user/repo").get_branch("master")
print('Micro CI/CD up and looking for changes\nPress Ctl-c to exit...')
while True:
try:
latest = g.get_repo("USER/REPO").get_branch("master")
if commit.commit != latest.commit:
commit = latest
print('####################### NEW COMMIT DETECTED #######################')
subprocess.run(['sh', 'redeploy.sh'])
else:
sleep(60)
except KeyboardInterrupt:
sys.exit(0)
if __name__ == '__main__':
main()
#!/usr/bin/env bash
# Store git credentials ("git config credential.helper store")
# Run systemd unit as user with stored credentials
docker-compose kill
git pull
docker-compose up -d
[Unit]
Description=Micro CICD
Documentation=""
After=network.target
Requires=docker.socket
[Service]
RestartSec=10
Restart=on-failure
User=<user with git store>
Group=<user with git store>
WorkingDirectory=/path/to/project
ExecStart=/usr/bin/python3 /path/to/project/microcicd.py
[Install]
WantedBy=multi-user.target