From eaa0067879b26b89b45943dd2c8ba835d4e4e2e3 Mon Sep 17 00:00:00 2001
From: Yorick Peterse <yorick@yorickpeterse.com>
Date: Wed, 23 Oct 2019 14:58:50 +0200
Subject: [PATCH] Sync stable branches to CE upon a push

This adds a CI job that runs before all other jobs on a stable branch,
triggering a Merge Train job to sync the stable branch to CE. No checks
are in place for older stable branches, as those simply won't have these
changes.
---
 .gitlab-ci.yml                    |  2 ++
 .gitlab/ci/releases.gitlab-ci.yml | 22 +++++++++++++++++++++
 scripts/sync-stable-branch.sh     | 32 +++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 .gitlab/ci/releases.gitlab-ci.yml
 create mode 100644 scripts/sync-stable-branch.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c1a9d38d5aa1b..36108d04e9c85 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,6 +1,7 @@
 image: "registry.gitlab.com/gitlab-org/gitlab-build-images:ruby-2.6.3-golang-1.11-git-2.22-chrome-73.0-node-12.x-yarn-1.16-postgresql-9.6-graphicsmagick-1.3.33"
 
 stages:
+  - sync
   - prepare
   - quick-test
   - test
@@ -40,3 +41,4 @@ include:
   - local: .gitlab/ci/setup.gitlab-ci.yml
   - local: .gitlab/ci/test-metadata.gitlab-ci.yml
   - local: .gitlab/ci/yaml.gitlab-ci.yml
+  - local: .gitlab/ci/releases.gitlab-ci.yml
diff --git a/.gitlab/ci/releases.gitlab-ci.yml b/.gitlab/ci/releases.gitlab-ci.yml
new file mode 100644
index 0000000000000..1ddc4e90fcfbd
--- /dev/null
+++ b/.gitlab/ci/releases.gitlab-ci.yml
@@ -0,0 +1,22 @@
+---
+
+# Syncs any changes pushed to a stable branch to the corresponding CE stable
+# branch. We run this prior to any tests so that random failures don't prevent a
+# sync.
+sync-stable-branch:
+  # We don't need/want any global before/after commands, so we overwrite these
+  # settings.
+  image: alpine:edge
+  stage: sync
+  # This job should only run on EE stable branches on the canonical GitLab.com
+  # repository.
+  only:
+    variables:
+      - $CI_SERVER_HOST == "gitlab.com"
+    refs:
+      - /^[\d-]+-stable-ee$/@gitlab-org/gitlab
+  before_script:
+    - apk add --no-cache --update curl bash
+  after_script: []
+  script:
+    - bash scripts/sync-stable-branch.sh
diff --git a/scripts/sync-stable-branch.sh b/scripts/sync-stable-branch.sh
new file mode 100644
index 0000000000000..fc62453d74356
--- /dev/null
+++ b/scripts/sync-stable-branch.sh
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+
+# This script triggers a merge train job to sync an EE stable branch to its
+# corresponding CE stable branch.
+
+set -e
+
+if [[ "$MERGE_TRAIN_TRIGGER_TOKEN" == '' ]]
+then
+    echo 'The variable MERGE_TRAIN_TRIGGER_TOKEN must be set to a non-empy value'
+    exit 1
+fi
+
+if [[ "$MERGE_TRAIN_TRIGGER_URL" == '' ]]
+then
+    echo 'The variable MERGE_TRAIN_TRIGGER_URL must be set to a non-empy value'
+    exit 1
+fi
+
+if [[ "$CI_COMMIT_REF_NAME" == '' ]]
+then
+    echo 'The variable CI_COMMIT_REF_NAME must be set to a non-empy value'
+    exit 1
+fi
+
+curl -X POST \
+    -F token="$MERGE_TRAIN_TRIGGER_TOKEN" \
+    -F ref=master \
+    -F "variables[MERGE_FOSS]=1" \
+    -F "variables[SOURCE_BRANCH]=$CI_COMMIT_REF_NAME" \
+    -F "variables[TARGET_BRANCH]=${CI_COMMIT_REF_NAME/-ee/}" \
+    "$MERGE_TRAIN_TRIGGER_URL"
-- 
GitLab