1+ #! /bin/bash
2+
3+ set -e
4+
5+ if [ -z " $GH_TOKEN " ]
6+ then
7+ echo " You must provide the action with a GitHub Personal Access Token secret in order to deploy."
8+ exit 1
9+ fi
10+
11+ if [ -z " $BRANCH " ]
12+ then
13+ echo " You must provide the action with a branch name it should deploy to, for example gh-pages or docs."
14+ exit 1
15+ fi
16+
17+ if [ -z " $DOC_FOLDER " ];
18+ then
19+ DOC_FOLDER=$BRANCH
20+ fi
21+
22+ if [ -z " $FOLDER " ]
23+ then
24+ echo " You must provide the action with the folder name in the repository where your compiled page lives."
25+ exit 1
26+ fi
27+
28+ case " $FOLDER " in /* |./* )
29+ echo " The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly."
30+ exit 1
31+ esac
32+
33+ if [ -z " $COMMIT_EMAIL " ]
34+ then
35+ COMMIT_EMAIL=" ${GITHUB_ACTOR} @users.noreply.github.com"
36+ fi
37+
38+ if [ -z " $COMMIT_NAME " ]
39+ then
40+ COMMIT_NAME=" ${GITHUB_ACTOR} "
41+ fi
42+ if [ -z " $TARGET_REPOSITORY " ]
43+ then
44+ TARGET_REPOSITORY=" ${GITHUB_REPOSITORY} "
45+ fi
46+
47+ # Directs the action to the the Github workspace.
48+ cd $GITHUB_WORKSPACE && \
49+
50+ # Configures Git.
51+ git init && \
52+ git config --global user.email " ${COMMIT_EMAIL} " && \
53+ git config --global user.name " ${COMMIT_NAME} " && \
54+
55+ git config --global http.version HTTP/1.1
56+ git config --global http.postBuffer 157286400
57+
58+ # # Initializes the repository path using the access token.
59+ REPOSITORY_PATH=" https://${GH_TOKEN} @github.com/${TARGET_REPOSITORY} .git" && \
60+
61+ # Checks to see if the remote exists prior to deploying.
62+ # If the branch doesn't exist it gets created here as an orphan.
63+ if [ " $( git ls-remote --heads " $REPOSITORY_PATH " " $BRANCH " | wc -l) " -eq 0 ];
64+ then
65+ echo " Creating remote branch ${BRANCH} as it doesn't exist..."
66+ mkdir $DOC_FOLDER && \
67+ cd $DOC_FOLDER && \
68+ git init && \
69+ git checkout -b $BRANCH && \
70+ git remote add origin $REPOSITORY_PATH && \
71+ touch README.md && \
72+ git add README.md && \
73+ git commit -m " Initial ${BRANCH} commit" && \
74+ git push " https://$GITHUB_ACTOR :$GH_TOKEN @github.com/$TARGET_REPOSITORY .git" $BRANCH
75+ else
76+ # # Clone the target repository
77+ git clone " $REPOSITORY_PATH " $DOC_FOLDER --branch $BRANCH --single-branch && \
78+ cd $DOC_FOLDER
79+ fi
80+
81+ # Builds the project if a build script is provided.
82+ if [ -n " $BUILD_SCRIPT " ]; then
83+ echo " Running build scripts... $BUILD_SCRIPT " && \
84+ eval " $BUILD_SCRIPT "
85+ fi
86+
87+ if [ -n " $CNAME " ]; then
88+ echo " Generating a CNAME file in in the $PWD directory..."
89+ echo $CNAME > CNAME
90+ git add CNAME
91+ fi
92+
93+ # Commits the data to Github.
94+ if [ -z " $VERSION " ]
95+ then
96+ echo " No Version. Publishing Snapshot of Docs"
97+ if [ -n " ${DOC_SUB_FOLDER} " ]; then
98+ mkdir -p snapshot/$DOC_SUB_FOLDER
99+ cp -r " ../$FOLDER /." ./snapshot/$DOC_SUB_FOLDER /
100+ git add snapshot/$DOC_SUB_FOLDER /*
101+ else
102+ mkdir -p snapshot
103+ cp -r " ../$FOLDER /." ./snapshot/
104+ git add snapshot/*
105+ fi
106+ else
107+ echo " Publishing $VERSION of Docs"
108+ if [ -z " $BETA " ] || [ " $BETA " = " false" ]
109+ then
110+ echo " Publishing Latest Docs"
111+ if [ -n " ${DOC_SUB_FOLDER} " ]; then
112+ mkdir -p latest/$DOC_SUB_FOLDER
113+ cp -r " ../$FOLDER /." ./latest/$DOC_SUB_FOLDER /
114+ git add latest/$DOC_SUB_FOLDER /*
115+ else
116+ mkdir -p latest
117+ cp -r " ../$FOLDER /." ./latest/
118+ git add latest/*
119+ fi
120+ fi
121+
122+ majorVersion=${VERSION: 0: 4}
123+ majorVersion=" ${majorVersion} x"
124+
125+ if [ -n " ${DOC_SUB_FOLDER} " ]; then
126+ mkdir -p " $VERSION /$DOC_SUB_FOLDER "
127+ cp -r " ../$FOLDER /." " ./$VERSION /$DOC_SUB_FOLDER "
128+ git add " $VERSION /$DOC_SUB_FOLDER /*"
129+ else
130+ mkdir -p " $VERSION "
131+ cp -r " ../$FOLDER /." " ./$VERSION /"
132+ git add " $VERSION /*"
133+ fi
134+
135+ if [ -n " ${DOC_SUB_FOLDER} " ]; then
136+ mkdir -p " $majorVersion /$DOC_SUB_FOLDER "
137+ cp -r " ../$FOLDER /." " ./$majorVersion /$DOC_SUB_FOLDER "
138+ git add " $majorVersion /$DOC_SUB_FOLDER /*"
139+ else
140+ mkdir -p " $majorVersion "
141+ cp -r " ../$FOLDER /." " ./$majorVersion /"
142+ git add " $majorVersion /*"
143+ fi
144+ fi
145+
146+
147+ git commit -m " Deploying to ${BRANCH} - $( date +" %T" ) " --quiet && \
148+ git push " https://$GITHUB_ACTOR :$GH_TOKEN @github.com/$TARGET_REPOSITORY .git" $BRANCH || true && \
149+ echo " Deployment successful!"
0 commit comments