forked from martinthomson/i-d-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghpages.mk
153 lines (138 loc) · 6 KB
/
ghpages.mk
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
## Update the gh-pages branch with useful files
ifeq (true,$(TRAVIS))
# Travis is a nightmare. It doesn't actually include the branch name in the
# repo at all. Also, it doesn't consistently set the current branch name.
ifneq (,$(TRAVIS_PULL_REQUEST_BRANCH))
SOURCE_BRANCH := $(TRAVIS_PULL_REQUEST_BRANCH)
else
SOURCE_BRANCH := $(TRAVIS_BRANCH)
endif
else
ifdef GITHUB_REF
ifneq (,$(filter refs/heads/%,$(GITHUB_REF)))
SOURCE_BRANCH := $(patsubst refs/heads/%,%,$(GITHUB_REF))
else
ifneq (,$(filter refs/tags/%,$(GITHUB_REF)))
SOURCE_BRANCH := $(patsubst refs/tags/%,%,$(GITHUB_REF))
else
SOURCE_BRANCH := $(notdir $(GITHUB_REF))
endif
endif
else
SOURCE_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
ifeq (HEAD,$(SOURCE_BRANCH))
SOURCE_BRANCH := $(shell git rev-parse --short HEAD)
endif
endif
endif
# Default to pushing if a key or token is available.
ifeq (pull_request,$(GITHUB_EVENT_NAME))
PUSH_GHPAGES ?= false
endif
ifneq (,$(GITHUB_PUSH_TOKEN)$(CI_HAS_WRITE_KEY))
PUSH_GHPAGES ?= true
endif
PUSH_GHPAGES ?= false
.IGNORE: fetch-ghpages
.PHONY: fetch-ghpages
fetch-ghpages:
git fetch -qf origin gh-pages:gh-pages
GHPAGES_ROOT := /tmp/ghpages$(PID)
ghpages: $(GHPAGES_ROOT)
$(GHPAGES_ROOT): fetch-ghpages
@git show-ref refs/heads/gh-pages >/dev/null 2>&1 || \
(git show-ref refs/remotes/origin/gh-pages >/dev/null 2>&1 && \
git branch -t gh-pages origin/gh-pages) || \
! echo 'Error: No gh-pages branch, run `make -f $(LIBDIR)/setup.mk setup-ghpages` to initialize it.'
git clone -q -b gh-pages . $@
GHPAGES_TARGET := $(GHPAGES_ROOT)$(filter-out /$(DEFAULT_BRANCH),/$(SOURCE_BRANCH))
ifneq ($(GHPAGES_TARGET),$(GHPAGES_ROOT))
$(GHPAGES_TARGET): $(GHPAGES_ROOT)
mkdir -p $@
endif
GHPAGES_PUBLISHED := $(drafts_html) $(drafts_txt) $(GHPAGES_EXTRA)
GHPAGES_INSTALLED := $(addprefix $(GHPAGES_TARGET)/,$(GHPAGES_PUBLISHED))
$(GHPAGES_INSTALLED): $(GHPAGES_PUBLISHED) $(GHPAGES_TARGET)
cp -f $(notdir $@) $@
GHPAGES_ALL := $(GHPAGES_INSTALLED) $(GHPAGES_TARGET)/index.$(INDEX_FORMAT)
$(GHPAGES_TARGET)/index.$(INDEX_FORMAT): $(GHPAGES_INSTALLED) $(DEPS_FILES)
$(LIBDIR)/build-index.sh $(INDEX_FORMAT) "$(dir $@)" "$(SOURCE_BRANCH)" "$(GITHUB_USER)" "$(GITHUB_REPO)" $(drafts_source) >$@
ifneq ($(GHPAGES_TARGET),$(GHPAGES_ROOT))
GHPAGES_ALL += $(GHPAGES_ROOT)/index.$(INDEX_FORMAT)
$(GHPAGES_ROOT)/index.$(INDEX_FORMAT): $(GHPAGES_INSTALLED) $(DEPS_FILES)
$(LIBDIR)/build-index.sh $(INDEX_FORMAT) "$(dir $@)" "$(DEFAULT_BRANCH)" "$(GITHUB_USER)" "$(GITHUB_REPO)" $(drafts_source) >$@
endif
# GHPAGES_COMMIT_TTL is the number of days worth of commits to keep on the gh-pages branch.
GHPAGES_COMMIT_TTL ?= 30
# GHPAGES_BRANCH_TTL is the number of days to retain a directory on gh-pages
# after the corresponding branch has been deleted. This is measured from the last change.
GHPAGES_BRANCH_TTL ?= 30
.PHONY: cleanup-ghpages
cleanup-ghpages: $(GHPAGES_ROOT)
-@for remote in `git remote`; do \
git remote prune $$remote; \
done;
# Drop old gh-pages commits
# Retain $(GHPAGES_COMMIT_TTL) days of history.
# Only run this if more than $(GHPAGES_COMMIT_TTL)*2 days of history exists.
@KEEP=$$((`date '+%s'`-($(GHPAGES_COMMIT_TTL)*86400))); \
CUTOFF=$$((`date '+%s'`-($(GHPAGES_COMMIT_TTL)*172800))); \
ROOT=`git -C $(GHPAGES_ROOT) rev-list --max-parents=0 gh-pages`; \
if [ `git -C $(GHPAGES_ROOT) show -s --format=%ct $$ROOT` -lt $$CUTOFF ]; then \
NEW_ROOT=`git -C $(GHPAGES_ROOT) rev-list --min-age=$$KEEP --max-count=1 gh-pages`; \
if [ $$NEW_ROOT != $$ROOT ]; then \
git -C $(GHPAGES_ROOT) replace --graft $$NEW_ROOT && \
FILTER_BRANCH_SQUELCH_WARNING=1 git -C $(GHPAGES_ROOT) filter-branch gh-pages; \
fi \
fi
# Clean up obsolete directories
# Keep old branches for $(GHPAGES_BRANCH_TTL) days after the last changes (on the gh-pages branch).
@CUTOFF=$$(($$(date '+%s')-($(GHPAGES_BRANCH_TTL)*86400))); \
MAYBE_OBSOLETE=`comm -13 <(git branch -a | sed -e 's,.*[ /],,' | sort | uniq) <(ls $(GHPAGES_ROOT) | sed -e 's,.*/,,')`; \
for item in $$MAYBE_OBSOLETE; do \
if [ -d "$(GHPAGES_ROOT)/$$item" ] && \
[ `git -C $(GHPAGES_ROOT) log -n 1 --format=%ct -- $$item` -lt $$CUTOFF ]; then \
echo "Remove obsolete '$$item'"; \
git -C $(GHPAGES_ROOT) rm -rfq -- $$item; \
fi \
done
# Clean up contents of target directory
@if [ -d $(GHPAGES_TARGET) ]; then \
echo git -C $(GHPAGES_ROOT) rm -fq --ignore-unmatch -- $(GHPAGES_TARGET)/draft-*.html $(GHPAGES_TARGET)/draft-*.txt $(addprefix $(GHPAGES_TARGET)/,$(GHPAGES_EXTRA)); \
git -C $(GHPAGES_ROOT) rm -fq --ignore-unmatch -- $(GHPAGES_TARGET)/draft-*.html $(GHPAGES_TARGET)/draft-*.txt $(addprefix $(GHPAGES_TARGET)/,$(GHPAGES_EXTRA)); \
fi
.PHONY: ghpages gh-pages
gh-pages: ghpages
ifneq (,$(MAKE_TRACE))
ghpages:
@$(call MAKE_TRACE,ghpages)
else
ghpages: cleanup-ghpages $(GHPAGES_ALL)
git -C $(GHPAGES_ROOT) add -f $(GHPAGES_ALL)
if test `git -C $(GHPAGES_ROOT) status --porcelain | grep '^[A-Z]' | wc -l` -gt 0; then \
git -C $(GHPAGES_ROOT) $(CI_AUTHOR) commit -m "Script updating gh-pages from $(shell git rev-parse --short HEAD). [ci skip]"; fi
ifeq (true,$(PUSH_GHPAGES))
ifneq (,$(if $(CI_HAS_WRITE_KEY),1,$(if $(GITHUB_PUSH_TOKEN),,1)))
$(trace) all -s ghpages-push git -C $(GHPAGES_ROOT) push -f https://github.com/$(GITHUB_REPO_FULL) gh-pages
else
@echo git -C $(GHPAGES_ROOT) push -qf https://****@github.com/$(GITHUB_REPO_FULL) gh-pages
@git -C $(GHPAGES_ROOT) push -qf https://$(GITHUB_PUSH_TOKEN)@github.com/$(GITHUB_REPO_FULL) gh-pages >/dev/null 2>&1 \
|| $(trace) all -s ghpages-push ! echo "git -C $(GHPAGES_ROOT) push -qf https://****@github.com/$(GITHUB_REPO_FULL) gh-pages"
endif
else
ifeq (true,$(CI))
@echo "*** Warning: pushing to the gh-pages branch is disabled."
else
$(trace) all -s ghpages-push git -C $(GHPAGES_ROOT) push -f origin gh-pages
endif
endif # PUSH_GHPAGES
-rm -rf $(GHPAGES_ROOT)
endif # MAKE_TRACE
## Save published documents to the CI_ARTIFACTS directory
ifneq (,$(CI_ARTIFACTS))
$(CI_ARTIFACTS):
mkdir -p $@
.PHONY: artifacts
artifacts: $(GHPAGES_PUBLISHED) $(CI_ARTIFACTS)
cp -f $(filter-out $(CI_ARTIFACTS),$^) $(CI_ARTIFACTS)
endif