Skip to content

Instantly share code, notes, and snippets.

@fmarcia
Last active September 24, 2020 08:59
Show Gist options
  • Save fmarcia/26a34ae878fbaf804550018d38485d46 to your computer and use it in GitHub Desktop.
Save fmarcia/26a34ae878fbaf804550018d38485d46 to your computer and use it in GitHub Desktop.
Automatic push-to-production
#!/bin/bash
# P R E P A R E E N V I R O N M E N T
# prepare playground
playground=/tmp/playground
mkdir -p $playground
cd $playground
# setup developer repository
mkdir $playground/dev-repo
cd $playground/dev-repo
git init
cat > master-file << EOF
au clair de la lune
EOF
git add master-file
git commit -m 'First commit to master branch'
rm master-file
git checkout -b production
cat > prod-file << EOF
mon ami Pierrot
EOF
git add prod-file
git commit -m 'First commit to production branch'
# initialize pseudo-gitlab bare repository
cd $playground
git init gitlab-repo --bare
# push everything from developer repository to gitlab repository
cd $playground/dev-repo
git remote add origin $playground/gitlab-repo
git push --all origin
# clone gitlab repository to production repository
cd $playground
git clone gitlab-repo prod-repo -b production
# S E T U P S Y N C H R O N I Z A T I O N
# set production repository to update when receiving push
cd $playground/prod-repo
git config receive.denyCurrentBranch updateInstead
# add post-receive hook to gitlab repository
cd $playground/gitlab-repo/hooks
cat > post-receive <<EOF
#!/bin/bash
while read oldrev newrev refname
do
branch=\$(git rev-parse --symbolic --abbrev-ref \$refname)
if [ "\$branch" == "production" ]; then
git push prod-remote production:production
fi
done
EOF
chmod +x post-receive
# add production repository as remote in gitlab repository
cd $playground/gitlab-repo
git remote add prod-remote $playground/prod-repo
# C H E C K
# update production file
cd $playground/dev-repo
cat >> prod-file << EOF
prête-moi ta plume
EOF
# commit and push to gitlab repository
git commit -am 'Second commit to production branch'
git push origin -u production
# done: production file should be updated in production repository
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment