From fd271dd9d86a781b1a01b4e2af8d81e2d55d9b09 Mon Sep 17 00:00:00 2001
From: Alex Iribarren <Alex.Iribarren@cern.ch>
Date: Fri, 19 Feb 2021 12:08:08 +0100
Subject: [PATCH 1/5] Adding convenience functions

---
 .gitlab-ci.yml        |   8 ++-
 functions.sh          | 115 ++++++++++++++++++++++++++++++++++++++++++
 kojicli/Dockerfile    |   1 +
 kojicli/kinit.sh      |   7 ++-
 rpm-ci.yml            |  53 +++++++++----------
 workers/common.docker |   3 ++
 6 files changed, 156 insertions(+), 31 deletions(-)
 create mode 100644 functions.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 8bb7734..d1f8137 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -30,6 +30,8 @@ stages:
       least you can do
       EOF
     - rpmbuild -ba /root/min.spec
+    - echo_green "Everything looks good"
+    - echo_error "Errors would look like this"
 
 .tag_image:
   stage: tag
@@ -49,14 +51,18 @@ stages:
     - docker tag "$CI_REGISTRY_IMAGE/$NAME:$CI_COMMIT_SHORT_SHA" "$CI_REGISTRY_IMAGE/$NAME:$NEWTAG"
     - docker push "$CI_REGISTRY_IMAGE/$NAME"
 
-prepare_workers:
+prepare_images:
   stage: prepare
   script:
+    - cp functions.sh workers/
+    - cp functions.sh kojicli/
     - cd workers
     - make all
   artifacts:
     paths:
       - workers/Dockerfile.*
+      - workers/functions.sh
+      - kojicli/
     expire_in: 1 day
 
 build_kojicli:
