Skip to content

Commit 5499bfb

Browse files
committed
complete integrating setup into main deploy flow
closes #16
1 parent d370946 commit 5499bfb

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

deploy.sh

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,57 @@ main() {
4747
fi
4848

4949
if git ls-remote --exit-code $repo "refs/heads/$deploy_branch" ; then
50+
# deploy_branch exists in $repo; make sure we have the latest version
51+
5052
disable_expanded_output
5153
git fetch --force $repo $deploy_branch:$deploy_branch
5254
enable_expanded_output
5355
fi
5456

55-
if git show-ref --verify --quiet "refs/heads/$deploy_branch" ; then
56-
#read-tree
57-
58-
#make deploy_branch the current branch
59-
git symbolic-ref HEAD refs/heads/$deploy_branch
60-
61-
#put the previously committed contents of deploy_branch into the index
62-
git --work-tree "$deploy_directory" reset --mixed --quiet
63-
else
64-
# deploy_branch doesn't exist; create it
65-
#read-tree --empty?
66-
#write-tree
67-
#commit-tree
68-
git --work-tree "$deploy_directory" checkout --orphan $deploy_branch
69-
git --work-tree "$deploy_directory" rm -r "*"
57+
# check if deploy_branch exists locally
58+
if git show-ref --verify --quiet "refs/heads/$deploy_branch"
59+
then incremental_deploy
60+
else initial_deploy
7061
fi
7162

63+
restore_head
64+
}
65+
66+
initial_deploy() {
67+
git --work-tree "$deploy_directory" checkout --orphan $deploy_branch
68+
git --work-tree "$deploy_directory" add --all
69+
commit+push
70+
}
71+
72+
incremental_deploy() {
73+
#make deploy_branch the current branch
74+
git symbolic-ref HEAD refs/heads/$deploy_branch
75+
#put the previously committed contents of deploy_branch into the index
76+
git --work-tree "$deploy_directory" reset --mixed --quiet
7277
git --work-tree "$deploy_directory" add --all
7378

7479
set +o errexit
75-
diff=$(git --work-tree "$deploy_directory" diff --exit-code --quiet HEAD)$?
80+
diff=$(git --work-tree "$deploy_directory" diff --exit-code --quiet HEAD --)$?
7681
set -o errexit
7782
case $diff in
7883
0) echo No changes to files in $deploy_directory. Skipping commit.;;
79-
1)
80-
set_user_id
81-
git --work-tree "$deploy_directory" commit -m \
82-
"publish: $commit_title"$'\n\n'"generated from commit $commit_hash"
83-
84-
disable_expanded_output
85-
#--quiet is important here to avoid outputting the repo URL, which may contain a secret token
86-
git push --quiet $repo $deploy_branch
87-
enable_expanded_output
88-
;;
84+
1) commit+push;;
8985
*)
9086
echo git diff exited with code $diff. Aborting. Staying on branch $deploy_branch so you can debug. To switch back to master, use: git symbolic-ref HEAD refs/heads/master && git reset --mixed >&2
9187
return $diff
9288
;;
9389
esac
90+
}
9491

95-
restore_head
92+
commit+push() {
93+
set_user_id
94+
git --work-tree "$deploy_directory" commit -m \
95+
"publish: $commit_title"$'\n\n'"generated from commit $commit_hash"
96+
97+
disable_expanded_output
98+
#--quiet is important here to avoid outputting the repo URL, which may contain a secret token
99+
git push --quiet $repo $deploy_branch
100+
enable_expanded_output
96101
}
97102

98103
#echo expanded commands as they are executed (for debugging)

integration-test.bats

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,41 @@ create_repo() {
2424

2525
source deploy.sh --source-only
2626

27-
@test "script creates branch and deploys file" {
27+
@test "deploy creates branch and deploys file" {
2828

2929
main
3030

3131
git cat-file -e gh-pages:file # `file` exists on gh-pages
3232
}
3333

34-
@test " deploys file to existing branch" {
34+
@test " deploys new file to existing branch" {
3535
git branch gh-pages
3636

3737
main
3838

3939
git cat-file -e gh-pages:file # `file` exists on gh-pages
4040
}
4141

42-
@test " doesn't clobber files in deploy directory" {
42+
@test " updates existing file" {
43+
main
44+
echo updated > dist/file
45+
46+
main
47+
48+
[[ `git cat-file blob gh-pages:file` = updated ]]
49+
}
50+
51+
@test " removes missing file" {
52+
main
53+
rm dist/file
54+
55+
main --allow-empty
56+
57+
! git cat-file -e gh-pages:file # `file` does not exist on gh-pages
58+
}
59+
60+
@test " doesn't clobber file in deploy directory" {
61+
# make sure that a source file doesn't overwrite a deploy file with the same name
4362
echo source > file
4463
echo dist > dist/file
4564
git add file
@@ -48,7 +67,7 @@ source deploy.sh --source-only
4867
main
4968

5069
[[ `cat dist/file` = dist ]]
51-
[[ `git cat-file gh-pages:file` = dist ]]
70+
[[ `git cat-file blob gh-pages:file` = dist ]]
5271
}
5372

5473
@test " doesn't deploy source file" {

readme.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ Download the script (`wget https://github.com/X1011/git-directory-deploy/raw/mas
1212
- **default_username**, **default_email**: identity to use for git commits if none is set already. Useful for CI servers.
1313
- **repo**: repository to deploy to. Must be readable and writable. The default of "origin" will not work on Travis CI, since it uses the read-only git protocol. In that case, it is recommended to store a [GitHub token](https://help.github.com/articles/creating-an-access-token-for-command-line-use) in a [secure environment variable](http://docs.travis-ci.com/user/environment-variables/#Secure-Variables) and use it in an HTTPS URL like this: <code>repo=https://$GITHUB_TOKEN@github\.com/<i>user</i>/<i>repo</i>.git</code>
1414

15-
## setup
16-
Ensure configuration variables are correct in `deploy.sh` and run `./deploy.sh -s`
17-
1815
## run
1916
Do this every time you want to deploy, or have your CI server do it.
2017

@@ -27,6 +24,4 @@ Do this every time you want to deploy, or have your CI server do it.
2724
### options
2825
`-v`, `--verbose`: echo expanded commands as they are executed, using the xtrace option. This can be useful for debugging, as the output will include the values of variables that are being used, such as $commit_title and $deploy_directory. However, the script makes special effort to not output the value of $repo, as it may contain a secret authentication token.
2926

30-
`-s`, `--setup`: perform one-time setup to prepare the repo for deployments. Creates `deploy_branch`, initializes it with the contents of `deploy_directory`, and pushes it to `repo`.
31-
3227
`-e`, `--allow-empty`: allow deployment of an empty directory. By default, the script will abort if `deploy_directory` is empty.

0 commit comments

Comments
 (0)