Add Gitlab Templates for Docker, RTD, and GS 88/72488/1
authorTrevor Bramwell <tbramwell@linuxfoundation.org>
Fri, 7 May 2021 20:34:02 +0000 (13:34 -0700)
committerTrevor Bramwell <tbramwell@linuxfoundation.org>
Fri, 7 May 2021 21:31:28 +0000 (14:31 -0700)
Adds template for Gitlab-CI that projects can include to build
documentation, containers, and upload artifacts to Google Storage.

Change-Id: Ibc3cc75a3717f11357417f787900a31646ef84aa
Signed-off-by: Trevor Bramwell <tbramwell@linuxfoundation.org>
gitlab-templates/Docker.gitlab-ci.yml [new file with mode: 0644]
gitlab-templates/GoogleStorage.gitlab-ci.yml [new file with mode: 0644]
gitlab-templates/RTD.gitlab-ci.yml [new file with mode: 0644]

diff --git a/gitlab-templates/Docker.gitlab-ci.yml b/gitlab-templates/Docker.gitlab-ci.yml
new file mode 100644 (file)
index 0000000..8acb5a0
--- /dev/null
@@ -0,0 +1,70 @@
+# Build and push a Docker image with CI/CD.
+# Docker-in-Docker documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
+#
+# By default builds are tagged with their branch name and pushed to the
+# Gitlab Docker Registry. If DOCKER_TAG_LATEST is set to true, builds on
+# the $DOCKER_LATEST_BRANCH are also tagged and pushed as ":latest"
+#
+# Scheduled builds can be enabled on a Gitlab schedule by specifying
+# DOCKER_SCHEDULE = "true" in variables
+---
+variables:
+  # Docker registry where images will be pushed
+  DOCKER_REGISTRY: "$CI_REGISTRY"
+  DOCKER_USERNAME: "$CI_REGISTRY_USER"
+  DOCKER_TOKEN: "$CI_REGISTRY_PASSWORD"
+  # Whether or to push images after they're built
+  DOCKER_PUSH: "true"
+  # TODO: Conditionally include '--file' to docker build to reduce need
+  # to always define FILEPATH when BUILDCONTEXT is set
+  DOCKER_FILEPATH: "Dockerfile"
+  DOCKER_BUILDCONTEXT: "."
+  DOCKER_IMAGE: "$CI_REGISTRY_IMAGE"
+  # If LATEST_TAG is set to true, builds on the $DOCKER_LATEST_BRANCH
+  #   will be tagged and pushed with ":latest"
+  DOCKER_LATEST_TAG: "true"
+  DOCKER_LATEST_BRANCH: "$CI_DEFAULT_BRANCH"
+
+.docker-build-and-push: &docker-build-and-push
+  image: docker:latest
+  stage: deploy
+  interruptible: true
+  services:
+    - docker:dind
+  before_script:
+    - docker login -u "$DOCKER_USERNAME" -p "$DOCKER_TOKEN" $DOCKER_REGISTRY
+  script:
+    # Warm the cache by fetching the latest image. There's no guarantee
+    # the image will already exist on the runner.
+    - docker pull "$DOCKER_IMAGE:${CI_COMMIT_REF_SLUG}" || true
+    - >
+        docker build
+        --pull
+        --cache-from "$DOCKER_IMAGE:${CI_COMMIT_REF_SLUG}"
+        --file "$DOCKER_FILEPATH"
+        --tag "$DOCKER_IMAGE:${CI_COMMIT_REF_SLUG}"
+        $DOCKER_BUILDCONTEXT
+    - |
+      if [[ "$CI_COMMIT_BRANCH" == "$DOCKER_LATEST_BRANCH" && "$DOCKER_LATEST_TAG" == "true" ]]; then
+        docker tag "$DOCKER_IMAGE:${CI_COMMIT_REF_SLUG}" "$DOCKER_IMAGE"
+      fi
+    - |
+      # Push docker images if DOCKER_PUSH is set
+      if [[ "$DOCKER_PUSH" == "true" ]]; then
+        docker push "$DOCKER_IMAGE:${CI_COMMIT_REF_SLUG}"
+        # Push ':latest' if LATEST_TAG is true
+        if [[ "$CI_COMMIT_BRANCH" == "$DOCKER_LATEST_BRANCH" && "$DOCKER_LATEST_TAG" == "true" ]]; then
+          docker push "$DOCKER_IMAGE"
+        fi
+      fi
+  rules:
+    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
+      when: never
+    # Gitlab does not have a way of specifying which jobs are scheduled,
+    # so an extra variable is needed in order to signify docker build
+    # should be picked up by the schedule run.
+    - if: $CI_PIPELINE_SOURCE == "schedule" && $DOCKER_SCHEDULE != "true"
+      when: never
+    - if: '$CI_COMMIT_BRANCH == $DOCKER_LATEST_BRANCH'
+    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
+    - if: $CI_COMMIT_TAG
diff --git a/gitlab-templates/GoogleStorage.gitlab-ci.yml b/gitlab-templates/GoogleStorage.gitlab-ci.yml
new file mode 100644 (file)
index 0000000..be941a8
--- /dev/null
@@ -0,0 +1,30 @@
+# Template for uploading artifacts to Google Storage
+#
+# To upload artifacts to Google Storage, include this file in your
+# .gitlab-ci.yml file with the following stanza:
+#
+#   include:
+#     - project: anuket/releng
+#       file: '/gitlab-templates/GoogleStorage.gitlab-ci.yml'
+#
+# And append the following "- !reference.." line to the script portion
+# of a job where artifacts should be uploaded:
+#
+#   script:
+#      ...
+#      - !reference [.gsutil-install, script]
+#      ...
+#
+# After the script has been included `gsutil` will have access to the
+# necessary Google Storage bucket.
+---
+variables:
+  GS_URL: "artifacts.opnfv.org/$CI_PROJECT_NAME"
+  WORKSPACE: $CI_PROJECT_DIR
+
+.gsutil-install: &gsutil-install
+  script:
+    - dnf -y install python3-pip
+    - python3 -m pip install -U pip
+    - python3 -m pip install gsutil
+    - echo "$GSUTIL_CONFIG" > ~/.boto
diff --git a/gitlab-templates/RTD.gitlab-ci.yml b/gitlab-templates/RTD.gitlab-ci.yml
new file mode 100644 (file)
index 0000000..5b838d2
--- /dev/null
@@ -0,0 +1,95 @@
+# ReadTheDocs Workflow
+#
+# This workflow adds these builds to projects:
+#
+#   docs-build:
+#       Generate a html sphinx-build from the $DOCS_DIRECTORY
+#
+#   docs-link-check:
+#       Run a non-blocking sphinx-build linkcheck against
+#       the $DOCS_DIRECTORY
+#
+#   pages:
+#       Serve the built documentation as the Gitlab Pages site for
+#       the project
+#
+# Both docs-build and docs-link-check run on merge requests and merges
+# to the default branch that modify files under the $DOCS_DIRECTORY,
+# while pages only run on merges.
+#
+# Scheduled builds can be enabled when creating a schedule job and
+# specifying DOCS_SCHEDULE = "true" in build variables
+---
+variables:
+  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
+  DOCS_DIRECTORY: "docs"
+
+.docs-cache: &docs-cache
+  paths:
+    - .cache/pip
+    - venv/
+
+.docs-before-script: &docs-before-script
+  - python -V
+  - pip install virtualenv
+  - virtualenv venv
+  - source venv/bin/activate
+  - pip install Sphinx
+  - |
+    if [ -f "$DOCS_DIRECTORY/requirements.txt" ]; then
+      pip install -r "$DOCS_DIRECTORY/requirements.txt"
+    fi
+
+docs-build:
+  stage: build
+  image: python:3
+  before_script:
+    - *docs-before-script
+  script: |
+     sphinx-build -T -b html -D language=en $DOCS_DIRECTORY _build/html
+  cache: *docs-cache
+  artifacts:
+    paths:
+      - _build/html
+  rules:
+    - if: $CI_PIPELINE_SOURCE == "schedule" && $DOCS_SCHEDULE != "true"
+      when: never
+    - if: $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      changes:
+        - $DOCS_DIRECTORY/*
+
+docs-link-check:
+  stage: test
+  allow_failure: true
+  needs: []
+  image: python:3
+  before_script:
+    - *docs-before-script
+  script: |
+      sphinx-build -T -b linkcheck $DOCS_DIRECTORY _build/linkcheck
+  cache: *docs-cache
+  artifacts:
+    paths:
+      - _build/linkcheck
+  rules:
+    - if: $CI_PIPELINE_SOURCE == "schedule" && $DOCS_SCHEDULE != "true"
+      when: never
+    - if: $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      changes:
+        - $DOCS_DIRECTORY/*
+
+pages:
+  stage: deploy
+  image: python:3
+  script: |
+      mkdir public
+      mv _build/html/* public/
+  artifacts:
+    paths:
+      - public
+  rules:
+    - if: $CI_PIPELINE_SOURCE == "schedule"
+      when: never
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      changes:
+        - $DOCS_DIRECTORY/*