diff --git a/functions.sh b/functions.sh
new file mode 100644
index 0000000..5b8462e
--- /dev/null
+++ b/functions.sh
@@ -0,0 +1,115 @@
+#!/bin/bash
+
+## rpmci convenience functions
+
+
+## Pretty colors
+echo_green () {
+    echo -e "\e[32m$*\e[0m"
+}
+
+echo_red () {
+    echo -e "\e[31m$*\e[0m"
+}
+
+echo_bold_green () {
+    echo -e "\e[1m\e[32m$*\e[0m"
+}
+
+echo_bold_red () {
+    echo -e "\e[1m\e[31m$*\e[0m"
+}
+
+echo_error () {
+    echo_bold_red $*
+}
+
+echo_info () {
+    echo_green $*
+}
+
+echo_command () {
+    echo_bold_green $*
+}
+
+# OS checks
+rpmci_is_OS6 () {
+    [[ "$_KOJI_OS" == '6' ]]
+}
+
+rpmci_is_OS7 () {
+    [[ "$_KOJI_OS" == '7' ]]
+}
+
+rpmci_is_OS8 () {
+    [[ "$_KOJI_OS" == '8' ]]
+}
+
+rpmci_is_OS8s () {
+    [[ "$_KOJI_OS" == '8s' ]]
+}
+
+# Get the full name of the configured Koji tag
+get_kojitag () {
+    _KOJITAG_OS="KOJI_TAG_${_KOJI_OS}"
+    echo "${!_KOJITAG_OS:-"${KOJI_TAG}${_KOJI_OS}"}"
+}
+
+# Install the build dependencies specified in the spec file
+rpmci_install_builddeps () {
+    yum-builddep -y *.spec
+}
+
+# Install the RPMs built by Koji
+rpmci_install_koji_rpms () {
+    ARCH=$(rpm --eval "%{_arch}")
+    yum install -y --nogpgcheck `find koji/ -name "*${DIST}.noarch.rpm" -or -name "*${DIST}.$ARCH.rpm"`
+}
+
+# Add a new repository for extra dependencies
+rpmci_add_repo () {
+    NAME="$1"
+    URL="$2"
+    yum-config-manager --add-repo "$URL"
+    FILE=$(ls -tr /etc/yum.repos.d/ | tail -n1)
+    sed -i "s/\[.*\]/[${NAME}]\npriority=1/" /etc/yum.repos.d/${FILE}
+    sed -i "s/name=.*/name=${NAME}/" /etc/yum.repos.d/${FILE}
+
+    DUMP="--dump"
+    rpmci_is_OS7 && DUMP=""
+    FINALURL=$(yum-config-manager ${NAME} $DUMP | grep baseurl | awk '{print $3}')
+
+    STATUS=$(curl -sIL -o /dev/null -w "%{http_code}" "${FINALURL}/repodata/repomd.xml")
+    if [[ "${STATUS}" -ne 200 ]]; then
+        echo_error "Error while trying to reach ${FINALURL}: ${STATUS}"
+        exit 1
+    fi
+
+    echo_info "Added repo ${NAME}: ${FINALURL}"
+}
+
+# Add a Koji tag repository by specifying the full tag name (ie. 'config8-stable')
+rpmci_add_repo_kojitag () {
+    rpmci_add_repo ${1} "http://linuxsoft.cern.ch/internal/repos/${1}/\$basearch/os"
+}
+
+# Add a Koji stable tag repository. With no parameter, it will add the currently-configured Koji tag.
+# With a parameter, it will add the specified Koji tag's stable repo (ie. `rpmci_add_repo_kojitag_stable config8`)
+rpmci_add_repo_kojitag_stable () {
+    TAG=${1:-$(get_kojitag)}
+    rpmci_add_repo_kojitag "${TAG}-stable"
+}
+
+# Add a Koji qa tag repository. With no parameter, it will add the currently-configured Koji tag.
+# With a parameter, it will add the specified Koji tag's qa repo (ie. `rpmci_add_repo_kojitag_stable config8`)
+rpmci_add_repo_kojitag_qa () {
+    TAG=${1:-$(get_kojitag)}
+    rpmci_add_repo_kojitag "${TAG}-qa"
+}
+
+# Add a Koji testing tag repository. With no parameter, it will add the currently-configured Koji tag.
+# With a parameter, it will add the specified Koji tag's testing repo (ie. `rpmci_add_repo_kojitag_stable config8`)
+rpmci_add_repo_kojitag_testing () {
+    TAG=${1:-$(get_kojitag)}
+    rpmci_add_repo_kojitag "${TAG}-testing"
+}
diff --git a/kojicli/Dockerfile b/kojicli/Dockerfile
index 9714e50..742aac7 100644
--- a/kojicli/Dockerfile
+++ b/kojicli/Dockerfile
@@ -7,6 +7,7 @@ RUN dnf install -y epel-release \
   && dnf clean all
 
 COPY koji.conf /etc/
+COPY functions.sh /etc/profile.d/functions.sh
 COPY kinit.sh /etc/profile.d/kinit.sh
 
 ENV BASH_ENV /etc/profile.d/kinit.sh
diff --git a/kojicli/kinit.sh b/kojicli/kinit.sh
index 1844071..507d3a3 100644
--- a/kojicli/kinit.sh
+++ b/kojicli/kinit.sh
@@ -1,10 +1,13 @@
+
+source /etc/profile.d/functions.sh
+
 # Do a kinit if we have the ENV variables set
 if [[ ! -z "${KOJICI_USER}" && ! -z "${KOJICI_PWD}" ]]; then
-    echo -e "\e[32mGetting Kerberos token for ${KOJICI_USER}:\e[0m"
+    echo_info "Getting Kerberos token for ${KOJICI_USER}:"
     echo "${KOJICI_PWD}" | kinit ${KOJICI_USER}@CERN.CH
     RET=$?
     if [[ $RET -ne 0 ]]; then
-        echo -e "\e[1m\e[31mError while running kinit\e[0m"
+        echo_error "Error while running kinit"
         exit $RET
     fi
 fi
diff --git a/rpm-ci.yml b/rpm-ci.yml
index de3679c..07bff16 100644
--- a/rpm-ci.yml
+++ b/rpm-ci.yml
@@ -49,7 +49,7 @@ stages:
     if [[ "$ADD_VCS_TAG" == 'True' && $_KOJI_OS != 6 ]]; then
       for SPEC in *.spec; do
         if ! grep -q "VCS:" "$SPEC"; then
