diff --git a/.travis.yml b/.travis.yml index 4ea74d2b..90a007a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ jobs: - stage: build name: build docker image - script: bash scripts/image_build.sh $TRAVIS_REPO_SLUG $TRAVIS_BRANCH $TRAVIS_COMMIT + script: bash scripts/image_build.sh $TRAVIS_REPO_SLUG if: env(CD_BUILD_IGNORE) IS NOT present AND type NOT IN (pull_request) AND env(CD_DOCKER_USERNAME) IS present AND env(CD_DOCKER_PASSWORD) IS present notifications: diff --git a/Dockerfile b/Dockerfile index e31dc13c..bd23775d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,8 @@ RUN mkdir -p $RAILS_ROOT WORKDIR $RAILS_ROOT # Set environment variables. +ARG version_code +ENV VERSION_CODE=$version_code ENV RAILS_ENV production # Adding project files. diff --git a/config/initializers/version.rb b/config/initializers/version.rb index 4f36f6bc..2161d051 100644 --- a/config/initializers/version.rb +++ b/config/initializers/version.rb @@ -2,6 +2,6 @@ module Greenlight class Application - VERSION = "2" + VERSION = ENV["VERSION_CODE"] || "2" end end diff --git a/scripts/image_build.sh b/scripts/image_build.sh index 9fb8efbf..047630d8 100755 --- a/scripts/image_build.sh +++ b/scripts/image_build.sh @@ -23,19 +23,17 @@ # as part of the development process. # -echo "v2019022601" - display_usage() { echo "This script should be used as part of a CI strategy." echo -e "Usage:\n build_image.sh [ARGUMENTS]" echo -e "\nMandatory arguments \n" echo -e " repo_slug The git repository (e.g. bigbluebutton/greenlight)" + echo -e "\nOptional arguments \n" echo -e " branch | tag The branch (e.g. master | release-2.0.5)" - echo -e " commit_sha The sha for the current commit (e.g. 750615dd479c23c8873502d45158b10812ea3274)" } # if less than two arguments supplied, display usage -if [ $# -le 1 ]; then +if [ $# -le 0 ]; then display_usage exit 1 fi @@ -48,13 +46,12 @@ fi export CD_REF_SLUG=$1 export CD_REF_NAME=$2 -export CD_COMMIT_SHA=$3 -if [ -z $CD_DOCKER_REPO ]; then - export CD_DOCKER_REPO=$CD_REF_SLUG +if [ -z $CD_REF_NAME ]; then + export CD_REF_NAME=$(git branch | grep \* | cut -d ' ' -f2) fi if [ "$CD_REF_NAME" != "master" ] && [[ "$CD_REF_NAME" != *"release"* ]] && ( [ -z "$CD_BUILD_ALL" ] || [ "$CD_BUILD_ALL" != "true" ] ); then - echo "#### Docker image for $CD_REF_SLUG won't be built" + echo "#### Docker image for $CD_REF_SLUG:$CD_REF_NAME won't be built" exit 0 fi @@ -62,14 +59,17 @@ fi sed -i "/^group :production do/a\ \ gem 'sqlite3', '~> 1.3.6'" Gemfile # Set the version tag when it is a release or the commit sha was included. if [[ "$CD_REF_NAME" == *"release"* ]]; then - sed -i "s/VERSION =.*/VERSION = \"${CD_REF_NAME:8}\"/g" config/initializers/version.rb -elif [ ! -z $CD_COMMIT_SHA ]; then - sed -i "s/VERSION =.*/VERSION = \"$CD_REF_NAME ($(expr substr $CD_COMMIT_SHA 1 8))\"/g" config/initializers/version.rb + export CD_VERSION_CODE=${CD_REF_NAME:8} +else + export CD_VERSION_CODE="$CD_REF_NAME ($(expr substr $(git rev-parse HEAD) 1 7))" fi # Build the image +if [ -z $CD_DOCKER_REPO ]; then + export CD_DOCKER_REPO=$CD_REF_SLUG +fi echo "#### Docker image $CD_DOCKER_REPO:$CD_REF_NAME is being built" -docker build -t $CD_DOCKER_REPO:$CD_REF_NAME . +docker build --build-arg version_code=${CD_VERSION_CODE} -t $CD_DOCKER_REPO:$CD_REF_NAME . if [ -z "$CD_DOCKER_USERNAME" ] || [ -z "$CD_DOCKER_PASSWORD" ]; then echo "#### Docker image for $CD_DOCKER_REPO can't be published because CD_DOCKER_USERNAME or CD_DOCKER_PASSWORD are missing (Ignore this warning if running outside a CD/CI environment)"