forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-build-artifacts.sh
executable file
·141 lines (115 loc) · 3.72 KB
/
publish-build-artifacts.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
#!/usr/bin/env bash
set -x -u -e -o pipefail
# Setup environment
readonly thisDir=$(cd $(dirname $0); pwd)
# Find the most recent tag that is reachable from the current commit.
# This is shallow clone of the repo, so we might need to fetch more commits to
# find the tag.
function getLatestTag {
local depth=`git log --oneline | wc -l`
local latestTag=`git describe --tags --abbrev=0 || echo NOT_FOUND`
while [ "$latestTag" == "NOT_FOUND" ]; do
# Avoid infinite loop.
if [ "$depth" -gt "1000" ]; then
echo "Error: Unable to find the latest tag." 1>&2
exit 1;
fi
# Increase the clone depth and look for a tag.
depth=$((depth + 50))
git fetch --depth=$depth
latestTag=`git describe --tags --abbrev=0 || echo NOT_FOUND`
done
echo $latestTag;
}
function publishRepo {
COMPONENT=$1
ARTIFACTS_DIR=$2
BUILD_REPO="${COMPONENT}-builds"
REPO_DIR="tmp/${BUILD_REPO}"
if [ -n "${CREATE_REPOS:-}" ]; then
curl -u "$ORG:$TOKEN" https://api.github.com/user/repos \
-d '{"name":"'$BUILD_REPO'", "auto_init": true}'
fi
echo "Pushing build artifacts to ${ORG}/${BUILD_REPO}"
# create local repo folder and clone build repo into it
rm -rf $REPO_DIR
mkdir -p $REPO_DIR
(
cd $REPO_DIR && \
git init && \
git remote add origin $REPO_URL && \
# use the remote branch if it exists
if git ls-remote --exit-code origin ${BRANCH}; then
git fetch origin ${BRANCH} --depth=1 && \
git checkout origin/${BRANCH}
fi
git checkout -b "${BRANCH}"
)
# copy over build artifacts into the repo directory
rm -rf $REPO_DIR/*
cp -R $ARTIFACTS_DIR/* $REPO_DIR/
if [[ ${CI} ]]; then
(
# The file ~/.git_credentials is created in /.circleci/config.yml
cd $REPO_DIR && \
git config credential.helper "store --file=$HOME/.git_credentials"
)
fi
echo `date` > $REPO_DIR/BUILD_INFO
echo $SHA >> $REPO_DIR/BUILD_INFO
(
cd $REPO_DIR && \
git config user.name "${COMMITTER_USER_NAME}" && \
git config user.email "${COMMITTER_USER_EMAIL}" && \
git add --all && \
git commit -m "${COMMIT_MSG}" --quiet && \
git tag "${BUILD_VER}" && \
git push origin "${BRANCH}" --tags --force
)
}
# Publish all individual packages from packages-dist.
function publishPackages {
GIT_SCHEME=$1
PKGS_DIST=$2
BRANCH=$3
BUILD_VER=$4
for dir in $PKGS_DIST/*/
do
COMPONENT="$(basename ${dir})"
# Replace _ with - in component name.
COMPONENT="${COMPONENT//_/-}"
JS_BUILD_ARTIFACTS_DIR="${dir}"
if [[ "$GIT_SCHEME" == "ssh" ]]; then
REPO_URL="git@github.com:${ORG}/${COMPONENT}-builds.git"
elif [[ "$GIT_SCHEME" == "http" ]]; then
REPO_URL="https://github.com/${ORG}/${COMPONENT}-builds.git"
else
die "Don't have a way to publish to scheme $GIT_SCHEME"
fi
publishRepo "${COMPONENT}" "${JS_BUILD_ARTIFACTS_DIR}"
done
echo "Finished publishing build artifacts"
}
function publishAllBuilds() {
GIT_SCHEME="$1"
SHA=`git rev-parse HEAD`
COMMIT_MSG=`git log --oneline -1`
COMMITTER_USER_NAME=`git --no-pager show -s --format='%cN' HEAD`
COMMITTER_USER_EMAIL=`git --no-pager show -s --format='%cE' HEAD`
local shortSha=`git rev-parse --short HEAD`
local latestTag=`getLatestTag`
publishPackages $GIT_SCHEME dist/packages-dist $CUR_BRANCH "${latestTag}+${shortSha}"
# don't publish ivy builds on non-master branch
if [[ "${CI_BRANCH-}" == "master" ]]; then
publishPackages $GIT_SCHEME dist/packages-dist-ivy-aot "${CUR_BRANCH}-ivy-aot" "${latestTag}-ivy-aot+${shortSha}"
fi
}
# See docs/DEVELOPER.md for help
CUR_BRANCH=${CI_BRANCH:-$(git symbolic-ref --short HEAD)}
if [ $# -gt 0 ]; then
ORG=$1
publishAllBuilds "ssh"
else
ORG="angular"
publishAllBuilds "http"
fi