Last active
July 14, 2021 20:20
-
-
Save nestarz/6259394b36e1d8c0b14a174cda95d3fc to your computer and use it in GitHub Desktop.
Self Hosted Deploy Solution Git Hooks post-update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $(git rev-parse --is-bare-repository) = true ] | |
then | |
REPOSITORY_BASENAME=$(basename "$PWD") | |
else | |
REPOSITORY_BASENAME=$(basename $(readlink -nf "$PWD"/..)) | |
fi | |
REPOSITORY_BASENAME=${REPOSITORY_BASENAME%.git} | |
TARGET="/home/username/$REPOSITORY_BASENAME/" | |
GIT_DIR=$PWD | |
BRANCHES=("master" "main") | |
echo TARGET is $TARGET | |
echo GIT_DIR is $GIT_DIR | |
while read oldrev newrev ref | |
do | |
branch=$(cut -d/ -f3 <<< "$ref") | |
# only checking out the master (or whatever branch you would like to deploy) | |
if [[ " ${BRANCHES[@]} " =~ " $branch " ]]; | |
then | |
mkdir -p $TARGET | |
echo "Ref $ref received. Deploying ${branch} branch to production..." | |
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $branch | |
docker run --rm --name build-site -v "$TARGET":/usr/src/app -w /usr/src/app node:16-alpine sh -c "yarn; yarn build --base=$BASE_PATH" | |
else | |
echo "Ref $ref received. Doing nothing: only the ${branch} branch may be deployed on this server." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before using the hook you must give gitea's user following permissions:
sudo chmod -R 777 /home/username/git/
sudo usermod -aG docker git