Skip to content

Continuous integration deploy

Justin Littman edited this page Aug 14, 2015 · 14 revisions

Continuous integration deploy

The following describes the steps for setting up a continuous integration deploy on an Amazon EC2 Ubuntu Server 14.04 LTS. It can be readily adapted to other environments.

The deploy upgrades docker and docker-compose, fetches the latest docker-compose.yml, pulls the latest images, and restarts the containers.

  1. As root, install docker and docker-compose:

     apt-get update
     curl -sSL https://get.docker.com/ | sh
     usermod -aG docker ubuntu
     apt-get install python-pip
     pip install -U docker-compose
    
  2. Follow these (or similar) instructions for setting up email.

  3. Add the deploy script as /home/ubuntu/run_sfm.sh:

     #!/bin/bash 
    
     function error_exit
     {
     	echo "SFM CI deploy: $1" 1>&2
     	exit 1
     }
    
     export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    
     #Uograde docker and docker-compose
     curl -sSL https://get.docker.com/ > get_docker.sh
     if [ "$?" != "0" ]; then
     	error_exit "Downloading docker install failed"
     fi
    
     sh get_docker.sh
     if [ "$?" != "0" ]; then
     	error_exit "Docker install failed"
     fi
    
     pip install -U docker-compose
     if [ "$?" != "0" ]; then
             error_exit "Upgrading docker-compose failed"
     fi
    
     #Get files
     curl -L https://github.com/gwu-libraries/sfm/raw/t6-docker/docker-compose.yml > docker-compose.yml
     if [ "$?" != "0" ]; then
             error_exit "Fetching docker-compose.yml failed"
     fi
     curl -L https://github.com/gwu-libraries/sfm/raw/t6-docker/example.secrets.env > secrets.env
     if [ "$?" != "0" ]; then
             error_exit "Fetching secrets.env failed"
     fi
    
     #Pull containers
     docker-compose pull
     if [ "$?" != "0" ]; then
             error_exit "Pulling containers failed"
     fi
    
     #Start containers
     docker-compose up -d
     if [ "$?" != "0" ]; then
             error_exit "Bringing up containers failed"
     fi
    
     echo "SFM CI deploy: Succeeded"
    

and then make executable:

    chmod +x /home/ubuntu/run_sfm.sh
  1. Setup cron to run script at 1am every night and email the results:

     sudo crontab -e
    

and add line (with appropriate email address):

    0 1 * * * /home/ubuntu/run_sfm.sh > /home/ubuntu/log.txt 2>&1; cat /home/ubuntu/log.txt | mail -s "`tail -1 /home/ubuntu/log.txt`" justinlittman@email.gwu.edu