From 7daa4492ec61dad6ccb1e4d5dd348d1b2828d8e8 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Fri, 4 Jan 2019 11:11:37 +0100
Subject: [PATCH] #12 Proper versions for script and images at the same time

---
 .gitlab-ci.yml | 31 ++++++++++++++++++--------
 Dockerfile     |  3 ++-
 l3d            | 60 +++++++++++++++++++++++++++++++++-----------------
 3 files changed, 64 insertions(+), 30 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 0855ba2..ddc4f97 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,26 +1,39 @@
+variables:
+  VERSION: ${CI_COMMIT_TAG}
+
 before_script:
   - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
 
+l3d-script:
+  stage: build
+  script: sed -i -e "s/'{{ version }}'/${VERSION}/g" l3d
+  artifacts:
+    name: l3d
+    paths:
+      - l3d
+  only:
+    - tags
+
 php-7.0:
   stage: build
   script:
-    - docker build --pull --build-arg PHP_VERSION=7.0 -t $CI_REGISTRY_IMAGE:php-7.0 .
-    - docker push $CI_REGISTRY_IMAGE:php-7.0
+    - docker build --pull --build-arg PHP_VERSION=7.0 --build-arg VERSION=${VERSION} -t ${CI_REGISTRY_IMAGE}/php-7.0:${VERSION} .
+    - docker push ${CI_REGISTRY_IMAGE}/php-7.0:${VERSION}
   only:
-    - master
+    - tags
 
 php-7.1:
   stage: build
   script:
-    - docker build --pull --build-arg PHP_VERSION=7.1 -t $CI_REGISTRY_IMAGE:php-7.1 .
-    - docker push $CI_REGISTRY_IMAGE:php-7.1
+    - docker build --pull --build-arg PHP_VERSION=7.1 --build-arg VERSION=${VERSION} -t ${CI_REGISTRY_IMAGE}/php-7.1:${VERSION} .
+    - docker push ${CI_REGISTRY_IMAGE}/php-7.1:${VERSION}
   only:
-    - master
+    - tags
 
 php-7.2:
   stage: build
   script:
-    - docker build --pull --build-arg PHP_VERSION=7.2 -t $CI_REGISTRY_IMAGE:php-7.2 .
-    - docker push $CI_REGISTRY_IMAGE:php-7.2
+    - docker build --pull --build-arg PHP_VERSION=7.2 --build-arg VERSION=${VERSION} -t ${CI_REGISTRY_IMAGE}/php-7.2:${VERSION} .
+    - docker push ${CI_REGISTRY_IMAGE}/php-7.2:${VERSION}
   only:
-    - master
+    - tags
diff --git a/Dockerfile b/Dockerfile
index 4209d83..c0bfe30 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,10 +1,11 @@
 ARG PHP_VERSION
+ARG VERSION
 
 FROM registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_VERSION}
 
 LABEL com.example.vendor="LakeDrops" \
       maintainer="juergen.haas@lakedrops.com" \
-      version="1.2.2" \
+      version="${VERSION}" \
       description="Drupal development environment from LakeDrops."
 
 ENV LAKEDROPS_DEV_ENV 1
diff --git a/l3d b/l3d
index e21c1c8..f76ee7a 100755
--- a/l3d
+++ b/l3d
@@ -1,39 +1,51 @@
 #!/usr/bin/env bash
 