-          echo -e "\e[32mAdding VCS tag to spec file: ${_VCS_TAG}\e[0m"
+          echo_info "Adding VCS tag to spec file: ${_VCS_TAG}"
           sed -i "1iVCS: ${_VCS_TAG}" "$SPEC"
         fi
       done
@@ -59,7 +59,7 @@ stages:
   image: $IMAGE
   interruptible: true
   before_script:
-    - yum-builddep -y *.spec
+    - rpmci_install_builddeps
   dependencies: []
 
 .build_srpm:
@@ -72,7 +72,7 @@ stages:
     - COUNT=`ls -l build/SRPMS/*${DIST}.src.rpm 2>/dev/null | wc -l`
     - |
       if [[ $COUNT -eq 0 ]]; then
-        echo -e "\e[1m\e[31mNo source RPMs found in build/SRPMS/, please check your Makefile and your .spec\e[0m"
+        echo_error "No source RPMs found in build/SRPMS/, please check your Makefile and your .spec"
         exit 1
       fi
   artifacts:
@@ -90,7 +90,7 @@ stages:
     - make -f $MAKEFILE rpm
     - |
       if [[ ! -d build/RPMS/ ]]; then
-        echo -e "\e[1m\e[31mbuild/SRPMS/ not found, please check your Makefile and your .spec\e[0m"
+        echo_error "build/RPMS/ not found, please check your Makefile and your .spec"
         exit 1
       fi
   artifacts:
@@ -105,10 +105,10 @@ stages:
   script:
     - |
       if [[ -e .rpmlint ]]; then
-        echo -e "\e[32mUsing custom rpmlint configuration from .rpmlint\e[0m"
+        echo_info "Using custom rpmlint configuration from .rpmlint"
         CONFIG="-f .rpmlint"
       else
-        echo -e "\e[32mUsing default rpmlint configuration. Create a .rpmlint file to override settings\e[0m"
+        echo_info "Using default rpmlint configuration. Create a .rpmlint file to override settings"
       fi
     - rpmlint -i $CONFIG *.spec
     - rpmlint -i $CONFIG build/
@@ -118,9 +118,7 @@ stages:
   stage: test
   interruptible: true
   before_script:
-    - export _KOJITAG_OS="KOJI_TAG_${_KOJI_OS}"
-    - export _KOJITAG=${!_KOJITAG_OS:-"${KOJI_TAG}${_KOJI_OS}"}
-    - yum install -y --nogpgcheck `ls koji/*${DIST}.noarch.rpm koji/*${DIST}.x86_64.rpm`
+    - rpmci_install_koji_rpms
   script:
     - echo "Test me!"
 
@@ -130,17 +128,16 @@ stages:
   before_script:
     - |
       if [[ -z "$KOJICI_USER" ]]; then
-        echo -e "\e[1m\e[31mVariable KOJICI_USER not defined\e[0m"
+        echo_error "Variable KOJICI_USER not defined"
         exit 1
       fi
     - |
       if [[ -z "$KOJICI_PWD" ]]; then
-        echo -e "\e[1m\e[31mVariable KOJICI_PWD not defined\e[0m"
+        echo_error "Variable KOJICI_PWD not defined"
         exit 1
       fi
     - mkdir koji
-    - export _KOJITAG_OS="KOJI_TAG_${_KOJI_OS}"
-    - export _KOJITAG=${!_KOJITAG_OS:-"${KOJI_TAG}${_KOJI_OS}"}
+    - export _KOJITAG=$(get_kojitag)
   dependencies: []
   variables:
     GIT_STRATEGY: none
@@ -152,20 +149,20 @@ stages:
     - |
       for SRPM in build/SRPMS/*${DIST}.src.rpm; do
         export BUILDNAME=$(rpm -qp --qf "%{n}-%{v}-%{r}\n" ${SRPM})
-        echo -e "\e[32mBuild name of ${SRPM} is \"${BUILDNAME}\"\e[0m"
-        echo -e "\e[1m\e[32mkoji -p ${_KOJI_PROFILE} build --wait --scratch ${_KOJITAG} ${SRPM}\e[0m"
+        echo_info "Build name of ${SRPM} is \"${BUILDNAME}\""
+        echo_command "koji -p ${_KOJI_PROFILE} build --wait --scratch ${_KOJITAG} ${SRPM}"
         koji -p ${_KOJI_PROFILE} build --wait --scratch ${_KOJITAG} ${SRPM} | tee taskid
         export TASKID=$(sed -z -e 's/.*Created task:\ \([0-9]\+\).*/\1/g' taskid)
         cd koji
