From 3670299186908866964e8a5306c1d79682581492 Mon Sep 17 00:00:00 2001 From: Aaron Suarez Date: Thu, 25 Apr 2019 22:04:00 -0500 Subject: [PATCH 1/2] Add scripts to build and publish docker image --- .gitignore | 1 - .travis.yml | 6 +++++- bin/build | 8 ++++++++ bin/publish | 25 +++++++++++++++++++++++++ bin/run.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lib/util.sh | 18 ++++++++++++++++++ 6 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 bin/build create mode 100644 bin/publish create mode 100644 bin/run.sh create mode 100644 lib/util.sh diff --git a/.gitignore b/.gitignore index 0ab8dca3..10793ef3 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ lib64/ parts/ sdist/ diff --git a/.travis.yml b/.travis.yml index c64b8f4a..08e70266 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,9 @@ env: global: - SQLALCHEMY_DATABASE_URI="sqlite:///:memory:" + - IMAGE_NAME=resources-api + - DEPLOY_BRANCHES="master staging" + - AWS_REGION=us-east-2 language: python sudo: required dist: xenial @@ -36,4 +39,5 @@ script: after_script: - docker-compose run resources-api coverage xml - docker-compose -f docker-compose.yml down - - if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then ./cc-test-reporter -r 147d129d98f3d606ce69bc151bbeded40dc684fe11e0f5a831f11f6b36680b22 after-build --exit-code $TRAVIS_TEST_RESULT; fi \ No newline at end of file + - if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then ./cc-test-reporter -r 147d129d98f3d606ce69bc151bbeded40dc684fe11e0f5a831f11f6b36680b22 after-build --exit-code $TRAVIS_TEST_RESULT; fi + - bash bin/run.sh diff --git a/bin/build b/bin/build new file mode 100644 index 00000000..25e99e96 --- /dev/null +++ b/bin/build @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -eu +# import util functions +source "${SCRIPTDIR}/../lib/util.sh" + +echo "Building Docker image..." +runCommand "docker build -t $IMAGE_NAME -f Dockerfile ." || exit $? diff --git a/bin/publish b/bin/publish new file mode 100644 index 00000000..4bc00ffa --- /dev/null +++ b/bin/publish @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +# Publishes the most recent web container to docker hubs repo. +# This script assumes docker push works. +# You must set up docker push on your own. + +set -eu + +DOCKER_REPO="operationcode/resources-api" + +IMAGE_ID=$(docker images $IMAGE_NAME:latest --format "{{.ID}}") + +if [ -n "$DOCKER_USERNAME" ]; then echo "Found username"; fi +if [ -n "$DOCKER_PASSWORD" ]; then echo "Found password"; fi + +if [ -n "$DOCKER_USERNAME" ] && [ -n "$DOCKER_PASSWORD" ]; then + echo "Logging in using ENV creds" + docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" +fi + +echo "Pushing image $IMAGE_NAME:$TRAVIS_BRANCH" +docker tag $IMAGE_ID $DOCKER_REPO +docker tag $IMAGE_ID ${DOCKER_REPO}:${TRAVIS_BUILD_NUMBER} +docker push $DOCKER_REPO +docker push ${DOCKER_REPO}:${TRAVIS_BUILD_NUMBER} diff --git a/bin/run.sh b/bin/run.sh new file mode 100644 index 00000000..0479818d --- /dev/null +++ b/bin/run.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +# Only process first job in matrix (TRAVIS_JOB_NUMBER ends with ".1") +if [[ ! $TRAVIS_JOB_NUMBER =~ \.1$ ]]; then + echo "Skipping deploy since it's not the first job in matrix" + exit 0 +fi + +# Don't process pull requests +# $TRAVIS_PULL_REQUEST will be the PR number or "false" if not a PR +if [[ -n "$TRAVIS_PULL_REQUEST" ]] && [[ "$TRAVIS_PULL_REQUEST" != "false" ]]; then + echo "Skipping deploy because it's a pull request" + exit 0 +fi + +# Only process branches listed in DEPLOY_BRANCHES +BRANCHES_TO_DEPLOY=($DEPLOY_BRANCHES) +if [[ ! " ${BRANCHES_TO_DEPLOY[@]} " =~ " ${TRAVIS_BRANCH} " ]]; then + # whatever you want to do when arr contains value + echo "Skipping deploy, not a branch to be deployed" + exit 0 +fi + +pip install awscli -q + +if [ $? = 0 ]; then + AWSBIN=$(which aws) + AWSPATH=$(dirname $AWSBIN) + export PATH=$PATH:$AWSPATH + + # Get absolute path of dir where run.sh is located + SOURCE="${BASH_SOURCE[0]}" + while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located + done + export SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + + bash ${SCRIPTDIR}/build && + bash ${SCRIPTDIR}/publish + +else + echo "Failed to install AWS CLI" + exit 1 +fi diff --git a/lib/util.sh b/lib/util.sh new file mode 100644 index 00000000..18e36517 --- /dev/null +++ b/lib/util.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +function isDryRun { + if [ "$DRYRUN" = "1" ]; then + return 0 + else + return 1 + fi +} + +function runCommand { + if isDryRun; then + echo $1; + else + eval $1 + return $? + fi +} From 54b065669c1e8d2bfd5ff78972717622e46f8761 Mon Sep 17 00:00:00 2001 From: wimo7083 Date: Sat, 27 Apr 2019 21:00:33 -0600 Subject: [PATCH 2/2] make adjustments to try out dockerhub --- .travis.yml | 3 +-- bin/run.sh | 8 -------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 08e70266..d7d9b9cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,7 @@ env: global: - SQLALCHEMY_DATABASE_URI="sqlite:///:memory:" - IMAGE_NAME=resources-api - - DEPLOY_BRANCHES="master staging" - - AWS_REGION=us-east-2 + - DEPLOY_BRANCHES="master" language: python sudo: required dist: xenial diff --git a/bin/run.sh b/bin/run.sh index 0479818d..79826edf 100644 --- a/bin/run.sh +++ b/bin/run.sh @@ -21,12 +21,7 @@ if [[ ! " ${BRANCHES_TO_DEPLOY[@]} " =~ " ${TRAVIS_BRANCH} " ]]; then exit 0 fi -pip install awscli -q - if [ $? = 0 ]; then - AWSBIN=$(which aws) - AWSPATH=$(dirname $AWSBIN) - export PATH=$PATH:$AWSPATH # Get absolute path of dir where run.sh is located SOURCE="${BASH_SOURCE[0]}" @@ -40,7 +35,4 @@ if [ $? = 0 ]; then bash ${SCRIPTDIR}/build && bash ${SCRIPTDIR}/publish -else - echo "Failed to install AWS CLI" - exit 1 fi