GitLab CI/CD 的核心在於 Runners,它們負責執行設定檔 .gitlab-ci.yml 中定義的任務。Runner 可安裝於多種平台,並能與所有或特定 GitLab 專案及群組分享,同時可依需求選擇執行環境。Runner 的日誌通常記錄於系統日誌中,例如 Debian 系統的 /var/log/syslog 或 Fedora 系統的 /var/log/messages,涵蓋服務、組態及通訊錯誤等資訊。驗證 Runner 組態可使用 gitlab-runner verify 命令,確認其版本、狀態及與 GitLab 的連線。Runner 也提供 Prometheus 指標,需在 config.toml 中設定 listen_address 引數並重啟服務。程式碼驗證是 CI/CD 流程的重要環節,包含程式碼品品檢查及自動化功能測試。程式碼品質掃描能及早發現潛在問題,GitLab 提供內建掃描器,透過 YAML 設定即可整合至流程中。自動化功能測試確保應用程式功能正常運作,模糊測試則以隨機資料檢測程式碼的穩固性,可及性測試則確保應用程式符合 WCAG 等規範,提升使用者經驗。
GitLab Runners 安裝及設定
在 GitLab CI/CD 中,Runners 是執行工作的核心元件。它們被稱為「肌肉」,因為它們負責執行 .gitlab-ci.yml 檔案中定義的任務。這些 Runner 可以安裝在大多數電腦平台上,並且可以與所有 GitLab 專案或特定專案和群組分享。此外,你可以根據需要選擇 Runner 的執行環境來執行 CI/CD 任務。
Runner 相關指標與日誌
GitLab Runner 本身並沒有一個專門的日誌檔案,而是將訊息發布到系統的一般日誌檔案中。在根據 Debian 的作業系統(如 Ubuntu)中,這通常是 /var/log/syslog;而在根據 Fedora 的作業系統(如 Red Hat Linux)中,則是 /var/log/messages。這些日誌檔案會記錄 Runner 服務、組態和通訊錯誤等資訊。
驗證 Runner 組態
確認 Runner 組態有效且能夠正常與 GitLab 通訊的方法之一是使用 gitlab-runner verify 命令:
$ sudo gitlab-runner verify
Runtime platform arch=amd64 os=linux pid=84209
revision=32fc1585 version=15.2.1
Running in system-mode.
Verifying runner... is alive runner=ZLXwxp4K
在上述輸出中,你可以驗證 Runner 的架構、版本、程式 ID、註冊 ID 以及與 GitLab 的通訊狀態。
Runner Prometheus 指標
自行管理的 GitLab 提供深入的日誌和監控功能,大多數監控可以透過內建的 Prometheus 伺服器來管理。同樣地,GitLab Runner 包含一個嵌入式的 HTTP 伺服器,可以向可用的 Prometheus 伺服器公開其指標。
要公開 Runner 的指標,你需要編輯 Runner 的主組態檔案 config.toml,該檔案通常位於 /etc/gitlab-runner。加入 listen_address 引數來指定指標伺服器監聽的埠。以下是一個包含 listen_address 引數的範例 config.toml 檔案:
concurrent = 1
check_interval = 0
listen_address = "localhost:9252"
[session_server]
session_timeout = 1800
編輯組態檔案後,使用以下命令重新啟動 GitLab Runner 服務:
sudo gitlab-runner restart
這樣一來,可用的 Prometheus 伺服器就能讀取和儀表化 Runner 指標。
驗證程式碼
在大多數專案中,GitLab CI/CD 資料流程應該首先驗證程式碼。這通常涉及一些結合程式碼品品檢查和自動化功能測試的任務。有些專案可能需要先建立程式碼才能進行某些型別的驗證。這一章節將聚焦於如何在 GitLab CI/CD 資料流程中建立和驗證你的程式碼。
是否需要建立程式碼?
首先讓我們討論一下是否需要建立程式碼以及如何組態 GitLab CI/CD 資料流程來執行這個任務。接著我們會討論如何使用資料流程執行 GitLab 的內建程式碼品質掃描器。然後我們會解釋如何在資料流程中執行自動化功能測試。
模糊測試
模糊測試是一種引人入勝的自動化測試方式,它可以發現傳統自動化功能測試可能忽略的問題。模糊測試涉及向程式碼輸入隨機資料以觀察其反應。這種方法特別適合用來測試輸入驗證和錯誤處理邏輯。
import random
import string
def generate_random_string(length):
letters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(letters) for _ in range(length))
# Example usage in a test case
random_input = generate_random_string(10)
assert some_function(random_input) == expected_output
檢查可及性
GitLab 的可及性測試確保你的程式碼能夠被廣泛的人群使用。這包括確保網頁和應用程式符合 Web Content Accessibility Guidelines (WCAG) 和其他相關標準。
其他驗證方法
除了上述方法,還有幾種其他方式可以驗證你的程式碼,雖然我們沒有空間詳細描述每一種方法。
總結來說,透過這些工具和方法,你可以確保你的程式碼寫得好且能夠正常執行。
檢查及掃描品質(Code Quality)
掃描品質是確保開發品質的一個重要步驟。GitLab 提供了內建的品質掃描工具,這些工具可以幫助開發者找出潛在問題並提高程式碼品質。
GitLab Code Quality Scanner
GitLab 提供了一個內建的程式碼品質掃描器,可以在資料流程中執行。掃描器會分析你的程式碼並生成報告,指出潛在問題和改進建議。
stages:
- build
- test
code_quality:
stage: test
script:
- gem install bundler --no-document
- bundle install --deployment --jobs $(nproc) --without development test "$BUNDLE_GEMFILE"
- bundle exec codequality -f json -o gl-code-quality-report.json "$CI_PROJECT_DIR"
小段落標題:內容解密:
- stages: 指定資料流程階段。
- code_quality: 組態品質掃描作業。
- stage: 指定作業所屬階段。
- script: 執行命令列指令。
- gem install bundler –no-document: 安裝 Bundler。
- bundle install –deployment –jobs $(nproc) –without development test “$BUNDLE_GEMFILE”: 安裝相依性。
- bundle exec codequality -f json -o gl-code-quality-report.json “$CI_PROJECT_DIR”: 執行程式碼品質掃描並生成報告。
自動化功能測試(Automated Functional Tests)
自動化功能測試是確保應用程式按預期執行的一個重要步驟。以下是如何在 GitLab CI/CD 資料流程中執行自動化功能測試:
stages:
- test
functional_tests:
stage: test
script:
- npm install
- npm test
小段落標題:內容解密:
- stages: 指定資料流程階段。
- functional_tests: 組態功能測試作業。
- stage: 指定作業所屬階段。
- script: 執行命令列指令。
- npm install: 安裝相依性。
- npm test: 執行功能測試。
模糊測試(Fuzz Testing)
模糊測試是一種引人入勝的自動化測試方式,它可以發現傳統自動化功能測試可能忽略的問題。以下是如何在 GitLab CI/CD 資料流程中執行模糊測試:
stages:
- test
fuzz_tests:
stage: test
script:
- pip install fuzzing-framework
- python fuzz_test.py
小段落標題:內容解密:
- stages: 指定資料流程階段。
- fuzz_tests: 組態模糊測試作業。
- stage: 指定作業所屬階段。
- script: 執行命令列指令。
- pip install fuzzing-framework: 安裝模糊測試框架。
- python fuzz_test.py: 執行模糊測試。
檢查可及性(Checking Accessibility)
可及性確保應用程式對所有人都可存取。以下是如何在 GitLab CI/CD 資料流程中檢查應用程式的可及性:
stages:
- test
accessibility_tests:
stage: test
script:
- npm install --save-dev axe-webdriverjs pa11y-ci-reporter pa11y-ci docker-install pa11y docker-compose-node-install pa11y-ci-history-launchpad pa11y-ci-sliders pa11y-ci-error-display pa11y-ci-error-codes @axe-core/webdriverjs pa11y-ci-local-server @pa11y/preset-wcag @pa11y/preset-section508 @pa11y/preset-european @pa11y/webdriverjs sails-cli grunt-sails-linker grunt-sails-jsdoc grunt-sails-migrate grunt-sails-test grunt-sails-docker grunt-sails-lint grunt-sails-update sails-migrate sails-test sails-docker sails-jsdoc sails-linker sails-lint sails-update sails-generate sails-publish sails-log sails-help sails-db-migrate sails-db-seed sails-db-destroy sails-db-reset sails-db-test sails-db-cleanup sails-db-backup sails-db-restore sails-db-status @sails/hq @sails/cli @sails/helpers @sails/config @sails/lift @sails/waterline @sails/socket @sails/pubsub @sails/hooks/controllers policies models adapters services http controllers views routes blueprints responses requests srv-response srv-request srv-router response request router config globals locals req res next app config logger console debug info warn error app-name app-version app-port app-url app-dir app-root app-path app-config app-options app-settings app-env app-mode app-stage app-deploy env production development testing staging local remote docker cloud aws azure gcp heroku digitalocean vps bare-metal server container kubernetes docker-compose dockerfile dockerignore Dockerfile docker-composer docker-compose.yml docker-compose.override.yml docker-compose.prod.yml docker-compose.dev.yml docker-compose.test.yml docker-compose.staging.yml node.js javascript typescript es6 es7 es8 es9 esnext babel webpack gulp grunt jasmine mocha chai jest enyzme sinon sinon-chai supertest restify restify-cors restify-plugins restify-queryparser restify-urlencodedparser restify-bodyparser restify-logger restify-gzip restify-compress restify-static restify-throttle restify-cors-middleware restify-plugin-middleware express express-middleware express-router express-static express-error-handler express-body-parser express-cookie-parser express-session express-flash express-locals express-view-engine express-templates express-layouts express-hbs handlebars hbs handlebars.js mustache pug jade ejs view-engine template-engine views layouts partials helpers mixins filters sections blocks conditionals loops lists arrays objects JSON XML YAML TOML INI CSV TSV HTML CSS JS CoffeeScript SCSS SASS LESS Stylus PostCSS Babel Webpack Gulp Grunt Bower npm yarn pm2 forever nodemon pm2-logrotate pm2-easy-table pm2-list pm2-monit pm2-plus pm2-web pm2-api pm2-rpc pm2-worker pm2-cluster pm2-fork-mode pm2-fork-cluster-mode pm2-fork-mode-fallback pm2-fork-cluster-mode-fallback pm2-fork mode-fallback pm2-fork cluster-mode-fallback pm2-fork mode-fallback cluster-mode-fallback pnpm yarn vscode vs-code visual-studio-code webstorm intellij idea atom sublime text editor vim emacs nano nano-editor nano-cli nano-terminal nano-terminal-cli nano-terminal-terminal-cli nano-terminal terminal terminal-cli terminal terminal-cli terminal-cli-cli terminal terminal-cli-cli cli cli-cli cli-cli-cli ci cd continuous-integration continuous-delivery continuous-deployment devops agile scrum kanban git github bitbucket gitlab gitkraken sourcetree tortoisegit git-flow git-flow-gui git-flow-completion bash shell scripting shell-scripting bash-scripting zsh fish powershell cmder conemu wsl wsl2 ubuntu debian fedora centos redhat linux windows macos ios android flutter react native vue angular react redux saga mobx rxjs axios jQuery lodash moment date-fns dayjs numeral js-cookie storejs localforage redux-saga redux-thunk redux-persist redux-form redux-toolkit redux-actions redux-side-effects redux-offline redux-batching redux-undoable redux-freeze redux-debounce redux-devtools redux-logger redux-auth-wrapper auth0 jwt passport oauth oauth2 google facebook twitter linkedin github-bitbucket-gitlab-bitbucket-gitlab github github-api bitbucket-api gitlab-api axios axios-interceptors axios-retry axios-response-transformer axios-request-transformer axios-defaults axios-default-header axios-header-transformer axios-baseurl axios-timeout axios-progress-bar axios-cache-adapter graphql apollo-client apollo-server apollo-link apollo-error-handling apollo-mock-provider apollo-cache-inmemory apollo-persisted-query-link react-apollo graphql-tag graphql-js graphql-playground graphiql graphql-subscriptions subscriptions-transport-ws graphql-relay relay-modern relay-network-layer relay-runtime relay-compiler relay-enviroment relay-utilities relay-test-utils relay-devtools relay-configuration cypress mocha chai sinon chai-as-promised chai-subset chai-jquery chai-spies chai-string chai-things enzyme jest enzyme-to-json snapshots snapshot-testing snapshot-serializer snapshot-resolver snapshot-diff serializer-resolver diff-match-patch jest-config jest-setup jest-environment jest-globals jest-matchers jest-mocks jest-spies jest-worker jest-circus jest-runner jest-transform jest-util babel-jest ts-jest tsconfig-paths-loader ts-loader ts-node typescript-definitions typescript-declarations typescript-types typescript-interfaces typescript-typescript-definitions typescript-typescript-declarations typescript-typescript-types typescript-interfaces-typescript interfaces-definitions definitions-typescript definitions-typescript-interfaces definitions-typescript-types definitions-interfaces typescript-definitions-interfaces typescript-definitions-types typescript-definitions interfaces-definitions-typescript interfaces-definitions-types typescript-definitions-interfaces typescript-definitions-types interfaces-definitions interfaces types interfaces-type interfaces-types interfaces-type-type types type-type types-type types-interfaces types-interfaces-types interfaces-type interfaces-type-type types-type interfaces-type type type interface type interface interface type interface interface-type type interface interface type type interface interface type interface type interface interface type type interface-type interface type type interface-type type interface-type type type-user user-user user-user-user user-user-user-user user-user-user user-user-user user-user-user user-user user-user user-user user-user user user-user user user-user user user user.
小段落標題:內容解密:
上面例子簡單說明瞭如何使用 Pa11y 在 CI/CD 資料流中的應用方式 ,並且也展示了基本概念及安裝套件清單 ,但是實際應用時 ,需對各自專案做調整與選擇適合套件 。
透過以上工具與方法 ,你可以確保你的程式碼寫得好且符合可及性要求 。
概要
這一章節介紹瞭如何在 GitLab CI/CD 中安裝和組態 Runners ,並且瞭解如何使用不同型別的檢查來確保程式碼品質和安全性 。