GitLab 15.7 的 Web IDE 尚無 File templates 功能

前言

根據 GitLab 原廠的消息《A first look at the new GitLab Web IDE and remote development experience》,在 GitLab 15.7 開始 Beta 測試基於 Visual Studio Code (VS Code) 的 Web IDE。

由於 gitlab.com 會自動隨著版本升級,因此目前使用 gitlab.com 的讀者,在閱讀《和艦長一起30天玩轉GitLab》時,會遇到一個有點尷尬的狀況——即是在書中的範例,我經常會建議讀者可以直接開一個乾淨的 Project,並且使用 Web IDE 內建的 File templates 功能產生像是 Dockerfile.gitlab-ci.yml,方便讀者可以快速的試用與學習 GitLab 的功能;但由於 GitLab 15.7 將 Web IDE 換成 VS Code 了,便導致 File Templates 功能暫時無法使用。

目前在 GitLab 原廠 Issue 清單中,確實有一張 Issue - File template feature for VSCode 提到會將 File Templates 功能塞回 Web IDE (VS Code),但恐怕仍需要好一段時間。因此在本文中,我會逐一說明本書有哪些章節有使用到 File Templates,並提供對應的替代做法。

短期內一勞永逸的方案

首先說明,在短期內最簡單的方案——答案就是不要用 gitlab.com 來搭配閱讀本書。(咦)

因為 Web IDE (VS Code) 目前仍是 Beta 功能,只是原廠很暴力的在 gitlab.com 上直接啟用它,才導致這個狀況。如果你是自己架設 GitLab Server,那麼你其實是可以避開這個問題的

  • 不要升級到 GitLab 15.7,繼續使用像是 15.6 或其他 15.* 的版本搭配本書。
  • 或者升級到 GitLab 15.7,但不要去啟用 vscode_web_ide 這個 Feature。(預設是關閉的,因此你還是可以升級到 15.7 啦)

目前有提到 File Templates 的章節

p.3-51

Web IDE 還有另一個方便的小功能是 File templates,讓我們在新增像是 Dockerfile 時,可以選擇現成的範本。

這裡是本書首次介紹舊版 Web IDE 內建 File templates,就只能請使用 Web IDE (VS Code) 的讀者一笑置之了。

p.3-52

