Zum Hauptinhalt springen

Pipeline Beispiel

Im folgenden ein Beispiel für eine CI/CD-Pipeline Anhand eines Beispieles.

Die Pipeline enthält die oben beschriebenen Schritte:

build pages

In der stage test pages werden zu Beginn alle nötigen Pakete über den Node Paket Manager installiert. Al nächstes wird die Website mithilfe des Befehls npm run build erstellt. Hierfür ist das Docker Image nodejs benötigt.

build pages:
stage: build pages
script:
- cd my-website
- npm install
- npm run build
artifacts:
paths:
- ./my-website/build

test pages

In der nachfolgenden stage test pages werden alle relevanten tests der Anwendung durchgeführt.

Beim ersten Test wird mithilfe des grep Befehls überprüft, ob die Seite für die passende Subdomain erstellt wurde. Hier wird das docker image alpine verwendet, da es sehr klein ist und für die Prüfung völlig ausreichend ist.

test pages artifact:
image: alpine
stage: test pages
script:
# Check if build is created
- grep -q "https://gl.hl-dev.de/kom/" ./my-website/build/index.html

Bei zweiten Test, welcher parallel zum ersten durchgeführt wird, werden die Markdown Dateien mithilfe des npm Paketes markdown-link-check auf nicht vorhandene externe Links geprüft.

test external link:
stage: test pages
script:
- npm install -g markdown-link-check
- cd my-website/blog
- find . -name \*.md -print0 | xargs -0 -n1 markdown-link-check -c ../test/linkcheck_config.json
- cd ..
- cd docs
- find . -name \*.md -print0 | xargs -0 -n1 markdown-link-check -c ../test/linkcheck_config.json

Im dritten parallel stattfindenden Test werden die Markdown Dateien auf Rechtschreibung hin geprüft. Hierfür existiert ein Docker Image, welche nuspell und powershell enthält.

Info

Das Docker Image befindet sich folgender Repository: https://gitlab.hl-dev.de/root/nuspell/

test nuspell:
image: registry.hl-dev.de:5050/root/nuspell
stage: test pages
script:
- ls
- nuspell -D
- cd nuspell
# 13.12.2022 deactivated because of plantuml plugin
- pwsh nuspell.ps1 -dicFile ./de_DE_frami.dic -files "../my-website/blog/*.md"

Kompletter yml-Code zur Pipeline

Im folgenden noch die komplette Datei der CI/CD-Pipeline.

image: node

stages:
- build pages
- test pages
- deploy pages
- build productive
- test productive
- deploy productive
- test upload productive

cache:
paths:
- ./my-website/node_modules/

build pages:
stage: build pages
script:
- cd my-website
- npm install
- npm run build
artifacts:
paths:
- ./my-website/build

test pages artifact:
image: alpine
stage: test pages
script:
# Check if build is created
- grep -q "https://gl.hl-dev.de/kom/" ./my-website/build/index.html

test external link:
stage: test pages
script:
- npm install -g markdown-link-check
- cd my-website/blog
- find . -name \*.md -print0 | xargs -0 -n1 markdown-link-check -c ../test/linkcheck_config.json
- cd ..
- cd docs
- find . -name \*.md -print0 | xargs -0 -n1 markdown-link-check -c ../test/linkcheck_config.json

test nuspell:
image: registry.hl-dev.de:5050/root/nuspell
stage: test pages
script:
- ls
- nuspell -D
- cd nuspell
- pwsh nuspell.ps1 -dicFile ./de_DE_frami.dic -files "../my-website/blog/*.md"

pages:
image: alpine
stage: deploy pages
script:
# deploy to gitlab pages
- mv ./my-website/build ./public
artifacts:
paths:
- public

build productive:
stage: build productive
script:
- cd my-website
# replace test url
- sed -i '0,/kom/s///' docusaurus.config.js
- sed -i '0,/gl\./s//kom\./' docusaurus.config.js
- cat docusaurus.config.js
- sed -i 's/kom//' blog/authors.yml
- cat blog/authors.yml
# build website
- npm install
- npm run build
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# https://docs.gitlab.com/ee/ci/yaml/index.html#dependencies
# set dependency to download only the correct build, so that the test can run correctly
dependencies:
- build productive
artifacts:
paths:
- ./my-website/build

.production_template: &production_template
image: alpine
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# https://docs.gitlab.com/ee/ci/yaml/index.html#dependencies
# set dependency to download only the correct build, so that the test can run correctly
dependencies:
- build productive

test productive:
<<: *production_template
stage: test productive
script:
# check if build is created and the path isn't set
- grep -q -v "https://gl.hl-dev.de/kom/" ./my-website/build/index.html
- grep -q "https://kom.hl-dev.de/" ./my-website/build/index.html

deploy productive:
<<: *production_template
stage: deploy productive
script:
# deploy via ftp
- apk add --no-cache lftp
- cd my-website
- lftp -c "set ftp:ssl-allow no; open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_SERVER; mirror -Rev ./build/ ./"

test upload productive:
<<: *production_template
stage: test upload productive
script:
# check if it is deployed to hl-dev.de webserver
- apk add --no-cache curl
- curl -u $WEB_USERNAME:$WEB_PASSWORD https://kom.hl-dev.de | tac | tac | grep -q "kom.hl-dev.de"

Kommentare