From 4a478c4096e4466725768cd3184fb6a33ad8fcb0 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Tue, 24 Sep 2019 17:48:12 +0100
Subject: [PATCH] ci: Add support for additional apt sources

In addition to standard sources.list(5) syntax, this supports a shorthand
"both ..." for adding similar "deb ..." and "deb-src ..." lines.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 ci/Jenkinsfile       | 26 +++++++++++++++++++++-----
 debian/gitlab-ci.yml | 12 ++++++++++++
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/ci/Jenkinsfile b/ci/Jenkinsfile
index 8b9153c1d..38d45b50d 100644
--- a/ci/Jenkinsfile
+++ b/ci/Jenkinsfile
@@ -73,6 +73,10 @@ pipeline {
             env.CI_APT_SOURCES_FILE = ''
           }
 
+          if (!env.CI_EXTRA_APT_SOURCES) {
+            env.CI_EXTRA_APT_SOURCES = ''
+          }
+
           checkout changelog: true, poll: true, scm: [
             $class: 'GitSCM',
             branches: [[name: "origin/${env.CI_PRESSURE_VESSEL_GIT_BRANCH}"]],
@@ -94,14 +98,26 @@ pipeline {
           '''
         }
 
-        sh """
-        sed -e 's!@BASE_IMAGE@!${env.CI_DOCKER_REGISTRY}/${env.CI_DOCKER_IMAGE}!g' < src/ci/Dockerfile.in > src/ci/Dockerfile
-        if [ -n "${env.CI_APT_SOURCES_FILE}" ]; then
-          cp "${env.CI_APT_SOURCES_FILE}" src/ci/sources.list
+        sh '''
+        set -eu
+
+        sed -e 's!@BASE_IMAGE@!'"${CI_DOCKER_REGISTRY}/${CI_DOCKER_IMAGE}"'!g' < src/ci/Dockerfile.in > src/ci/Dockerfile
+
+        if [ -n "${CI_APT_SOURCES_FILE}" ]; then
+          cp "${CI_APT_SOURCES_FILE}" src/ci/sources.list
         else
           touch src/ci/sources.list
         fi
-        """
+
+        echo "${CI_EXTRA_APT_SOURCES}" | while read -r first rest; do
+          if [ "x$first" = xboth ]; then
+            echo "deb $rest" >> src/ci/sources.list
+            echo "deb-src $rest" >> src/ci/sources.list
+          else
+            echo "$first $rest" >> src/ci/sources.list
+          fi
+        done
+        '''
 
         script {
           docker.withRegistry("https://${env.CI_DOCKER_REGISTRY}", dockerRegistryCred) {
diff --git a/debian/gitlab-ci.yml b/debian/gitlab-ci.yml
index 73e724459..5e2959126 100644
--- a/debian/gitlab-ci.yml
+++ b/debian/gitlab-ci.yml
@@ -58,6 +58,18 @@ build:scout:
 
             if [ -n "${SCOUT_APT_SOURCES_FILE}" ]; then
                 cp "${SCOUT_APT_SOURCES_FILE}" /etc/apt/sources.list
+
+                if [ -n "${CI_EXTRA_APT_SOURCES-}" ]; then
+                  echo "${CI_EXTRA_APT_SOURCES}" | while read -r first rest; do
+                    if [ "x$first" = xboth ]; then
+                      echo "deb $rest" >> /etc/apt/sources.list
+                      echo "deb-src $rest" >> /etc/apt/sources.list
+                    else
+                      echo "$first $rest" >> /etc/apt/sources.list
+                    fi
+                  done
+                fi
+
                 apt-get -y update
             fi
 
-- 
GitLab