Repository navigation
Expand file tree
/
Copy pathtest.sh
More file actions
132 lines (103 loc) · 4.17 KB
/
Copy pathtest.sh
File metadata and controls
132 lines (103 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# This script will build the site and run tests.
# For now, it only builds for staging and stores the site.
echo "Starting tests..."
# Unshallow to get tags
if [ -f ${HOME}/clone/.git/shallow ];
then
git fetch --unshallow || true
fi
# Get the branch name
branch=$(git symbolic-ref --short -q HEAD)
# If the branch name is not available,
# try for the environment variable for the branch name.
# CI_BRANCH is CodeShip's environment variable.
if [[ $branch = "" ]]
then
branch=$CI_BRANCH
fi
echo "Branch: $branch"
# Get the short hash of the latest commit
commit=$(git log --pretty=format:"%h" -n 1^ 2>&1)
echo "Commit: $commit"
# Get the tag on the last commit
tag=$(git describe --tags --exact-match $commit 2>&1)
echo "Latest tag: $tag"
# Get commit author
committer=$(git show --format="%aN <%aE>" $commit)
echo "Committer: $committer"
# Get the time this script was run
timestamp=$(date +%Y-%m-%d:%H-%M-%S)
echo "Timestamp: $timestamp"
# Set the path and name for the deploy-version file
versionFile="$HOME/cache/_site/deploy-version-$commit.txt"
echo "Version file: $versionFile"
# Set the path and name for the branch-check file
branchCheckFile="$HOME/cache/_site/branch-check.txt"
# If the tag starts with 'release', then run the tests
# on the existing build.
if [[ $tag* =~ ^release.*$ ]]
then
echo "This is tagged for release."
# Double check that the last build was on the `live` branch,
# so that we don't accidentally release a preview branch.
# If the check fails, exit with an error.
# Read the branch-check file, then check it for 'live'.
branchCheck=$(<$branchCheckFile)
echo "Last build was on the $branchCheck branch."
if [[ $branchCheck != 'live' ]]
then
echo "Cannot deploy '$tag': last build was on '$branchCheck'."
echo "We can only release from the 'live' branch."
echo "Please only use 'release' tags on the 'live' branch."
EXIT 1
fi
# No need for tests, we're deploying the already-tested build to live.
# Add the release tag name and new date to the deploy-version file.
echo "Release tag: $tag" >> "$versionFile"
echo "Tested for live: $timestamp" >> "$versionFile"
# Otherwise, build the site.
else
# Build the site
echo "Building the site..."
# Prevent timeouts by sending something to the terminal.
# 300 seconds ten times is 50 minutes.
function prevent_terminal_timeout() { ( for i in {1..10}; do echo "Preventing timeout by echoing every 300 seconds"; sleep 300; done ) & local pid=$!; trap 'kill ${pid}' SIGINT SIGTERM EXIT; }
prevent_terminal_timeout &
# If this has a _config.yml file, assume Jekyll and build,
# otherwise, assume this is a static-file repo (e.g. external media)
# and only copy the relevant files to the _site folder.
if [ -f _config.yml ];
then
# If a preview, build with a preview baseurl,
# named after the 'preview' tag, e.g. 'preview-new-feature'.
if [[ $tag* =~ ^preview.*$ ]]
then
bundle exec jekyll build --baseurl="/$tag"
# Otherwise, build the live site.
else
bundle exec jekyll build --config="_config.yml,_configs/_config.live.yml"
fi
else
mkdir -p _site/book/images/web
rsync -a book/images/web/ _site/book/images/web
fi
echo "Done building."
# -------------------------------------------------
# To do: add any commands that run your tests here.
# -------------------------------------------------
# Write to a deploy-version file, named for the commit
# and containing a timestamp. >> appends new text.
echo "Last commit: $commit" >> "$versionFile"
echo "Last commit by: $committer" >> "$versionFile"
echo "Built for staging: $timestamp" >> "$versionFile"
# Write the name of the current branch to the branch-check file.
# The single > replaces the file contents.
echo "$branch" > "$branchCheckFile"
# Save a copy of the build in the cache. See:
# https://documentation.codeship.com/basic/builds-and-configuration/dependency-cache/#manual-caching
echo "Copying build to cache..."
rsync -a _site "$HOME/cache"
echo "Copied."
fi
echo "Tests complete."