Skip to content

Instantly share code, notes, and snippets.

@izznogooood
Last active March 6, 2019 13:18
Show Gist options
  • Save izznogooood/338841aec5fc1fe0abca47a45eec87e3 to your computer and use it in GitHub Desktop.
Save izznogooood/338841aec5fc1fe0abca47a45eec87e3 to your computer and use it in GitHub Desktop.
GitHub Micro CICD Script

Rapid development and not ment for any sort of production

(Or over time...)

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.

microcicd.py

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()

redeploy.sh

#!/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

/etc/systemd/system/microcicd.service

[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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment