Review build script

This commit is contained in:
CrazyMax 2018-12-03 04:46:49 +01:00
parent 8883354cfc
commit 216258c4d1
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
3 changed files with 119 additions and 33 deletions

View File

@ -8,6 +8,7 @@
/.editorconfig /.editorconfig
/.gitignore /.gitignore
/.travis.yml /.travis.yml
/build.sh
/CHANGELOG.md /CHANGELOG.md
/LICENSE /LICENSE
/README.md /README.md

View File

@ -6,8 +6,10 @@ services:
env: env:
global: global:
- VERSION=17955 - VERSION=17955
- DOCKER_LOGIN=crazymax
- DOCKER_USERNAME=crazymax - DOCKER_USERNAME=crazymax
- DOCKER_REPONAME=jetbrains-license-server - DOCKER_REPONAME=jetbrains-license-server
- QUAY_LOGIN=crazymax
- QUAY_USERNAME=crazymax - QUAY_USERNAME=crazymax
- QUAY_REPONAME=jetbrains-license-server - QUAY_REPONAME=jetbrains-license-server
- MICROBADGER_HOOK=https://hooks.microbadger.com/images/crazymax/jetbrains-license-server/PWF7sHnaK1djP4E9r5VY5suUqVk= - MICROBADGER_HOOK=https://hooks.microbadger.com/images/crazymax/jetbrains-license-server/PWF7sHnaK1djP4E9r5VY5suUqVk=
@ -17,40 +19,8 @@ env:
before_install: before_install:
- sudo apt-get update - sudo apt-get update
- docker --version - docker --version
- export BRANCH=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo $TRAVIS_PULL_REQUEST_BRANCH; fi)
- export DOCKER_TAG=$(if [ "$BRANCH" == "master" ]; then echo "latest"; else echo $BRANCH; fi)
- export WAN_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
- echo "WAN_IP=$WAN_IP, TRAVIS_BRANCH=$TRAVIS_BRANCH, PR=$PR, BRANCH=$BRANCH, DOCKER_TAG=$DOCKER_TAG"
install: script: ./build.sh
- |
docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
--build-arg VCS_REF=${TRAVIS_COMMIT::8} \
--build-arg VERSION=${VERSION} \
-t docker_build .
before_script:
- docker run -d -p 8000:80 -e JLS_VIRTUAL_HOSTS=$WAN_IP --name $DOCKER_REPONAME docker_build
- sleep 20
- docker logs $DOCKER_REPONAME
script:
- docker ps | grep $DOCKER_REPONAME
- |
test $TRAVIS_PULL_REQUEST = false \
&& echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin \
&& docker tag docker_build $DOCKER_USERNAME/$DOCKER_REPONAME:$DOCKER_TAG \
&& docker tag docker_build $DOCKER_USERNAME/$DOCKER_REPONAME:$VERSION \
&& docker push $DOCKER_USERNAME/$DOCKER_REPONAME \
&& curl -X POST $MICROBADGER_HOOK \
&& echo "$QUAY_PASSWORD" | docker login quay.io --username "$QUAY_USERNAME" --password-stdin \
&& docker tag docker_build quay.io/$DOCKER_USERNAME/$DOCKER_REPONAME:$DOCKER_TAG \
&& docker tag docker_build quay.io/$DOCKER_USERNAME/$DOCKER_REPONAME:$VERSION \
&& docker push quay.io/$QUAY_USERNAME/$QUAY_REPONAME
branches:
except:
- /^[0-9]/
notifications: notifications:
webhooks: webhooks:

115
build.sh Normal file
View File

@ -0,0 +1,115 @@
#!/usr/bin/env bash
set -e
PROJECT=jetbrains-licenses-server
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
BUILD_TAG=docker_build
VCS_REF=${TRAVIS_COMMIT::8}
RUNNING_TIMEOUT=120
RUNNING_LOG_CHECK="is listening on"
PUSH_LATEST=${PUSH_LATEST:-true}
DOCKER_USERNAME=${DOCKER_USERNAME:="crazymax"}
DOCKER_LOGIN=${DOCKER_LOGIN:="crazymax"}
DOCKER_REPONAME=${DOCKER_REPONAME:="jetbrains-licenses-server"}
QUAY_USERNAME=${QUAY_USERNAME:="crazymax"}
QUAY_LOGIN=${QUAY_LOGIN:="crazymax"}
QUAY_REPONAME=${QUAY_REPONAME:="jetbrains-licenses-server"}
# Check local or travis
BRANCH=${TRAVIS_BRANCH:-local}
if [[ ${TRAVIS_PULL_REQUEST} == "true" ]]; then
BRANCH=${TRAVIS_PULL_REQUEST_BRANCH}
fi
DOCKER_TAG=${BRANCH:-local}
if [[ "$BRANCH" == "master" ]]; then
DOCKER_TAG=latest
elif [[ "$BRANCH" == "local" ]]; then
BUILD_DATE=
VERSION=local
fi
echo "PROJECT=${PROJECT}"
echo "VERSION=${VERSION}"
echo "BUILD_DATE=${BUILD_DATE}"
echo "BUILD_TAG=${BUILD_TAG}"
echo "VCS_REF=${VCS_REF}"
echo "PUSH_LATEST=${PUSH_LATEST}"
echo "DOCKER_LOGIN=${DOCKER_LOGIN}"
echo "DOCKER_USERNAME=${DOCKER_USERNAME}"
echo "DOCKER_REPONAME=${DOCKER_REPONAME}"
echo "QUAY_LOGIN=${QUAY_LOGIN}"
echo "QUAY_USERNAME=${QUAY_USERNAME}"
echo "QUAY_REPONAME=${QUAY_REPONAME}"
echo "TRAVIS_BRANCH=${TRAVIS_BRANCH}"
echo "TRAVIS_PULL_REQUEST=${TRAVIS_PULL_REQUEST}"
echo "BRANCH=${BRANCH}"
echo "DOCKER_TAG=${DOCKER_TAG}"
echo
# Build
echo "### Build"
docker build \
--build-arg BUILD_DATE=${BUILD_DATE} \
--build-arg VCS_REF=${VCS_REF} \
--build-arg VERSION=${VERSION} \
-t ${BUILD_TAG} .
echo
echo "### Test"
docker rm -f ${PROJECT} || true
docker run -d -p 8000:80 \
-e "JLS_VIRTUAL_HOSTS=docker.test" \
--name ${PROJECT} ${BUILD_TAG}
echo
echo "### Waiting for ${PROJECT} to be up..."
TIMEOUT=$((SECONDS + RUNNING_TIMEOUT))
while read LOGLINE; do
echo ${LOGLINE}
if [[ ${LOGLINE} == *"${RUNNING_LOG_CHECK}"* ]]; then
echo "Container up!"
break
fi
if [[ $SECONDS -gt ${TIMEOUT} ]]; then
>&2 echo "ERROR: Failed to run ${PROJECT} container"
docker rm -f ${PROJECT} > /dev/null 2>&1 || true
exit 1
fi
done < <(docker logs -f ${PROJECT} 2>&1)
docker rm -f ${PROJECT} > /dev/null 2>&1 || true
echo
if [ "${VERSION}" == "local" -o "${TRAVIS_PULL_REQUEST}" == "true" ]; then
echo "INFO: This is a PR or a local build, skipping push..."
exit 0
fi
if [[ ! -z ${DOCKER_PASSWORD} ]]; then
echo "### Push to Docker Hub..."
echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_LOGIN" --password-stdin > /dev/null 2>&1
if [ "${DOCKER_TAG}" == "latest" -a "${PUSH_LATEST}" == "true" ]; then
docker tag ${BUILD_TAG} ${DOCKER_USERNAME}/${DOCKER_REPONAME}:${DOCKER_TAG}
fi
if [[ "${VERSION}" != "latest" ]]; then
docker tag ${BUILD_TAG} ${DOCKER_USERNAME}/${DOCKER_REPONAME}:${VERSION}
fi
docker push ${DOCKER_USERNAME}/${DOCKER_REPONAME}
if [[ ! -z ${MICROBADGER_HOOK} ]]; then
echo "Call MicroBadger hook"
curl -X POST ${MICROBADGER_HOOK}
echo
fi
echo
fi
if [[ ! -z ${QUAY_PASSWORD} ]]; then
echo "### Push to Quay..."
echo "$QUAY_PASSWORD" | docker login quay.io --username "$QUAY_LOGIN" --password-stdin > /dev/null 2>&1
if [ "${DOCKER_TAG}" == "latest" -a "${PUSH_LATEST}" == "true" ]; then
docker tag ${BUILD_TAG} quay.io/${QUAY_USERNAME}/${QUAY_REPONAME}:${DOCKER_TAG}
fi
if [[ "${VERSION}" != "latest" ]]; then
docker tag ${BUILD_TAG} quay.io/${QUAY_USERNAME}/${QUAY_REPONAME}:${VERSION}
fi
docker push quay.io/${QUAY_USERNAME}/${QUAY_REPONAME}
echo
fi