-        echo -e "\e[1m\e[32mkoji -p ${_KOJI_PROFILE} download-task --noprogress $TASKID\e[0m"
+        echo_command "koji -p ${_KOJI_PROFILE} download-task --noprogress $TASKID"
         koji -p ${_KOJI_PROFILE} download-task --noprogress $TASKID
         cd ~-
         export LOCAL_VERSION=$(rpm -qp --qf "%{v}-%{r}\n" ${SRPM})
         export KOJI_VERSION=$(rpm -qp --qf "%{v}-%{r}\n" koji/${BUILDNAME%${DIST}}*.src.rpm)
-        echo -e "\e[32mLocal version is ${LOCAL_VERSION}, Koji version is ${KOJI_VERSION}\e[0m"
+        echo_info "Local version is \"${LOCAL_VERSION}\", Koji version is \"${KOJI_VERSION}\"."
         if [[ "$KOJI_VERSION" != "$LOCAL_VERSION" ]]; then
-          echo -e "\e[1m\e[31mThe version created locally does not match the one created by Koji.\e[0m"
-          echo -e "\e[1m\e[31mMake sure you specify the DIST_* variable correctly in your CI configuration.\e[0m"
+          echo_error "The version created locally does not match the one created by Koji."
+          echo_error "Make sure you specify the DIST_* variable correctly in your CI configuration."
           exit 1
         fi
       done
@@ -187,22 +184,22 @@ stages:
         export PKGNAME=$(rpm -qp --qf "%{n}\n" ${SRPM})
         PKGEXISTS=$(koji -p ${_KOJI_PROFILE} list-pkgs --quiet --noinherit --tag="${_KOJITAG}-testing" --package="${PKGNAME}" > /dev/null 2>&1; echo $?)
         if [ $PKGEXISTS -eq 1 ]; then
-          echo -e "\e[1m\e[31mPackage ${PKGNAME} has not been added to the tag ${_KOJITAG}.\e[0m"
+          echo_error "Package ${PKGNAME} has not been added to the tag ${_KOJITAG}."
           echo "You can add it by running something like this:"
           echo "  for i in testing qa stable; do koji add-pkg --owner=$KOJICI_USER ${_KOJITAG}-\$i ${PKGNAME}; done"
           exit 1
         fi
         export BUILDNAME=$(rpm -qp --qf "%{n}-%{v}-%{r}\n" ${SRPM})
-        echo -e "\e[32mBuild name of ${SRPM} is \"${BUILDNAME}\"\e[0m"
+        echo_info "Build name of ${SRPM} is \"${BUILDNAME}\""
         BUILDEXISTS=$(koji -p ${_KOJI_PROFILE} list-builds --quiet --buildid="${BUILDNAME}" 2>&1 | grep -q COMPLETE; echo $?)
         if [ $BUILDEXISTS -eq 0 ]; then
-          echo -e "\e[1m\e[31mBuild ${BUILDNAME} already exists in Koji, please update the version number\e[0m"
+          echo_error "Build ${BUILDNAME} already exists in Koji, please update the version number."
           exit 1
         fi
-        echo -e "\e[1m\e[32mkoji -p ${_KOJI_PROFILE} build --wait ${_KOJITAG} ${SRPM}\e[0m"
+        echo_command "koji -p ${_KOJI_PROFILE} build --wait ${_KOJITAG} ${SRPM}"
         koji -p ${_KOJI_PROFILE} build --wait ${_KOJITAG} ${SRPM}
         cd koji
-        echo -e "\e[1m\e[32mkoji -p ${_KOJI_PROFILE} download-build --noprogress --debuginfo ${BUILDNAME}\e[0m"
+        echo_command "koji -p ${_KOJI_PROFILE} download-build --noprogress --debuginfo ${BUILDNAME}"
         koji -p ${_KOJI_PROFILE} download-build --noprogress --debuginfo ${BUILDNAME}
         cd ~-
       done