在 GitLab 15.5,如果你自架 GitLab Server 並升級至付費等級 Premium,你就能自行新增更多的 File templates,建立你團隊專用的範本庫,讓團隊成員在新增各種常用的檔案時,免去自行複製貼上的重複動作,可直接選用事先準備好的 Templates。(https://docs.gitlab.com/ee/user/admin_area/settings/instance_template_repository.html)

如果你是 GitLab 15.7 + 有啟用 vscode_web_ide 的使用者,這裡要特別注意,Web IDE (VS Code) 目前仍沒有 File templates 功能,因此連帶就算你有升級為 Premium,也新增了自己的專屬範本庫,但你的 User 在 Web IDE (VS Code) 中可是無法使用它們的。

p.3-60 ~ p.3-61

GitLab 官方已經貼心預備了 PHP Package 專用的 .gitlab-ci.yml 範本,下面的範例檔案可以直接透過 Web IDE 的 File templates 功能建立。

以下直接提供此 .gitlab-ci.yml 範本內容,內容來自原廠 Project

# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Composer.gitlab-ci.yml

# Publishes a tag/branch to Composer Packages of the current project
publish:
  image: curlimages/curl:latest
  stage: build
  variables:
    URL: "$CI_SERVER_PROTOCOL://$CI_SERVER_HOST:$CI_SERVER_PORT/api/v4/projects/$CI_PROJECT_ID/packages/composer?job_token=$CI_JOB_TOKEN"
  script:
    - version=$([[ -z "$CI_COMMIT_TAG" ]] && echo "branch=$CI_COMMIT_REF_NAME" || echo "tag=$CI_COMMIT_TAG")
    - insecure=$([ "$CI_SERVER_PROTOCOL" = "http" ] && echo "--insecure" || echo "")
    - response=$(curl -s -w "\n%{http_code}" $insecure --data $version $URL)
    - code=$(echo "$response" | tail -n 1)
    - body=$(echo "$response" | head -n 1)
    # Output state information
    - if [ $code -eq 201 ]; then
        echo "Package created - Code $code - $body";
      else
        echo "Could not create package - Code $code - $body";
        exit 1;
      fi

p.3-61

補充說明 File templates 也有其他程式語言的 .gitlab-ci.yml 範本,可以直接無痛套用。

GitLab 原廠提供的 Templates,大致都可以在此 Project 內找到。

p.3-68 ~ p.3-69

  1. 在 Group 內建立一個乾淨的 Project,命名為 demo-image 。接著進入 Web IDE 並使用 File templates 新增 Dockerfile ,Template 選擇的是 HTTPd

以下直接提供此 Dockerfile 範本內容,內容來自原廠 Project

FROM httpd:alpine

COPY ./ /usr/local/apache2/htdocs/
  1. 再次使用 File templates 新增 .gitlab-ci.yml ,Template 選擇 Docker 。

以下直接提供此 .gitlab-ci.yml 範本內容,內容來自原廠 Project

# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Docker.gitlab-ci.yml

# Build a Docker image with CI/CD and push to the GitLab registry.
# Docker-in-Docker documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
#
# This template uses one generic job with conditional builds
# for the default branch and all other (MR) branches.

docker-build:
  # Use the official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  # Default branch leaves tag empty (= latest tag)
  # All other branches are tagged with the escaped branch name (commit ref slug)
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag=""
        echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
      else
        tag=":$CI_COMMIT_REF_SLUG"
        echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
      fi
    - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
    - docker push "$CI_REGISTRY_IMAGE${tag}"
  # Run this job in a branch where a Dockerfile exists
  rules:
    - if: $CI_COMMIT_BRANCH
      exists:
        - Dockerfile

p.3-75

  1. 開啟 Web IDE,新增檔案 .gitlab-ci.yml ,在 File templates 中選擇 SAST

以下直接提供此 .gitlab-ci.yml 範本內容,內容來自原廠 Project

# This template moved to Jobs/SAST.gitlab-ci.yml in GitLab 14.0
# Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/292977

include:
  template: Jobs/SAST.gitlab-ci.yml

p.3-77

  1. 開啟 Web IDE,新增檔案 .gitlab-ci.yml ,在 File templates 中選擇 Secret-Detection

以下直接提供此 .gitlab-ci.yml 範本內容,內容來自原廠 Project

# This template moved to Jobs/Secret-Detection.gitlab-ci.yml in GitLab 14.0
# Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/292977

include:
  template: Jobs/Secret-Detection.gitlab-ci.yml

p.3-81

  1. 開啟 Web IDE,新增檔案 .gitlab-ci.yml ,在 File templates 中選擇 Code-Quality

以下直接提供此 .gitlab-ci.yml 範本內容,內容來自原廠 Project

include:
  template: Jobs/Code-Quality.gitlab-ci.yml

p.3-83

  1. 進入Web IDE,點擊 New file ,利用 File templates 為 Project 建立 .gitlab-ci.yml,選用的 Template 為Docker

以下直接提供此 .gitlab-ci.yml 範本內容,內容來自原廠 Project

# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Docker.gitlab-ci.yml

# Build a Docker image with CI/CD and push to the GitLab registry.
# Docker-in-Docker documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
#
# This template uses one generic job with conditional builds
# for the default branch and all other (MR) branches.

docker-build:
  # Use the official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  # Default branch leaves tag empty (= latest tag)
  # All other branches are tagged with the escaped branch name (commit ref slug)
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag=""
        echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
      else
        tag=":$CI_COMMIT_REF_SLUG"
        echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
      fi
    - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
    - docker push "$CI_REGISTRY_IMAGE${tag}"
  # Run this job in a branch where a Dockerfile exists
  rules:
    - if: $CI_COMMIT_BRANCH
      exists:
        - Dockerfile