|
| 1 | +#!/usr/bin/env sh |
| 2 | +# Use with Gitlab CI jobs from scripts/delivery/.gitlab-ci.delivery_example.yml |
| 3 | + |
| 4 | +echo -e "\n- Start of delivery script" |
| 5 | +# set -x #echo on |
| 6 | + |
| 7 | +# Defining functions # For local use only, NOT FOR USE IN CI |
| 8 | + |
| 9 | +CURRENT_TAG_FUNC() |
| 10 | + { |
| 11 | + git describe --tags $(git rev-list --tags --max-count=1) |
| 12 | + } |
| 13 | + |
| 14 | +# Defining variables |
| 15 | +echo -e "- Defining variables...\n" |
| 16 | + |
| 17 | +PACKAGE_DIR=$(pwd)/files_to_be_deployed |
| 18 | +echo -e "PACKAGE_DIR = $PACKAGE_DIR" |
| 19 | + |
| 20 | +# TARGET_GIT_REPO=XXX@XXX.git # For local use only, NOT FOR USE IN CI |
| 21 | +# For CI use, var is moved to CI job itself, so that same script can be used to clone on multiple repos |
| 22 | +echo -e "TARGET_GIT_REPO = $TARGET_GIT_REPO" |
| 23 | + |
| 24 | +# TARGET_GIT_REPO_BRANCH=master # For local use only, NOT FOR USE IN CI |
| 25 | +# For CI use, var is moved to CI job itself, so that same script can be used to clone on multiple repos |
| 26 | +echo -e "TARGET_GIT_REPO_BRANCH = $TARGET_GIT_REPO_BRANCH" |
| 27 | + |
| 28 | +# TARGET_GIT_REPO_TYPE=GITLAB # For local use only, NOT FOR USE IN CI |
| 29 | +# For CI use, var is moved to CI job itself, so that same script can be used to clone on multiple repos |
| 30 | +echo -e "TARGET_GIT_REPO_TYPE = $TARGET_GIT_REPO_TYPE" |
| 31 | + |
| 32 | +# CURRENT_TAG=$(CURRENT_TAG_FUNC) # For local use only, NOT FOR USE IN CI |
| 33 | +CURRENT_TAG="$CI_COMMIT_REF_NAME" # For CI use only, using Gitlab predefined variable |
| 34 | +echo -e "CURRENT_TAG = $CURRENT_TAG" |
| 35 | + |
| 36 | +# GIT_USER_EMAIL="XXX@XXX.com" # For local use only, NOT FOR USE IN CI |
| 37 | +echo -e "GIT_USER_EMAIL = $GIT_USER_EMAIL" # For CI use only, using Gitlab custom variable |
| 38 | + |
| 39 | +# GIT_USER_NAME="XXX CI/CD" # For local use only, NOT FOR USE IN CI |
| 40 | +echo -e "GIT_USER_NAME = $GIT_USER_NAME" # For CI use only, using Gitlab custom variable |
| 41 | + |
| 42 | +# Preparing delivery dir |
| 43 | +echo -e "- Preparing delivery dir...\n" |
| 44 | +mkdir "$PACKAGE_DIR" |
| 45 | +cd "$PACKAGE_DIR" |
| 46 | + |
| 47 | +# Initialising external git repo |
| 48 | +echo -e "- Initialising external git repo...\n" |
| 49 | +git init && git config --local core.excludesfile false && git config --local core.fileMode true |
| 50 | +git remote add origin $TARGET_GIT_REPO |
| 51 | +git pull origin master |
| 52 | +git fetch |
| 53 | +git checkout $TARGET_GIT_REPO_BRANCH |
| 54 | +git config --local user.email "$GIT_USER_EMAIL" |
| 55 | +git config --local user.name "$GIT_USER_NAME" |
| 56 | + |
| 57 | +# Deleting files in delivery dir |
| 58 | +echo -e "- Deleting files in delivery dir...\n" |
| 59 | +set -x #echo on |
| 60 | +find -maxdepth 1 ! -name '.git' -exec rm -rv {} \; 1> /dev/null |
| 61 | +ls -lah |
| 62 | + |
| 63 | +# Copying files to delivery dir |
| 64 | +echo -e "- Copying files to delivery dir...\n" |
| 65 | +rsync -av --quiet --progress ../. . --exclude .git/ --exclude files_to_be_deployed/ |
| 66 | + |
| 67 | +# Making sure everything needed will be included in commit |
| 68 | +echo -e "- Making sure everything needed will be included in commit...\n" |
| 69 | +echo -e "-- Deleting all .gitignore files...\n" |
| 70 | +mv .gitignore .gitig |
| 71 | +find . -name '.gitignore' -type f | wc -l |
| 72 | +find . -name '.gitignore' -type f -exec rm {} + |
| 73 | +# mv .gitig .gitignore // Note that we don't keep the .gitignore file like we usualy do, all project content must be commited here |
| 74 | + |
| 75 | +echo -e "-- Deleting all .git directories except root one...\n" |
| 76 | +mv .git .got |
| 77 | +find . -name '.git' -type d | wc -l |
| 78 | +find . -name '.git' -type d -exec rm -rf {} + |
| 79 | +mv .got .git |
| 80 | + |
| 81 | +# Removing local DB settings from settings.php |
| 82 | +sed -i -e "/$databases\['default'\]\['default'\] = array (/,/)/d" web/sites/default/settings.php |
| 83 | +# Adding install profile value in settings.php |
| 84 | +echo "\$settings['install_profile'] = 'sdd';" >> web/sites/default/settings.php |
| 85 | +# Adding settings.local.php to web dir |
| 86 | +cp settings/settings.local.php web/sites/default/settings.local.php |
| 87 | +sed -i "/settings.local.php';/s/# //g" web/sites/default/settings.php |
| 88 | + |
| 89 | +# Preventing platform.sh error "Application name 'app' is not unique" |
| 90 | +if [ "$TARGET_GIT_REPO_TYPE" = "PLATFORM.SH" ]; then |
| 91 | + echo -e "- Preventing platform.sh error "Application name 'app' is not unique"...\n" |
| 92 | + sed -i "s|name: 'app'|name: 'XXX'|g" ../.platform.app.yaml |
| 93 | +fi |
| 94 | +# Moving hosting env files back at project root |
| 95 | +if [ "$TARGET_GIT_REPO_TYPE" = "GITLAB" ]; then |
| 96 | + echo -e "- Moving hosting env files back at project root...\n" |
| 97 | + mv .gitlab-ci.yml .gitlab-ci-backup.yml |
| 98 | + mv hosting/* hosting/.* . |
| 99 | +fi |
| 100 | + |
| 101 | +# Commiting to external repo |
| 102 | +echo -e "- Commiting to external repo...\n" |
| 103 | +git add -A 1> /dev/null |
| 104 | +git status -s |
| 105 | +git commit --quiet -m "$CURRENT_TAG" |
| 106 | +git push origin $TARGET_GIT_REPO_BRANCH --quiet |
| 107 | +git tag "$CURRENT_TAG" |
| 108 | +git push --tag |
| 109 | + |
| 110 | +# Cleaning delivery dir |
| 111 | +echo -e "- Cleaning delivery dir...\n" |
| 112 | +cd .. |
| 113 | +rm -rf "$PACKAGE_DIR" |
| 114 | + |
| 115 | +echo -e "- End of delivery script" |
| 116 | + |
0 commit comments