-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
90 lines (71 loc) · 3.07 KB
/
entrypoint.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
#!/bin/sh
set -e
set -o pipefail
# ENVIRONMENT VARIABLES:
# [OPTIONAL]: TARGET_REPOSITORY, PROJECT_DIR, BUILD_DIR, BUILD_BRANCH, GITHUB_HOSTNAME
# [MANDATORY]: TOKEN
# When does these optional env variables become mandatory?
# TARGET_REPOSITORY: If you wish to deploy the BUILD_BRANCH to a different remote repository
# PROJECT_DIR: If your project directory is not the repository's base directory
# BUILD_DIR: If the static build is generated in a different directory and not in ${PROJECT_DIR}/build directory
# BUILD_BRANCH: If you want to publish the build to different branch and not to 'build' branch
# GITHUB_HOSTNAME: If the hostname of github, is not 'github.com'
# NOTE:
# This action assumes that your project has a build script available in package.json which generates the static build using the necessary package.
# BUILD_DIR is relative to PROJECT_DIR
# You can use this action with other frameworks/libraries by just adding a 'build' script to package.json and passing the static build-directory path through BUILD_DIR env variable.
# SCRIPT:
# Checks if the value of TARGET_REPOSITORY is empty
# Checks for GITHUB_REPOSITORY, if it's empty
# Exits with status code 1, if both of them are empty
if [[ -z "$TARGET_REPOSITORY" ]]; then
if [[ -z "$GITHUB_REPOSITORY" ]]; then
echo "Set the GITHUB_REPOSITORY env variable."
exit 1
fi
TARGET_REPOSITORY=${GITHUB_REPOSITORY}
fi
# TOKEN is the GitHub Personal Access Token to access and push the build code to the target repository's new/existing branch.
# Checks for token and exits with an error code, if it's empty.
if [[ -z "$TOKEN" ]]; then
echo "Set the TOKEN env variable which can access the $TARGET_REPOSITORY"
exit 1
fi
# PROJECT_DIR is the directory is the base dir from where dir references are taken and where the package.json is found.
# Assumes it as . by default
if [[ -z "$PROJECT_DIR" ]]; then
PROJECT_DIR="."
fi
# BUILD_DIR is the directory to where the static build will be output (relative to PROJECT_DIR) on executing the command yarn build.
# Assumes it as ./build by default
if [[ -z "$BUILD_DIR" ]]; then
BUILD_DIR="build"
fi
# BUILD_BRANCH is the branch where the build is deployed.
# Takes it as 'build' by default
if [[ -z "$BUILD_BRANCH" ]]; then
BUILD_BRANCH="build"
fi
# If empty, sets the value of GITHUB_HOSTNAME variable as github.com i.e. default value
if [[ -z "$GITHUB_HOSTNAME" ]]; then
GITHUB_HOSTNAME="github.com"
fi
main() {
echo "Starting build..."
remote_repo="https://${TOKEN}@${GITHUB_HOSTNAME}/${TARGET_REPOSITORY}.git"
# remote_branch=$BUILD_BRANCH
echo "Using yarn $(yarn --version)"
cd $PROJECT_DIR
echo "Install yarn dependencies"
yarn install --frozen-lockfile --silent
echo "Building in $(pwd) directory"
yarn build
cd $BUILD_DIR
git init
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add -f .
git commit -m "Deploy the static build to ${BUILD_BRANCH} branch of ${TARGET_REPOSITORY}"
git push --force ${remote_repo} master:${BUILD_BRANCH}
}
main "$@"