-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathaction.sh
More file actions
executable file
·135 lines (108 loc) · 3.35 KB
/
action.sh
File metadata and controls
executable file
·135 lines (108 loc) · 3.35 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
133
134
135
#!/bin/bash
set -e
set -o pipefail
###
# Environment variable definitions.
##
if [[ -n "${TOKEN}" ]]; then
GITHUB_TOKEN=${TOKEN}
fi
if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
if [[ -z "${TARGET_REPO}" ]]; then
echo "Set the TARGET_REPO env variable."
exit 1
fi
if [[ -z "${TARGET_BRANCH}" ]]; then
TARGET_BRANCH=main
echo "No TARGET_BRANCH was set, so defaulting to ${TARGET_BRANCH}"
fi
if [[ -z "${HUGO_PUBLISH_DIR}" ]]; then
HUGO_PUBLISH_DIR=public
echo "No HUGO_PUBLISH_DIR was set, so defaulting to ${HUGO_PUBLISH_DIR}"
fi
if [[ -z "${HUGO_VERSION}" ]]; then
HUGO_VERSION=$(curl -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/gohugoio/hugo/releases?page=1&per_page=1" | jq -r ".[].tag_name" | sed 's/v//g')
echo "No HUGO_VERSION was set, so defaulting to ${HUGO_VERSION}"
fi
if [[ "${HUGO_EXTENDED}" = "true" ]]; then
EXTENDED_INFO=" (extended)"
EXTENDED_URL="extended_"
else
EXTENDED_INFO=""
EXTENDED_URL=""
fi
###
# Downloading of Hugo.
###
echo "Downloading Hugo: ${HUGO_VERSION}${EXTENDED_INFO}"
URL=https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${EXTENDED_URL}${HUGO_VERSION}_Linux-64bit.tar.gz
echo "Using '${URL}' to download Hugo"
curl -sSL "${URL}" > /tmp/hugo.tar.gz
tar -C /tmp -xf /tmp/hugo.tar.gz
mv /tmp/hugo /usr/bin/hugo
###
# Optionally install Go.
###
# shellcheck disable=SC2153
if [[ -n "${GO_VERSION}" ]]; then
echo "Installing Go: ${GO_VERSION}"
curl -sL "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" > /tmp/go.tar.gz
tar -C /tmp -xf /tmp/go.tar.gz
mv /tmp/go /go
rm -rf \
/usr/local/go/pkg/*/cmd \
/usr/local/go/pkg/bootstrap \
/usr/local/go/pkg/obj \
/usr/local/go/pkg/tool/*/api \
/usr/local/go/pkg/tool/*/go_bootstrap \
/usr/local/go/src/cmd/dist/dist \
/tmp/go.tar.gz
# Provide version details and sanity check installation
echo "Installed Go: ${GO_VERSION}"
go version
fi
###
# Git config for private repositories
# This is needed if you're using hugo mod themes with private repositories
###
git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf 'https://github.com/'
# https://github.com/actions/checkout/issues/766
git config --global --add safe.directory "${PWD}"
###
# Build the site.
###
echo "Building the Hugo site with: 'hugo ${HUGO_ARGS}'"
hugo "${HUGO_ARGS}"
TARGET_REPO_URL="https://${GITHUB_TOKEN}@github.com/${TARGET_REPO}.git"
rm -rf .git
cd "${HUGO_PUBLISH_DIR}"
if [[ -n "${CNAME}" ]]; then
echo "CNAME set to ${CNAME}, creating file CNAME"
echo "${CNAME}" > CNAME
fi
echo "Committing the site to git and pushing"
git init
###
# Setup the git configuration for the newly init'd repo
###
if ! git config --get user.name; then
git config --global user.name "${GITHUB_ACTOR}"
fi
if ! git config --get user.email; then
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
fi
# https://github.com/actions/checkout/issues/766
git config --global --add safe.directory "${PWD}"
echo "Getting hash for base repository commit"
HASH=$(echo "${GITHUB_SHA}" | cut -c1-7)
###
# Now add all the changes and commit and push
###
git checkout -b "${TARGET_BRANCH}"
git add . && \
git commit -m "Auto publishing site from ${GITHUB_REPOSITORY}@${HASH}" && \
git push --force "${TARGET_REPO_URL}" "${TARGET_BRANCH}"
echo "Complete"