Skip to content

Instantly share code, notes, and snippets.

@bernard-ng
Last active August 31, 2020 18:23
Show Gist options
  • Save bernard-ng/c042072206604846bafe35bd35be9b63 to your computer and use it in GitHub Desktop.
Save bernard-ng/c042072206604846bafe35bd35be9b63 to your computer and use it in GitHub Desktop.
a script that deploys a angular or ionic app to a production server using git
BUILD_DEPLOY_DIRECTORY="$HOME/dev/projects/app-build"
BUILD_DEPLOY_REMOTE=$(git config --get remote.origin.url)
BUILD_DIRECTORY="$HOME/dev/projects/app/www" # www or build
BUILD_COMMIT_MESSAGE=$(date +'%c')
R=$(tput setaf 1)
G=$(tput setaf 2)
Y=$(tput setaf 3)
NC=$(tput sgr0)
function angular_cli_build() {
printf "Building the project with angular CLI...\n\n"
ng lint &&
ng build --prod &&
printf "%sApplication built with angular CLI ! %s \n\n" "$G" "$NC"
}
function delete_previous_build() {
printf "Changing directory to build-deploy directory... \n\n"
cd "$BUILD_DEPLOY_DIRECTORY" || exit 2
printf "%sChanged, current directory : (%s) ! %s \n" "$G" "$PWD" "$NC"
printf "%sDeleting previous build files except from (.htaccess)... %s \n\n" "$Y" "$NC"
if [ "$PWD" == "$BUILD_DEPLOY_DIRECTORY" ]; then
rm -vrf $(ls -I ".htaccess" -aI ".git")
printf "%sPrevious build files deleted %s \n\n" "$G" "$NC"
else
printf "%sTrying to delete files in the wrong directory ! %s \n" "$G" "$NC"
exit 1
fi
}
function version_and_deploy() {
printf "Copying new built files from build to build-deploy directory... \n\n"
cp -av "$BUILD_DIRECTORY/." "$BUILD_DEPLOY_DIRECTORY"
printf "%sFiles copied to (%s) ! %s \n\n" "$G" "$BUILD_DEPLOY_DIRECTORY" "$NC"
printf "Versioning new build... \n\n"
git add . &&
git commit -m "build(app): ${BUILD_COMMIT_MESSAGE}" &&
printf "%sNew build versioned %s \n" "$G" "$NC"
printf "%sDeploying to remote server (%s) ... %s \n\n" "$Y" "$BUILD_DEPLOY_REMOTE" "$NC" &&
git push origin master &&
printf "%s Deployed ! %s \n" "$G" "$NC"
}
printf "%sDeploy process started, current directory : (%s) %s \n\n" "$Y" "$PWD" "$NC"
if [ ! -d "$BUILD_DEPLOY_DIRECTORY" ]; then
printf "%sBuild-deploy directory doesn't exist : (%s) %s \n" "$R" "$BUILD_DEPLOY_DIRECTORY" "$NC"
exit 2
fi
angular_cli_build && delete_previous_build && version_and_deploy &&
printf "%s Changing directory to build directory... %s \n" "$G" "$NC"
cd "$BUILD_DIRECTORY" || exit 2
printf "%s Finished ! %s \n\n" "$G" "$NC"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment