Question

How do I automatically run a script whenever a specified remote git repository (on bitbucket) changes?

The script will contain lines to git pull dependant repositories; run tests; and if tests pass, deploy then reload relevant services.

Was it helpful?

Solution

The best solution is to use a Bitbucket hook to get notified when something changes in the remote repository and probably the easiest way to implement this is to have a CI software to listen for the notification and run your bar script.

The complicated way is to write code yourself to receive the notification; I found an example that you might use as a starting point (and another one, even more complete)

And, of course, you could always periodically poll for remote changes, but that's not very efficient. Best way is to use the hooks.

OTHER TIPS

Using polling, you can check for changes on a branch from a cron job.

The Makefile content (supposed to be in the root folder of a git project)


BRANCH?=main

need_update:
    git fetch
    if [ `git rev-list HEAD...origin/${BRANCH} --count` -eq '0' ]; then \
        echo "No updates, aborting build!\n"; \
        exit 1; \
    fi

fetch:
    git pull

build:
    docker-compose build

restart:
    docker-compose down
    docker-compose up -d

upgrade: need_update fetch build restart

Then in your crontab add an interval and something like su -c 'cd ~/<git-project-path> && make upgrade' <user with git credentials>

Poorman CD is served :P

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top