-function help {
+VERSION='{{ version }}'
+
+function showHelp {
   echo "L3D - LakeDropsDrupalDev"
   echo "========================="
   echo ""
-  echo "Version: 1.4.1"
+  echo "Version: ${VERSION}"
   echo "Info and support: https://gitlab.lakedrops.com/docker/l3d"
   echo ""
   echo "Usage:"
-  echo "  l3d [ help | selfupdate | update | PROJECTNAME]"
+  echo "  l3d [ help | version | selfupdate | update | PROJECTNAME ]"
   echo ""
   echo ""
 }
 
-function selfupdate {
+function showVersion {
+  echo "${VERSION}"
+}
+
+function selfUpdate {
+  NEWVERSION=$(curl -s https://gitlab.lakedrops.com/api/v4/projects/282/repository/tags | grep -o '[0-9]*\.[0-9]*\.[0-9]*' | head -1)
+  if [[ "$NEWVERSION" == "$VERSION" ]]; then
+    echo "No new version available"
+    return
+  fi
+  NEWSCRIPT="https://gitlab.lakedrops.com/docker/l3d/-/jobs/artifacts/${NEWVERSION}/raw/l3d?job=l3d-script"
   DEST=$(which l3d)
   WGET=$(which wget)
   CURL=$(which curl)
   if [[ -n "$WGET" ]]; then
-    sudo wget -O $DEST https://gitlab.lakedrops.com/docker/l3d/raw/master/l3d > /dev/null 2>&1
+    sudo wget -O ${DEST} ${NEWSCRIPT} > /dev/null 2>&1
   elif [[ -n "$CURL" ]]; then
-    sudo curl -o $DEST https://gitlab.lakedrops.com/docker/l3d/raw/master/l3d > /dev/null 2>&1
+    sudo curl -o ${DEST} ${NEWSCRIPT} > /dev/null 2>&1
   else
     echo "Could't find wget or curl to perform the update!"
     exit 1
   fi
-  sudo chmod +x $DEST
+  sudo chmod +x ${DEST}
   echo "Self update succeeded!"
 }
 
-function update {
-  getenv
-  IMAGEID=$(docker image ls -q registry.lakedrops.com/docker/l3d:php-${PHP_VERSION})
+function updateImage {
+  getConfig
+  IMAGEID=$(docker image ls -q registry.lakedrops.com/docker/l3d/php-${PHP_VERSION}:${VERSION})
   echo "Updating the image ..."
-  STATUS=$(docker pull registry.lakedrops.com/docker/l3d:php-${PHP_VERSION})
+  STATUS=$(docker pull registry.lakedrops.com/docker/l3d/php-${PHP_VERSION}:${VERSION})
   if [[ "$STATUS" == *"Status: Image is up to date"* ]]; then
     echo "Already up to date"
   else
@@ -53,7 +65,7 @@ function update {
   fi
 }
 
-function getenv {
+function getConfig {
   if [[ -f ".env" ]]; then
     export $(cat .env | xargs) > /dev/null 2>&1
   fi
@@ -76,7 +88,7 @@ function getenv {
   fi
 }
 
-function start {
+function startContainer {
   if [[ -n "${COMPOSE_PROJECT_NAME}" ]]; then
     ID=$(docker container ls -q -f name=${COMPOSE_PROJECT_NAME}_l3d)
 
@@ -95,7 +107,7 @@ function start {
         --volume ${HOME}/.gitconfig:/root/.gitconfig \
         --workdir /drupal \
         --restart unless-stopped \
-        registry.lakedrops.com/docker/l3d:php-${PHP_VERSION}
+        registry.lakedrops.com/docker/l3d/php-${PHP_VERSION}:${VERSION}
     fi
 
     EXISTING=$(ls -1)
@@ -108,19 +120,26 @@ function start {
 }
 
 COMPOSE_PROJECT_NAME=
+if [[ "$VERSION" == "{{ version }}" ]]; then
+  selfUpdate
+fi
 case $1 in
 
   help )
-    help
+    showHelp
+    ;;
+
+  version )
+    showVersion
     ;;
 
   selfupdate )
-    selfupdate
+    selfUpdate
     ;;
 
   update )
-    update
-    start
+    updateImage
+    startContainer
     ;;
 
   * )
@@ -130,8 +149,9 @@ case $1 in
         COMPOSE_PROJECT_NAME=$1
       fi
     else
-      getenv
+      getConfig
     fi
-    start
+    startContainer
     ;;
+
 esac
-- 
GitLab