forked from angular/dev-infra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish_to_github.sh
executable file
·85 lines (64 loc) · 2.84 KB
/
publish_to_github.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
#!/bin/bash
# Script to publish the build artifacts to a GitHub repository.
# Builds will be automatically published once new changes are made to the repository.
# Script arguments:
# - $1: Package name
# - $2: Package builds repository
# - $3: Package dist directory (e.g. in bazel-out)
# The script should immediately exit if any command in the script fails.
set -e
# Go to the project root directory
cd $(dirname ${0})/..
if [ -z ${SNAPSHOT_BUILDS_GITHUB_TOKEN} ]; then
echo "Error: No access token for GitHub could be found." \
"Please set the environment variable 'SNAPSHOT_BUILDS_GITHUB_TOKEN'."
exit 1
fi
packageName="${1}"
packageRepo="${2}"
packageDistDir="$(realpath ${3})"
buildVersion=$(cd ${packageDistDir} && node -pe "require('./package.json').version")
branchName=${CIRCLE_BRANCH:-'main'}
commitSha=$(git rev-parse --short HEAD)
commitAuthorName=$(git --no-pager show -s --format='%an' HEAD)
commitAuthorEmail=$(git --no-pager show -s --format='%ae' HEAD)
commitMessage=$(git log --oneline -n 1)
buildVersionName="${buildVersion}-sha-${commitSha}"
buildTagName="${branchName}-${commitSha}"
buildCommitMessage="${branchName} - ${commitMessage}"
repoUrl="https://github.com/angular/${packageRepo}.git"
authenticatedRepoUrl="https://${SNAPSHOT_BUILDS_GITHUB_TOKEN}:@github.com/angular/${packageRepo}.git"
repoDir="/tmp/${packageRepo}"
echo "Starting publish process of ${packageName} for ${buildVersionName} into ${branchName}.."
# Prepare cloning the builds repository
rm -rf ${repoDir}
mkdir -p ${repoDir}
echo "Starting cloning process of ${repoUrl} into ${repoDir}.."
if [[ $(git ls-remote --heads ${repoUrl} ${branchName}) ]]; then
echo "Branch ${branchName} already exists. Cloning that branch."
git clone ${repoUrl} ${repoDir} --depth 1 --branch ${branchName}
cd ${repoDir}
echo "Cloned repository and switched into the repository directory (${repoDir})."
else
echo "Branch ${branchName} does not exist on ${packageRepo} yet."
echo "Cloning default branch and creating branch '${branchName}' on top of it."
git clone ${repoUrl} ${repoDir} --depth 1
cd ${repoDir}
echo "Cloned repository and switched into directory. Creating new branch now.."
git checkout -b ${branchName}
fi
# Copy the build files to the repository
rm -rf ./*
cp -r ${packageDistDir}/* ./
# Prepare Git for pushing the artifacts to the repository.
git config user.name "${commitAuthorName}"
git config user.email "${commitAuthorEmail}"
echo "Git configuration has been updated to match the last commit author. Publishing now.."
git add -A
if git diff-index --quiet HEAD --; then
echo "Skipping push as no changes occured between this push and the previous commit."
else
git commit -m "${buildCommitMessage}"
git push ${authenticatedRepoUrl} ${branchName} --force
echo "Published package artifacts for ${packageName}#${buildVersionName} into ${branchName}"
fi