From bb033b58cc564ba8bca6c76855b52212a1738d88 Mon Sep 17 00:00:00 2001 From: Lukas Bestle Date: Fri, 11 Sep 2015 23:24:43 +0200 Subject: [PATCH] Make the revision param for deployments optional The deployment script now uses the current revision of the set branch in this case. Does not work together with a custom value of $CONFIG_HASH_LENGTH though. --- README.md | 10 ++++-- bin/project_deploy | 78 +++++++++++++++++++++++++++++++++------------- bin/site_deploy | 7 +++-- 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index f0d25d4..0764b03 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,15 @@ This is a tutorial on how you would implement this setup with this toolset. Using `project_add` is similar, but it uses a custom project path – the headers of the files in `bin/` contain detailed usage information. 3. **Get the current state from your GitHub repository (other Git repositories work the same)** - As "main_site" was setup using a clone URL, you can use `site_deploy ` to get and install the current state of your project: + As "main_site" was setup using a clone URL, you can use `site_deploy []` to fetch and install the current state of your project automatically: + + ```bash + site_deploy main_site + ``` + + If you omit the Git revision, the latest one is used instead. But if you want to deploy a specific revision, that's easy too: ```bash - # You need the full SHA-1 hash of your commit for this to work. - # Automatic deployments using web hooks do this for you automatically. site_deploy main_site 78ca1d2fa93147b0... ``` diff --git a/bin/project_deploy b/bin/project_deploy index 3d5c7ef..fd1365e 100755 --- a/bin/project_deploy +++ b/bin/project_deploy @@ -6,10 +6,11 @@ # .postdeploy.sh in the repository (if existing) and links the version # as current. # -# Usage: project_deploy +# Usage: project_deploy [] # # Path to the project # SHA-1 commit hash of the revision to checkout +# Defaults to the latest commit on the set branch ######################################################################## # Functions @@ -56,11 +57,20 @@ if [[ $CONFIG_PRESERVE_VERSIONS == 1 ]]; then CONFIG_PRESERVE_VERSIONS=2 fi -if [[ -z "$revision" ]]; then +if [[ -z "$project" ]]; then # Print help - echo -e "\033[1mUsage:\033[0m \033[34mproject_deploy\033[0m " + echo -e "\033[1mUsage:\033[0m \033[34mproject_deploy\033[0m []" exit 1 fi +if [[ -z "$revision" ]]; then + revision="latest" + + # Check if the default value for $CONFIG_HASH_LENGTH has been changed + if [[ $CONFIG_HASH_LENGTH != 40 ]]; then + echo -e "\033[31mYou changed the default value of \033[34m\$CONFIG_HASH_LENGTH\033[31m. To make sure the deployed revisions are unique and never duplicated, deployments of the latest revision aren't supported with a custom value of this option. Please give me the exact revision string in your custom length.\033[0m" >&2 + exit 1 + fi +fi # Check if the project exists if [[ ! -f "$project/.project" ]]; then @@ -74,12 +84,6 @@ if [[ ! -f "$project/.origin" ]]; then exit 1 fi -# Check if the commit hash is valid -if [[ ${#revision} != $CONFIG_HASH_LENGTH ]]; then - echo -e "\033[31mThe length of the revision name \033[34m$revision\033[31m is not equal to the configured \033[34m$CONFIG_HASH_LENGTH\033[31m characters.\033[0m" >&2 - exit 1 -fi - # Add header to log log="$project/logs/$revision.log" if [[ -f "$log" ]]; then @@ -89,19 +93,10 @@ fi echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n------\033[0m" >> "$log" # Log everything beginning here -{ - # Check if we already installed this version - if ls "$project/versions"/*-$revision &> /dev/null; then - # Yes, use that - - # Build the pathname of the destination directory - versionName=$(basename $(echo "$project/versions"/*-$revision)) - buildVersionPath - - echo -e "\033[1mThis revision has already been installed, using previous installation \033[34m$versionName\033[0;1m.\033[0m" - else - # No, fetch and install - +{ + # Make sure the repository exists and is updated + # We only need to do this if the revision was not deployed yet + if ! ls "$project/versions"/*-$revision &> /dev/null; then # Check if the repository has already been cloned url=$(cat "$project/.origin") if ! [[ -d "$project/repo" ]]; then @@ -121,6 +116,45 @@ echo -e "\033[1;35mRun on \033[34m$(date)\033[35m by \033[34m$USER\033[35m:\n--- fi echo -e " => \033[32mSuccess.\033[0m" fi + fi + + # Determine the actual revision to use + if [[ "$revision" != "latest" ]]; then + # Check if the given commit hash is valid + if [[ ${#revision} != $CONFIG_HASH_LENGTH ]]; then + echo -e "\033[31mThe length of the revision name \033[34m$revision\033[31m is not equal to the configured \033[34m$CONFIG_HASH_LENGTH\033[31m characters.\033[0m" >&2 + exit 1 + fi + else + # Check if the set branch exists + branch=$(cat "$project/.branch") + echo -e "\033[1mDetermining current revision for branch \033[34m$branch\033[0;1m...\033[0m" + if ! (cd "$project/repo"; git show-branch "$branch" &> /dev/null); then + echo -e "\033[31mThe set branch \033[34m$branch\033[31m does not exist.\033[0m" >&2 + exit 1 + fi + + # Get the commit hash from the set branch + revision="$(cd "$project/repo"; git show --format=format:%H "$branch")" + if [[ $? != 0 ]]; then + echo -e " => \033[31mCould not determine current revision.\033[0m" >&2 + exit 1 + fi + + echo -e " => \033[32mUsing revision \033[34m$revision\033[32m.\033[0m" + fi + + # Check if we already installed this version + if ls "$project/versions"/*-$revision &> /dev/null; then + # Yes, use that + + # Build the pathname of the destination directory + versionName=$(basename $(echo "$project/versions"/*-$revision)) + buildVersionPath + + echo -e "\033[1mThis revision has already been installed, using previous installation \033[34m$versionName\033[0;1m.\033[0m" + else + # No, fetch and install # Build the pathname of the destination directory # Get all version names, get the last one, split it by "-" and get the first part, remove leading zeros diff --git a/bin/site_deploy b/bin/site_deploy index 2c09ac8..ec49836 100755 --- a/bin/site_deploy +++ b/bin/site_deploy @@ -6,18 +6,19 @@ # .postdeploy.sh in the repository (if existing) and links the version # as current. # -# Usage: site_deploy +# Usage: site_deploy [] # # Name of the site (directory in ~/web/sites/) # SHA-1 commit hash of the revision to checkout +# Defaults to the latest commit on the set branch ######################################################################## site="$1" revision="$2" -if [[ -z "$revision" ]]; then +if [[ -z "$site" ]]; then # Print help - echo -e "\033[1mUsage:\033[0m \033[34msite_deploy\033[0m " + echo -e "\033[1mUsage:\033[0m \033[34msite_deploy\033[0m []" exit 1 fi