From 529c3cda4cc5e7ec18ad6220b0c6cf371bdbd933 Mon Sep 17 00:00:00 2001
From: Roman <roman@jointech.org>
Date: Mon, 7 Mar 2022 16:27:18 +0100
Subject: [PATCH] Feat(gitlab-ci): Add gitlab-ci pipeline definition for
 releases

Add pipeline to build and push to docker hub
    when there is a new commit to main: tagged as unstable
    when a tag is pushed: tagged as latest and with the tag
        afterwards create a release in gitlab when pushing was successful
---
 .gitlab-ci.yml | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 .gitlab-ci.yml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 00000000..afa10ee0
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,55 @@
+.release-rules: &release-rules
+  # rule to run job on a tag-reference which has the form number.number.number (semantic versioning)
+  # or number.number.number-text (semantic versioning + release-name)
+  # and where a Dockerfile exists
+  - if: $CI_COMMIT_TAG =~ /^\d+[.]\d+[.]\d+$/ || $CI_COMMIT_TAG =~ /^\d+[.]\d+[.]\d+[-]\S+$/
+    exists:
+      - Dockerfile
+
+stages:
+  - release
+
+docker-push:
+  # Use the official docker image.
+  image: docker:latest
+  stage: release
+  variables:
+    # the name of the image without version
+    AURA_IMAGE_NAME: "autoradio/steering"
+  services:
+    - docker:dind
+  before_script:
+    # default repo is docker.io (aka docker hub)
+    - docker login -u "$DOCKER_ID" -p "$DOCKER_HUB_AUTH"
+  script:
+    # every commit on main branch should build image as unstable
+    # else it is from a tag (enforced by gitlab-ci rules)
+    # hint: tags are references independent of branches
+    - |
+      if [ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]
+        then docker build -t $AURA_IMAGE_NAME:unstable .
+        else docker build -t $AURA_IMAGE_NAME -t $AURA_IMAGE_NAME:$CI_COMMIT_TAG .
+      fi
+    # TODO: maybe isolate docker build and docker push
+    - docker push "$AURA_IMAGE_NAME" --all-tags
+  rules: 
+    - *release-rules
+    # every commit on master/main branch should trigger a push to docker-hub as unstable without a release
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH 
+      exists:
+        - Dockerfile
+
+release_job:
+  stage: release
+  needs: 
+    - docker-push
+  image: registry.gitlab.com/gitlab-org/release-cli:latest
+  rules: *release-rules
+  script:
+    - echo "this will be a release when there is a tag, but tags should be protected to be only createable by maintainers."
+  release:
+    # TODO: automated release notes
+    name: 'Release $CI_COMMIT_TAG'
+    description: 'Created using the release-cli'
+    tag_name: '$CI_COMMIT_TAG'
+    ref: '$CI_COMMIT_TAG'
-- 
GitLab