-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.sh
executable file
Β·219 lines (190 loc) Β· 8.88 KB
/
main.sh
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
set -euo pipefail
TEST_BOT="${1}"
DEBUG="${2}"
TOKEN="${3}"
if [[ "${DEBUG}" == 'true' ]]; then
set -x
fi
MAX_GIT_RETRIES=5
function git_retry {
local try=0
until git "$@"; do
exit_code="$?"
try=$(($try + 1))
if [ $try -lt $MAX_GIT_RETRIES ]; then
sleep $((2 ** $try))
else
return $exit_code
fi
done
return 0
}
# Check brew's existence
if ! which brew &>/dev/null; then
PATH="/home/linuxbrew/.linuxbrew/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"
if ! which brew &>/dev/null; then
echo "Could not find 'brew' command in PATH or standard locations."
exit 1
fi
fi
# Set basic variables
HOMEBREW_PREFIX="$(brew --prefix)"
HOMEBREW_REPOSITORY="$(brew --repo)"
HOMEBREW_CORE_REPOSITORY="$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core"
HOMEBREW_CASK_REPOSITORY="$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-cask"
HOMEBREW_OTHER_CASK_REPOSITORIES=(
"${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask-drivers"
"${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask-fonts"
"${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-cask-versions"
)
if [[ "$GITHUB_REPOSITORY" =~ ^.+/homebrew-.+$ ]]; then
HOMEBREW_TAP_REPOSITORY="$(brew --repo "$GITHUB_REPOSITORY")"
fi
# Do in container or on the runner
if [[ -f "/.dockerenv" ]] || ([[ -f /proc/1/cgroup ]] && grep -qE "actions_job|docker" /proc/1/cgroup); then
# Fix permissions to give container user write access.
# Directories mounted by the runner are owned by the host user, and can't be written by the container user.
if [[ -n "$(command -v setfacl)" ]]; then
setfacl_dirs=()
user=$(whoami)
for dir in "$HOME" "$RUNNER_WORKSPACE" "$RUNNER_TEMP" # These are mounted by the runner
do
if [[ ! -w "$dir" ]]; then
# Give the container user RW permissions, plus execute for directories.
sudo setfacl -Rm "d:u:$user:rwX,u:$user:rwX" "$dir"
setfacl_dirs+=("$dir")
fi
done
# Store what we've changed so we can revert what we did later.
echo "SETFACL_DIRECTORIES=$(IFS=:; echo "${setfacl_dirs[*]}")" >> $GITHUB_STATE
echo "Set up ACL."
elif [[ ! -w "$GITHUB_OUTPUT" ]]; then
# setfacl isn't installed on Ubuntu by default.
echo '::warning::Missing write permissions to GitHub directories. Install `acl` (`setfacl`).'
fi
# Add safe directories. This is necessary to allow containers to run as root.
# See similar code in `actions/checkout`.
# Could do this for non-containers too, but we want to take care to not write into a shared $HOME.
# For self-hosted without containers, consider pre-setting this instead.
for repo in "$HOMEBREW_REPOSITORY" "$HOMEBREW_CORE_REPOSITORY" \
"$HOMEBREW_CASK_REPOSITORY" "$HOMEBREW_OTHER_CASK_REPOSITORIES[@]" \
"${HOMEBREW_TAP_REPOSITORY-}"
do
if [[ -n "$repo" ]]; then
git config --global --add safe.directory "$repo"
fi
done
HOMEBREW_IN_CONTAINER=1
else
# Add brew to PATH
echo "$HOMEBREW_PREFIX/sbin" >> $GITHUB_PATH
echo "$HOMEBREW_PREFIX/bin" >> $GITHUB_PATH
fi
# Use an access token to checkout (private repositories)
if [[ -n "${TOKEN}" ]]; then
git config --global "http.${GITHUB_SERVER_URL}/.extraheader" "Authorization: basic $(echo ${TOKEN} | base64)"
echo "TOKEN_SET=1" >> $GITHUB_STATE
fi
# Setup Homebrew/brew
if [[ "$GITHUB_REPOSITORY" =~ ^.+/brew$ ]]; then
cd "$HOMEBREW_REPOSITORY"
git remote set-url origin "https://github.com/$GITHUB_REPOSITORY"
git_retry fetch --tags origin "$GITHUB_SHA" '+refs/heads/*:refs/remotes/origin/*'
git remote set-head origin --auto
git checkout --force -B master FETCH_HEAD
cd -
echo "repository-path=$HOMEBREW_REPOSITORY" >> $GITHUB_OUTPUT
else
git_retry -C "$HOMEBREW_REPOSITORY" fetch --force origin
git -C "$HOMEBREW_REPOSITORY" checkout --force -B master origin/HEAD
if [[ -n "${HOMEBREW_TAP_REPOSITORY-}" ]]; then
echo "repository-path=$HOMEBREW_TAP_REPOSITORY" >> $GITHUB_OUTPUT
fi
fi
# Setup Homebrew Bundler RubyGems cache
GEMS_PATH="$HOMEBREW_REPOSITORY/Library/Homebrew/vendor/bundle/ruby/"
GEMS_HASH="$(shasum -a 256 "$HOMEBREW_REPOSITORY/Library/Homebrew/Gemfile.lock" | cut -f1 -d' ')"
echo "gems-path=$GEMS_PATH" >> $GITHUB_OUTPUT
echo "gems-hash=$GEMS_HASH" >> $GITHUB_OUTPUT
# Setup Homebrew/(home|linux)brew-core tap
if [[ "$GITHUB_REPOSITORY" =~ ^.+/(home|linux)brew-core$ ]]; then
cd "$HOMEBREW_CORE_REPOSITORY"
git remote set-url origin "https://github.com/$GITHUB_REPOSITORY"
git_retry fetch origin "$GITHUB_SHA" '+refs/heads/*:refs/remotes/origin/*'
git remote set-head origin --auto
git checkout --force -B master FETCH_HEAD
cd -
# Setup all other taps
else
if [[ -n "${HOMEBREW_TAP_REPOSITORY-}" ]]; then
if [[ "$GITHUB_REPOSITORY" =~ ^.+/homebrew-cask(-.+)*$ ]]; then
# Tap or update homebrew/cask for other cask repos.
if [[ "${HOMEBREW_TAP_REPOSITORY}" != "${HOMEBREW_CASK_REPOSITORY}" ]] && [[ -d "${HOMEBREW_CASK_REPOSITORY}" ]]; then
git_retry -C "$HOMEBREW_CASK_REPOSITORY" fetch --force origin
git -C "$HOMEBREW_CASK_REPOSITORY" remote set-head origin --auto
git -C "$HOMEBREW_CASK_REPOSITORY" checkout --force -B master origin/HEAD
elif ! [[ -d "${HOMEBREW_CASK_REPOSITORY}" ]]; then
git_retry clone https://github.com/Homebrew/homebrew-cask "${HOMEBREW_CASK_REPOSITORY}"
fi
for cask_repo in "${HOMEBREW_OTHER_CASK_REPOSITORIES[@]}"
do
if [[ "${HOMEBREW_TAP_REPOSITORY}" != "${cask_repo}" ]] && [[ -d "${cask_repo}" ]]; then
git_retry -C "${cask_repo}" fetch --force origin
git -C "${cask_repo}" remote set-head origin --auto
git -C "${cask_repo}" checkout --force -B master origin/HEAD
fi
done
fi
if [[ -d "$HOMEBREW_TAP_REPOSITORY" ]]; then
cd "$HOMEBREW_TAP_REPOSITORY"
git remote set-url origin "https://github.com/$GITHUB_REPOSITORY"
else
mkdir -vp "$HOMEBREW_TAP_REPOSITORY"
cd "$HOMEBREW_TAP_REPOSITORY"
git init
git remote add origin "https://github.com/$GITHUB_REPOSITORY"
fi
# Make repo available under `GITHUB_WORKSPACE` (default working directory), which some third-party taps may need.
# The symlink needs to be in this direction or `actions/cache` etc. will break as they rely on `GITHUB_WORKSPACE` being `PWD`.
if [[ -z "${HOMEBREW_IN_CONTAINER-}" ]] && [[ -z "${GITHUB_ACTIONS_HOMEBREW_SELF_HOSTED-}" ]]; then
(shopt -s dotglob; rm -rf "$GITHUB_WORKSPACE"/*; mv "$HOMEBREW_TAP_REPOSITORY"/* "$GITHUB_WORKSPACE")
rmdir "$HOMEBREW_TAP_REPOSITORY"
ln -vs "$GITHUB_WORKSPACE" "$HOMEBREW_TAP_REPOSITORY"
cd - && cd "$GITHUB_WORKSPACE"
echo "TAP_SYMLINK=$HOMEBREW_TAP_REPOSITORY" >> $GITHUB_STATE
fi
git_retry fetch origin "$GITHUB_SHA" '+refs/heads/*:refs/remotes/origin/*'
git remote set-head origin --auto
head="$(git symbolic-ref refs/remotes/origin/HEAD)"
head="${head#refs/remotes/origin/}"
git checkout --force -B "$head" FETCH_HEAD
cd -
fi
# Migrate linuxbrew-core to homebrew-core
HOMEBREW_CORE_REPOSITORY_ORIGIN="$(git -C "$HOMEBREW_CORE_REPOSITORY" remote get-url origin 2>/dev/null)"
if [[ "$HOMEBREW_CORE_REPOSITORY_ORIGIN" == "https://github.com/Homebrew/linuxbrew-core" ]]
then
git -C "$HOMEBREW_CORE_REPOSITORY" remote set-url origin "https://github.com/Homebrew/homebrew-core"
fi
git_retry -C "$HOMEBREW_CORE_REPOSITORY" fetch --force origin
git -C "$HOMEBREW_CORE_REPOSITORY" remote set-head origin --auto
git -C "$HOMEBREW_CORE_REPOSITORY" checkout --force -B master origin/HEAD
fi
if [[ "${TEST_BOT}" == 'true' ]]; then
# Setup Homebrew/homebrew-test-bot
HOMEBREW_TEST_BOT_REPOSITORY="$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-test-bot"
if ! [[ -d "$HOMEBREW_TEST_BOT_REPOSITORY" ]]; then
git_retry clone https://github.com/Homebrew/homebrew-test-bot "$HOMEBREW_TEST_BOT_REPOSITORY"
elif [[ "$GITHUB_REPOSITORY" != "Homebrew/homebrew-test-bot" ]]; then
git_retry -C "$HOMEBREW_TEST_BOT_REPOSITORY" fetch --force origin
git -C "$HOMEBREW_TEST_BOT_REPOSITORY" remote set-head origin --auto
git -C "$HOMEBREW_TEST_BOT_REPOSITORY" checkout --force -B master origin/HEAD
fi
fi
# Setup Linux permissions
if [[ "$RUNNER_OS" = "Linux" ]] && [[ -z "${HOMEBREW_IN_CONTAINER-}" ]] && [[ -z "${GITHUB_ACTIONS_HOMEBREW_SELF_HOSTED-}" ]]; then
# Workaround: Remove fontconfig incompatible fonts provided by the poppler
# installation in GitHub Actions image
sudo rm -rf /usr/share/fonts/cmap
fi