Advertisement
henno

deploy.cgi

Nov 24th, 2020
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.07 KB | None | 0 0
  1. #!/bin/bash
  2. # 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.
  3.  
  4. # Write some HTTP headers to make Nginx happy
  5. echo -e "Content-type: text/html\n"
  6.  
  7. # Variables
  8. DIRECTORY="$PWD/public"
  9. VCS_URL=git@github.com:henno/foo.git
  10. OUTPUT=''
  11.  
  12. log() {
  13.     # Output immediately (for terminal and web)
  14.     echo -e $1
  15.  
  16.     # Collect OUTPUT (for sending email later)
  17.     OUTPUT+="$1"$'\n'
  18. }
  19.  
  20. cmd() {
  21.     log "Doing '$@'"
  22.  
  23.     # Run the command and capture its output and exit code
  24.     COMMAND_OUTPUT=`$@ 2>&1`
  25.     COMMAND_EXIT_CODE=$?
  26.  
  27.     # Add command output to OUTPUT
  28.     OUTPUT+=$COMMAND_OUTPUT$'\n'
  29.  
  30.     # Send the command output to stdout, too
  31.     echo -e $COMMAND_OUTPUT$'\n'
  32.  
  33.     if [[ $COMMAND_EXIT_CODE -ne 0 ]]; then
  34.  
  35.         log "The previous command returned non-zero exit code. Aborting."
  36.  
  37.         # Send email to me
  38.         mail -s 'message subject' -aFrom:app3@fadsghafdhdfhjnbb.club  henno.taht@gmail.com <<< $OUTPUT
  39.  
  40.         # Stop the script
  41.         exit $?
  42.     fi
  43. }
  44.  
  45. log "Deploying $VCS_URL to $DIRECTORY"$'\n''--------------------------------------------------------------------------'
  46.  
  47. # Update project or re-create it from the scratch
  48. if [ -d "$DIRECTORY" ] && grep -q $VCS_URL "$DIRECTORY/.git/config"
  49. then
  50.  
  51.         # Update project
  52.  
  53.         # Change directory to the app folder
  54.         cd $DIRECTORY
  55.  
  56.         log "The project has already been cloned here."
  57.         log "Updating the project"
  58.  
  59.         # Change potentially changed files (only tracked ones) back to the state they were originally in Github
  60.         cmd "git reset --hard"
  61.  
  62.         # Pull the new commits from Github
  63.         cmd 'git pull'
  64.  
  65.     else
  66.  
  67.         # Re-create the project folder from the scratch
  68.  
  69.         log "Either the .git folder did not exist, or the config file did not exist or it did not contain VCS_URL"
  70.  
  71.         if [ -d "$DIRECTORY" ]; then
  72.  
  73.             log "Cleaning directory"
  74.             rm -rvf "$DIRECTORY/*" "$DIRECTORY/.*"
  75.         else
  76.             log "The app folder did not exist"
  77.             mkdir -p "$DIRECTORY"
  78.         fi
  79.  
  80.         cd "$DIRECTORY"
  81.  
  82.         log "Doing git clone"
  83.         git clone $VCS_URL public 2>&1
  84. fi
  85.  
  86.  
  87. # Run tests
  88. cmd 'npm run test'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement