|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Build artifact from the github repo and push it to the hosting provider remote repo. |
| 4 | + |
| 5 | + |
| 6 | +# Fail immediately when any command fails: |
| 7 | +set -e |
| 8 | + |
| 9 | +# Debug output? |
| 10 | +if [ "${DEBUG}" == "true" ]; then |
| 11 | + set -x |
| 12 | + env |
| 13 | +fi |
| 14 | + |
| 15 | +# Verify expected variables are defined: |
| 16 | +test -n "${GIT_REMOTE}" |
| 17 | +test -n "${GIT_SSH_PRIVATE_KEY}" |
| 18 | +test -n "${GH_EVENT_REF}" |
| 19 | + |
| 20 | +# Set up private ssh key for git |
| 21 | +mkdir ~/.ssh |
| 22 | +chmod 700 ~/.ssh |
| 23 | +echo "${GIT_SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa |
| 24 | +chmod 600 ~/.ssh/id_rsa |
| 25 | + |
| 26 | +git config --global core.sshCommand "ssh -i ~/.ssh/id_rsa -o IdentitiesOnly=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" |
| 27 | + |
| 28 | +echo "GITHUB_EVENT_NAME: $GITHUB_EVENT_NAME" |
| 29 | +echo "GITHUB_REF: $GITHUB_REF" |
| 30 | +echo "GH_EVENT_REF: $GH_EVENT_REF" |
| 31 | +echo "GH_EVENT_REF_TYPE: ${GH_EVENT_REF_TYPE}" |
| 32 | + |
| 33 | +# Prepare variables. |
| 34 | +FOLDER_GITHUB=$(pwd) |
| 35 | +PROJECT_NAME="$(basename `pwd`)" |
| 36 | +FOLDER_HOSTING="$FOLDER_GITHUB/../hosting" |
| 37 | +mkdir -p $FOLDER_HOSTING |
| 38 | +echo "# Github folder: $FOLDER_GITHUB" |
| 39 | +echo "# Hosting folder: $FOLDER_HOSTING" |
| 40 | + |
| 41 | +# Figure out who is building. |
| 42 | +shalite=$(git rev-parse --short HEAD) |
| 43 | +gitname=$(git show -s --format='%an' HEAD) |
| 44 | +gitemail=$(git show -s --format='%ae' HEAD) |
| 45 | +commitmessage=$(git log -1 --pretty=%B) |
| 46 | +if [ "$gitname" == "" ]; then |
| 47 | + gitname="Morpht Automation (GitHub Actions)" |
| 48 | + gitemail="deployer@morpht.com" |
| 49 | +fi |
| 50 | +echo "# Name: $gitname" |
| 51 | +echo "# Mail: $gitemail" |
| 52 | +echo "# SHA: $shalite" |
| 53 | +git config --global core.excludesfile false |
| 54 | +git config --global core.fileMode true |
| 55 | +git config --global user.name "${gitname}" |
| 56 | +git config --global user.email "${gitemail}" |
| 57 | + |
| 58 | +# Handling DELETE operation |
| 59 | +# This handles deletions of both branches and tags |
| 60 | +if [ "$GITHUB_EVENT_NAME" == "delete" ]; then |
| 61 | + set -x |
| 62 | + cd "$FOLDER_HOSTING" |
| 63 | + git clone "$GIT_REMOTE" . |
| 64 | + git push --delete origin "${GH_EVENT_REF}" |
| 65 | + { [ "${DEBUG}" ] || set +x; } 2>/dev/null |
| 66 | + exit 0 |
| 67 | +fi |
| 68 | + |
| 69 | +# Assert. We expect the "push" even here, no other events. |
| 70 | +if [ "$GITHUB_EVENT_NAME" != "push" ]; then |
| 71 | + echo "Unexpected GITHUB_EVENT_NAME: $GITHUB_EVENT_NAME" |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +# The Build |
| 76 | + |
| 77 | +# Install dependencies. |
| 78 | +set -x |
| 79 | +composer install --prefer-dist --no-progress --no-suggest |
| 80 | +{ [ "${DEBUG}" ] || set +x; } 2>/dev/null |
| 81 | + |
| 82 | +# Get current branch name. |
| 83 | +BRANCHNAME="$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)" |
| 84 | +if [ "$BRANCHNAME" == "HEAD" ]; then |
| 85 | + # We are not on a branch, get short commit hash instead. |
| 86 | + BRANCHNAME="$(git rev-parse --short HEAD)" |
| 87 | +fi |
| 88 | +echo "# Resolved branch for a build: $BRANCHNAME." |
| 89 | + |
| 90 | +# Hosting branch name. |
| 91 | + |
| 92 | +# Pantheon requires valid domain name and max 11 characters. |
| 93 | +## Replace underscores. |
| 94 | +#BRANCHNAME_HOSTING="${BRANCHNAME//_/-}" |
| 95 | +## Remove project name from the start |
| 96 | +#prefix="${PROJECT_NAME}-" |
| 97 | +#BRANCHNAME_HOSTING="${BRANCHNAME//$prefix/}" |
| 98 | +## Strip all invalid characters. |
| 99 | +#BRANCHNAME_HOSTING="${BRANCHNAME_HOSTING//[^a-zA-Z0-9\-]/}" |
| 100 | +## Get first 11 characters. |
| 101 | +#BRANCHNAME_HOSTING="$(echo ${BRANCHNAME_HOSTING:0:11})" |
| 102 | + |
| 103 | +# Keep names 1:1 for now to make sure branch delete above works. |
| 104 | +# @ToDo: Make this conditional based on external variable |
| 105 | +# to allow multiple hosting targets. |
| 106 | +BRANCHNAME_HOSTING="$BRANCHNAME-build" |
| 107 | +echo "# Hosting branch for a build: $BRANCHNAME_HOSTING." |
| 108 | + |
| 109 | +# Get artefact repo. |
| 110 | +echo "FOLDER_HOSTING: $FOLDER_HOSTING" |
| 111 | +echo "GIT_REMOTE: $GIT_REMOTE" |
| 112 | +echo "BRANCHNAME_HOSTING: $BRANCHNAME_HOSTING" |
| 113 | +cd "$FOLDER_HOSTING" |
| 114 | +git clone "$GIT_REMOTE" . |
| 115 | +git checkout "${BRANCHNAME_HOSTING}" || git checkout -b "${BRANCHNAME_HOSTING}" |
| 116 | + |
| 117 | +# Remove .git and ignores to make sure everything gets committed. |
| 118 | +cd $FOLDER_GITHUB |
| 119 | +(find . -type d -name ".git" && find . -name ".gitignore" && find . -name ".gitmodules") | xargs rm -rfv |
| 120 | + |
| 121 | +# Swap out .git folders |
| 122 | +cp -r "${FOLDER_HOSTING}/.git" . |
| 123 | + |
| 124 | +# Commit new artefact. |
| 125 | +git add -A . |
| 126 | +git commit -m "${commitmessage}" -m "GitHub build of ${BRANCHNAME} (${BRANCHNAME_HOSTING}) @ ${shalite}." --no-gpg-sign |
| 127 | + |
| 128 | +# Show what changed and push |
| 129 | +git show --stat |
| 130 | + |
| 131 | +# Handling PUSH operation |
| 132 | + |
| 133 | +# Handling a tag |
| 134 | +if [[ "$GH_EVENT_REF" =~ ^refs\/tags ]]; then |
| 135 | + tag_name=${GH_EVENT_REF:10} # cut off the first 10 chars to get tag name |
| 136 | + set -x |
| 137 | + git push --force origin $tag_name |
| 138 | + { [ "${DEBUG}" ] || set +x; } 2>/dev/null |
| 139 | + exit 0 |
| 140 | +fi |
| 141 | + |
| 142 | +# HEAD will push the current branch |
| 143 | +set -x |
| 144 | +git push origin "${BRANCHNAME_HOSTING}" --force |
| 145 | +{ [ "${DEBUG}" ] || set +x; } 2>/dev/null |
| 146 | +exit 0 |
0 commit comments