-
Notifications
You must be signed in to change notification settings - Fork 8
/
make.sh
executable file
·89 lines (73 loc) · 2.21 KB
/
make.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
#!/usr/bin/env bash
set -eo pipefail
main() {
if [ "$GITHUB_EVENT_NAME" == "pull_request" ];then
log "checking ..."
check
elif [ "$GITHUB_EVENT_NAME" == "push" ];then
log "building ..."
build
log "publishing new role packages ..."
deploy
fi
}
build() {
# build changed packages
local changedPackage; for changedPackage in $(findChangedPackageNames); do
log "creating package $changedPackage ..."
tar czf $changedPackage -C ${changedPackage%-*} .
done
}
check() {
log "ensuring only one role is changed in one PR ..."
local changedPackages; changedPackages="$(findChangedPackageNames)"
local changedCount=$(echo $changedPackages | wc -w)
if [ "$changedCount" -gt 1 ]; then
fatal 1 "Only one package is allowed to update at a time, though found $changedCount changed packages: [${changedPackages// /, }]."
fi
}
deploy() {
# switch to gh-pages branch
git fetch
git checkout -t origin/gh-pages
log "preparing git author info ..."
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
log "preparing git commit ..."
echo "roles:" > index.yml
findPackages | sort | sed 's/^/- /g' >> index.yml
git add *.tar.gz index.yml
git diff --staged --quiet || git commit -m "update"
log "pushing changes if any ..."
git push
}
findChangedPackageNames() {
comm -13 <(findPublishedPackageNames | sort) <(findPendingPackageNames | sort) | xargs
}
findPublishedPackageNames() {
log "retrieving published packages ..."
local owner=${GITHUB_REPOSITORY%%/*}
local repo=${GITHUB_REPOSITORY##*/}
local url=https://$owner.github.io/$repo/index.yml
curl -sL $url | awk '$1=="-" {print $2}'
}
findPendingPackageNames() {
local role version; for role in $(findRoles); do
version="$(awk '$1=="role_version:" {print $2}' $role/meta/main.yml | grep ^[0-9])" || fatal 1 "version is required: $role."
echo ${role}-${version}.tar.gz
done
}
findRoles() {
find . -mindepth 1 -maxdepth 1 -type d ! -name ".*" -printf "%f\n"
}
findPackages() {
find . -mindepth 1 -maxdepth 1 -type f -name "*.tar.gz" -printf "%f\n"
}
fatal() {
log ${@:2}
return $1
}
log() {
echo "$@" 1>&2
}
main