Skip to content

Commit 8e14665

Browse files
Aaron Suarezapex-omontgomery
Aaron Suarez
authored andcommitted
Add scripts to build and publish docker image
1 parent 8d028df commit 8e14665

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ dist/
2323
downloads/
2424
eggs/
2525
.eggs/
26-
lib/
2726
lib64/
2827
parts/
2928
sdist/

.travis.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
env:
22
global:
33
- SQLALCHEMY_DATABASE_URI="sqlite:///:memory:"
4+
- IMAGE_NAME=resources-api
5+
- DEPLOY_BRANCHES="master staging"
6+
- AWS_REGION=us-east-2
47
language: python
58
sudo: required
69
dist: xenial
@@ -36,4 +39,5 @@ script:
3639
after_script:
3740
- docker-compose run resources-api coverage xml
3841
- docker-compose -f docker-compose.yml down
39-
- if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then ./cc-test-reporter -r 147d129d98f3d606ce69bc151bbeded40dc684fe11e0f5a831f11f6b36680b22 after-build --exit-code $TRAVIS_TEST_RESULT; fi
42+
- if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then ./cc-test-reporter -r 147d129d98f3d606ce69bc151bbeded40dc684fe11e0f5a831f11f6b36680b22 after-build --exit-code $TRAVIS_TEST_RESULT; fi
43+
- bash bin/run.sh

bin/build

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
# import util functions
5+
source "${SCRIPTDIR}/../lib/util.sh"
6+
7+
echo "Building Docker image..."
8+
runCommand "docker build -t $IMAGE_NAME -f Dockerfile ." || exit $?

bin/publish

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
# Publishes the most recent web container to docker hubs repo.
4+
# This script assumes docker push works.
5+
# You must set up docker push on your own.
6+
7+
set -eu
8+
9+
DOCKER_REPO="operationcode/resources-api"
10+
11+
IMAGE_ID=$(docker images $IMAGE_NAME:latest --format "{{.ID}}")
12+
13+
if [ -n "$DOCKER_USERNAME" ]; then echo "Found username"; fi
14+
if [ -n "$DOCKER_PASSWORD" ]; then echo "Found password"; fi
15+
16+
if [ -n "$DOCKER_USERNAME" ] && [ -n "$DOCKER_PASSWORD" ]; then
17+
echo "Logging in using ENV creds"
18+
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
19+
fi
20+
21+
echo "Pushing image $IMAGE_NAME:$TRAVIS_BRANCH"
22+
docker tag $IMAGE_ID $DOCKER_REPO
23+
docker tag $IMAGE_ID ${DOCKER_REPO}:${TRAVIS_BUILD_NUMBER}
24+
docker push $DOCKER_REPO
25+
docker push ${DOCKER_REPO}:${TRAVIS_BUILD_NUMBER}

bin/run.sh

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
# Only process first job in matrix (TRAVIS_JOB_NUMBER ends with ".1")
4+
if [[ ! $TRAVIS_JOB_NUMBER =~ \.1$ ]]; then
5+
echo "Skipping deploy since it's not the first job in matrix"
6+
exit 0
7+
fi
8+
9+
# Don't process pull requests
10+
# $TRAVIS_PULL_REQUEST will be the PR number or "false" if not a PR
11+
if [[ -n "$TRAVIS_PULL_REQUEST" ]] && [[ "$TRAVIS_PULL_REQUEST" != "false" ]]; then
12+
echo "Skipping deploy because it's a pull request"
13+
exit 0
14+
fi
15+
16+
# Only process branches listed in DEPLOY_BRANCHES
17+
BRANCHES_TO_DEPLOY=($DEPLOY_BRANCHES)
18+
if [[ ! " ${BRANCHES_TO_DEPLOY[@]} " =~ " ${TRAVIS_BRANCH} " ]]; then
19+
# whatever you want to do when arr contains value
20+
echo "Skipping deploy, not a branch to be deployed"
21+
exit 0
22+
fi
23+
24+
pip install awscli -q
25+
26+
if [ $? = 0 ]; then
27+
AWSBIN=$(which aws)
28+
AWSPATH=$(dirname $AWSBIN)
29+
export PATH=$PATH:$AWSPATH
30+
31+
# Get absolute path of dir where run.sh is located
32+
SOURCE="${BASH_SOURCE[0]}"
33+
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
34+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
35+
SOURCE="$(readlink "$SOURCE")"
36+
[[ $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
37+
done
38+
export SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
39+
40+
bash ${SCRIPTDIR}/build &&
41+
bash ${SCRIPTDIR}/publish
42+
43+
else
44+
echo "Failed to install AWS CLI"
45+
exit 1
46+
fi

lib/util.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
function isDryRun {
4+
if [ "$DRYRUN" = "1" ]; then
5+
return 0
6+
else
7+
return 1
8+
fi
9+
}
10+
11+
function runCommand {
12+
if isDryRun; then
13+
echo $1;
14+
else
15+
eval $1
16+
return $?
17+
fi
18+
}

0 commit comments

Comments
 (0)