@@ -223,8 +220,8 @@ stages:
     - |
       for SRPM in build/SRPMS/*${DIST}.src.rpm; do
         export BUILDNAME=$(rpm -qp --qf "%{n}-%{v}-%{r}\n" ${SRPM})
-        echo -e "\e[32mBuild name of ${SRPM} is \"${BUILDNAME}\"\e[0m"
-        echo -e "\e[1m\e[32mkoji -p ${_KOJI_PROFILE} tag-build ${_KOJITAG}-qa ${BUILDNAME}\e[0m"
+        echo_info "Build name of ${SRPM} is \"${BUILDNAME}\"."
+        echo_command "koji -p ${_KOJI_PROFILE} tag-build ${_KOJITAG}-qa ${BUILDNAME}"
         koji -p ${_KOJI_PROFILE} tag-build ${_KOJITAG}-qa ${BUILDNAME}
       done
   allow_failure: false
@@ -240,7 +237,7 @@ stages:
     - |
       for SRPM in build/SRPMS/*${DIST}.src.rpm; do
         export BUILDNAME=$(rpm -qp --qf "%{n}-%{v}-%{r}\n" ${SRPM})
-        echo -e "\e[32mBuild name of ${SRPM} is \"${BUILDNAME}\"\e[0m"
-        echo -e "\e[1m\e[32mkoji -p ${_KOJI_PROFILE} tag-build ${_KOJITAG}-stable ${BUILDNAME}\e[0m"
+        echo_info "Build name of ${SRPM} is \"${BUILDNAME}\"."
+        echo_command "koji -p ${_KOJI_PROFILE} tag-build ${_KOJITAG}-stable ${BUILDNAME}"
         koji -p ${_KOJI_PROFILE} tag-build ${_KOJITAG}-stable ${BUILDNAME}
       done
diff --git a/workers/common.docker b/workers/common.docker
index cc4dd9f..5ac208a 100644
--- a/workers/common.docker
+++ b/workers/common.docker
@@ -21,4 +21,7 @@ RUN yum update -y \
   && yum clean all \
   && rm -rf /var/cache/yum
 
+COPY functions.sh /etc/profile.d/functions.sh
+ENV BASH_ENV /etc/profile.d/functions.sh
+
 ENTRYPOINT /bin/bash
-- 
GitLab


From 9dc529856e0599c6c969486c1778e4e474bb04a2 Mon Sep 17 00:00:00 2001
From: Alex Iribarren <Alex.Iribarren@cern.ch>
Date: Mon, 22 Feb 2021 11:40:35 +0100
Subject: [PATCH 2/5] Fix service-now links

---
 README.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 3623efe..ae96ea0 100644
--- a/README.md
+++ b/README.md
@@ -5,8 +5,8 @@ This repo exists to help you build RPMs using Koji and Gitlab's CI infrastructur
 
 ## Prerequirements:
 
-1. A [Koji tag](https://cern.service-now.com/service-portal/report-ticket.do?name=Koji-tag&fe=IT-RPM) where your packages will end up.
-2. A [service account](https://account.cern.ch/account/Management/NewAccount.aspx) with [access to Koji](https://cern.service-now.com/service-portal/report-ticket.do?name=request&fe=IT-RPM), and in particular to your Koji tag. We'll use the account `kojici` as an example, but you must have your own. This account can be used for as many RPMs as you want.
+1. A [Koji tag](https://cern.service-now.com/service-portal?id=sc_cat_item&name=Koji-tag&fe=IT-RPM) where your packages will end up.
+2. A [service account](https://account.cern.ch/account/Management/NewAccount.aspx) with [access to Koji](https://cern.service-now.com/service-portal?id=sc_cat_item&name=request&fe=IT-RPM), and in particular to your Koji tag. We'll use the account `kojici` as an example, but you must have your own. This account can be used for as many RPMs as you want.
 3. A [Gitlab](https://gitlab.cern.ch/) repository with Pipelines (ie. continuous integration) enabled. You can enable Pipelines by going to *General Settings* for your repo and toggling the *Pipelines* feature under *Visibility, project features, permissions*. More info in [KB0003690](https://cern.service-now.com/service-portal/article.do?n=KB0003690).
 4. Source code with a `Makefile` (or a `Makefile.koji`, which will take precedence if both exist) that defines two mandatory targets, `srpm` and `rpm`. Those targets should build at least one source RPM and an RPM, respectively. Check our [RPM starter](https://gitlab.cern.ch/linuxsupport/myrpm) repo for a working example.
 
@@ -351,4 +351,4 @@ stop adding this tag altogether by setting the variable `ADD_VCS_TAG` to `False`
 ## Getting Help
 
 If you run into issues on how to use RPMCI, you can contact us on the [Linux Mattermost channel](https://mattermost.web.cern.ch/it-dep/channels/linux)
-or by opening a [ServiceNOW ticket](https://cern.service-now.com/service-portal/report-ticket.do?name=incident&fe=IT-RPM).
+or by opening a [ServiceNOW ticket](https://cern.service-now.com/service-portal?id=sc_cat_item&name=incident&fe=IT-RPM).
-- 
GitLab


From 4f25e9b3431a77b6e4e1f697655a74ec1b0f5bd2 Mon Sep 17 00:00:00 2001
From: Alex Iribarren <Alex.Iribarren@cern.ch>
Date: Mon, 22 Feb 2021 12:41:32 +0100
Subject: [PATCH 3/5] Fix KB link

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index ae96ea0..eda9248 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@ This repo exists to help you build RPMs using Koji and Gitlab's CI infrastructur
 
 1. A [Koji tag](https://cern.service-now.com/service-portal?id=sc_cat_item&name=Koji-tag&fe=IT-RPM) where your packages will end up.
 2. A [service account](https://account.cern.ch/account/Management/NewAccount.aspx) with [access to Koji](https://cern.service-now.com/service-portal?id=sc_cat_item&name=request&fe=IT-RPM), and in particular to your Koji tag. We'll use the account `kojici` as an example, but you must have your own. This account can be used for as many RPMs as you want.
-3. A [Gitlab](https://gitlab.cern.ch/) repository with Pipelines (ie. continuous integration) enabled. You can enable Pipelines by going to *General Settings* for your repo and toggling the *Pipelines* feature under *Visibility, project features, permissions*. More info in [KB0003690](https://cern.service-now.com/service-portal/article.do?n=KB0003690).
+3. A [Gitlab](https://gitlab.cern.ch/) repository with Pipelines (ie. continuous integration) enabled. You can enable Pipelines by going to *General Settings* for your repo and toggling the *Pipelines* feature under *Visibility, project features, permissions*. More info in [KB0003690](https://cern.service-now.com/service-portal?id=kb_article&n=KB0003690).
 4. Source code with a `Makefile` (or a `Makefile.koji`, which will take precedence if both exist) that defines two mandatory targets, `srpm` and `rpm`. Those targets should build at least one source RPM and an RPM, respectively. Check our [RPM starter](https://gitlab.cern.ch/linuxsupport/myrpm) repo for a working example.
 
 Note: RPMCI uses [interruptible](https://docs.gitlab.com/ee/ci/yaml/#interruptible) jobs in order to stop the execution of a pipeline if a newer pipeline has made it redundant. In order to take advantage of this feature, [Auto-cancel pending pipelines](https://docs.gitlab.com/ee/ci/pipelines/settings.html#auto-cancel-pending-pipelines) has to be enabled for your project.
-- 
GitLab


From 633735a8b148a18e66e12c5f501e1ebfbd7add32 Mon Sep 17 00:00:00 2001
From: Alex Iribarren <Alex.Iribarren@cern.ch>
Date: Mon, 22 Feb 2021 15:00:39 +0100
Subject: [PATCH 4/5] Add docs

---
 README.md    | 64 +++++++++++++++++++++++++++++++++-------------------
 functions.sh |  3 ++-
 2 files changed, 43 insertions(+), 24 deletions(-)

diff --git a/README.md b/README.md
index eda9248..34648f7 100644
--- a/README.md
+++ b/README.md
@@ -63,6 +63,29 @@ variables, but you will have to specify the OS release yourself (ie. `KOJI_TAG_8
   BUILD_6: 'True'
 ```
 
+## Convenience Functions
+
+In order to simplify common operations, RPMCI includes a series of [convenience functions](functions.sh)
+which are built into the builder (and kojicli) docker images. These functions can
+be executed from your gitlab-ci scripts and allow you to easily perform common
+operations such as adding your Koji tag as a repo without having to worry about internal
+RPMCI variables and details. This also allows you to future-proof your scripts in case
+new versions of RPMCI change implementation details.
+
+Here is a partial list of the available functions, please check [functions.sh] for the full list:
+
+* `rpmci_add_repo_kojitag_stable`: add your stable Koji tag as a repo. If you add a parameter
+  (ie: `rpmci_add_repo_kojitag_stable config8`), then it adds the stable repo for the specified tag.
+* `rpmci_add_repo <name> <url>`: add a custom repo.
+* `rpmci_is_OS7`, `rpmci_is_OS8`, etc: test the OS a particular job is running on.
+* `rpmci_install_builddeps`: install the build dependencies specified in the spec file.
+* `rpmci_install_koji_rpms`: install the RPMs built by Koji (for the testing stages).
+* `get_kojitag`: get your full configured Koji tag name in a running job.
+* `echo_error`: print a message in bold red.
+* `echo_info`: print a message in green.
+* `echo_command`: print a message in bold green.
+
+
 ## Customizations
 
 ### Adding your own tests
@@ -98,22 +121,20 @@ Here are a few examples:
 .test_install:
   before_script:
     - yum install -y --nogpgcheck --enablerepo=cernonly oracle-instantclient-tnsnames.ora
-    - yum install -y --nogpgcheck `ls koji/*${DIST}.noarch.rpm koji/*${DIST}.x86_64.rpm`
+    - rpmci_install_koji_rpms
 ```
 
 ### Adding your own Koji tag's repo (for extra dependencies)
 
-This makes use of the `KOJI_TAG` variable, or of the `KOJI_TAG_x` variables, should they
-be defined. The first two lines of the script should be included without modifications.
+This makes use of the `rpmci_add_repo_kojitag_stable` convenience function, which
+will add your Koji tag's stable repo. You will still need to install your Koji-built
+RPMs, and for that you can use the `rpmci_install_koji_rpms` function.
 
 ```yaml
 .test_install:
   before_script:
-    - export _KOJITAG_OS="KOJI_TAG_${_KOJI_OS}"
-    - export _KOJITAG=${!_KOJITAG_OS:-"${KOJI_TAG}${_KOJI_OS}"}
-    - yum-config-manager --add-repo "http://linuxsoft.cern.ch/internal/repos/${_KOJITAG}-stable/\$basearch/os"
-    - sed -i "s/\[.*\]/[${_KOJITAG}]\npriority=1/" /etc/yum.repos.d/linuxsoft.cern.ch*${_KOJITAG}*.repo
-    - yum install -y --nogpgcheck `ls koji/*${DIST}.noarch.rpm koji/*${DIST}.x86_64.rpm`
+    - rpmci_add_repo_kojitag_stable
+    - rpmci_install_koji_rpms
 ```
 
 ### New job with extra tests
@@ -131,7 +152,7 @@ test_nose6:
   extends: test_install6
   before_script:
     - yum install -y python2-future
-    - yum install -y --nogpgcheck `ls koji/*${DIST}.noarch.rpm koji/*${DIST}.x86_64.rpm`
+    - rpmci_install_koji_rpms
   script:
     # These deps are only needed to run the unit tests
     - yum install --nogpgcheck -y python-nose python2-mock python-suds
@@ -183,11 +204,8 @@ such as from your Koji tag, you can add them for all OSes as follows:
 ```yaml
 .rpm_deps:
   before_script:
-    - export _KOJITAG_OS="KOJI_TAG_${_KOJI_OS}"
-    - export _KOJITAG=${!_KOJITAG_OS:-"${KOJI_TAG}${_KOJI_OS}"}
-    - yum-config-manager --add-repo "http://linuxsoft.cern.ch/internal/repos/${_KOJITAG}-stable/\$basearch/os"
-    - sed -i "s/\[.*\]/[${_KOJITAG}]\npriority=1/" /etc/yum.repos.d/linuxsoft.cern.ch*${_KOJITAG}*.repo
-    - yum-builddep -y *.spec
+    - rpmci_add_repo_kojitag_stable
+    - rpmci_install_builddeps
 ```
 
 You can also add different dependencies per OS, like this:
@@ -195,29 +213,29 @@ You can also add different dependencies per OS, like this:
 ```yaml
 .rpm_deps8:
   before_script:
-    - yum-config-manager --add-repo "http://linuxsoft.cern.ch/internal/repos/dbclients8-stable/\$basearch/os/"
+    - rpmci_add_repo_kojitag_stable "dbclients8"
     - yum install -y oracle-instantclient
-    - yum-builddep -y *.spec
+    - rpmci_install_builddeps
 
 .rpm_deps7:
   before_script:
     - yum install -y --enablerepo=cernonly oracle-instantclient
-    - yum-builddep -y *.spec
+    - rpmci_install_builddeps
 ```
 
-You may also perform conditional actions depending on the OS by using the `$_KOJI_OS` variable:
+You may also perform conditional actions depending on the OS by using the `rpmci_is_*` convenience functions:
 
 ```yaml
 .rpm_deps:
   before_script:
     - |
-      if [[ "$_KOJI_OS" -eq 8 ]]; then
-        yum-config-manager --add-repo "http://linuxsoft.cern.ch/internal/repos/dbclients8-stable/\$basearch/os/"
+      if rpmci_is_OS8; then
+        rpmci_add_repo_kojitag "dbclients8-stable"
       fi
-    - yum-builddep -y *.spec
+    - rpmci_install_builddeps
 ```
 
-Don't forget to include the `yum-builddep -y *.spec` line in your `before_script`, otherwise
+Don't forget to include the `rpmci_install_builddeps` line in your `before_script`, otherwise
 no dependencies will actually be installed!
 
 ### Adding your own CI stages
@@ -290,7 +308,7 @@ One solution to this issue is to use `rh-git29` software collection. To do that,
     - export LD_LIBRARY_PATH=/opt/rh/httpd24/root/usr/lib64:$LD_LIBRARY_PATH
     - export MANPATH=/opt/rh/rh-git29/root/usr/share/man:$MANPATH
     - export PERL5LIB=/opt/rh/rh-git29/root/usr/share/perl5/vendor_perl
-    - yum-builddep -y *.spec
+    - rpmci_install_builddeps
 ```
 
 This could be simplified by calling `source /bin/scl_source enable rh-git29`, but unfortunately it seems to fail.
diff --git a/functions.sh b/functions.sh
index 5b8462e..868a9f4 100644
--- a/functions.sh
+++ b/functions.sh
@@ -66,7 +66,8 @@ rpmci_install_koji_rpms () {
     yum install -y --nogpgcheck `find koji/ -name "*${DIST}.noarch.rpm" -or -name "*${DIST}.$ARCH.rpm"`
 }
 
-# Add a new repository for extra dependencies
+# Add a new repository for extra dependencies.
+# This function takes two parameters, the name of the repo and it's URL.
 rpmci_add_repo () {
     NAME="$1"
     URL="$2"
-- 
GitLab


From 9c14bd324e019c5debaa738c48a885c5cba588d6 Mon Sep 17 00:00:00 2001
From: Alex Iribarren <Alex.Iribarren@cern.ch>
Date: Mon, 22 Feb 2021 15:25:34 +0100
Subject: [PATCH 5/5] Show backwards compatibility case

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 34648f7..280090b 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,7 @@ variables, but you will have to specify the OS release yourself (ie. `KOJI_TAG_8
 
 ```yaml
   KOJI_TAG: 'mytag'
-  KOJI_TAG_8: 'mynewtag8'
+  KOJI_TAG_6: 'myoldtag6'
   BUILD_8: 'True'
   BUILD_7: 'True'
   BUILD_6: 'True'
-- 
GitLab