Skip to content

Instantly share code, notes, and snippets.

@derhasi
Last active October 14, 2019 13:43
Show Gist options
  • Save derhasi/20ad9f7461192dfdc081c3e31d13df8d to your computer and use it in GitHub Desktop.
Save derhasi/20ad9f7461192dfdc081c3e31d13df8d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
# Create build commit by pulling the remote branch and copying over the files from the project build
# which contain data in the docroot we usually do not commit.
LAST_COMMIT_INFO=$(git log -1 --pretty="[%h] (%an) %B")
LAST_COMMIT_USER=$(git show -s --format="%an")
LAST_COMMIT_USER_EMAIL=$(git show -s --format="%ae")
CURRENT_REV=$(git rev-parse --abbrev-ref HEAD)
# We store directory values, so we can restore them later.
CURRENT_PWD=$(pwd)
# This is the location of this exact file.
SCRIPT_FILE="$CURRENT_PWD"/${0}
SCRIPT_DIR=$(dirname "$SCRIPT_FILE")
CI_PROJECT_DIR=$(dirname "$SCRIPT_DIR")
BUILD_DIR="build-dir";
cd "$CI_PROJECT_DIR";
rm -rf "$BUILD_DIR";
GIT_REMOTE_DEVELOP="git@gitlab.com:example/example.git"
TARGET_BRANCH="$CURRENT_REV-build"
git clone "${GIT_REMOTE_DEVELOP}" "$BUILD_DIR"
cd "$BUILD_DIR";
git checkout -B "$TARGET_BRANCH"
git config user.email "$LAST_COMMIT_USER_EMAIL"
git config user.name "$LAST_COMMIT_USER"
git config --global push.default simple
rsync --archive --delete "${CI_PROJECT_DIR}/" "./" --exclude=".git" --exclude=".cache"
# Check if certain files exist to prevent pushing changes from an empty artifact build
# which has been expired.
echo "Checking for needed files."
ls -al docroot/
if [ ! -f "docroot/index.php" ]
then
echo "Missing files, aborting deployment."
exit 1;
fi
# Use the seperate .build.ignore to commit files in docroot.
mv .gitignore .origin.gitignore
mv .build.gitignore .gitignore
git add -A .
git commit --quiet -m "$LAST_COMMIT_INFO"
# push to remote branch
git push origin "$TARGET_BRANCH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment