Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Deploys app3 from version control, runs tests and if tests pass, runs new version of the app. If tests fail or any other error, sends email.
- # Write some HTTP headers to make Nginx happy
- echo -e "Content-type: text/html\n"
- # Variables
- DIRECTORY="$PWD/public"
- VCS_URL=git@github.com:henno/foo.git
- OUTPUT=''
- log() {
- # Output immediately (for terminal and web)
- echo -e $1
- # Collect OUTPUT (for sending email later)
- OUTPUT+="$1"$'\n'
- }
- cmd() {
- log "Doing '$@'"
- # Run the command and capture its output and exit code
- COMMAND_OUTPUT=`$@ 2>&1`
- COMMAND_EXIT_CODE=$?
- # Add command output to OUTPUT
- OUTPUT+=$COMMAND_OUTPUT$'\n'
- # Send the command output to stdout, too
- echo -e $COMMAND_OUTPUT$'\n'
- if [[ $COMMAND_EXIT_CODE -ne 0 ]]; then
- log "The previous command returned non-zero exit code. Aborting."
- # Send email to me
- mail -s 'message subject' -aFrom:app3@fadsghafdhdfhjnbb.club henno.taht@gmail.com <<< $OUTPUT
- # Stop the script
- exit $?
- fi
- }
- log "Deploying $VCS_URL to $DIRECTORY"$'\n''--------------------------------------------------------------------------'
- # Update project or re-create it from the scratch
- if [ -d "$DIRECTORY" ] && grep -q $VCS_URL "$DIRECTORY/.git/config"
- then
- # Update project
- # Change directory to the app folder
- cd $DIRECTORY
- log "The project has already been cloned here."
- log "Updating the project"
- # Change potentially changed files (only tracked ones) back to the state they were originally in Github
- cmd "git reset --hard"
- # Pull the new commits from Github
- cmd 'git pull'
- else
- # Re-create the project folder from the scratch
- log "Either the .git folder did not exist, or the config file did not exist or it did not contain VCS_URL"
- if [ -d "$DIRECTORY" ]; then
- log "Cleaning directory"
- rm -rvf "$DIRECTORY/*" "$DIRECTORY/.*"
- else
- log "The app folder did not exist"
- mkdir -p "$DIRECTORY"
- fi
- cd "$DIRECTORY"
- log "Doing git clone"
- git clone $VCS_URL public 2>&1
- fi
- # Run tests
- cmd 'npm run test'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement