鍒涘缓.gitlab-ci.yml鏂囦欢锛氬湪椤圭洰鏍圭洰褰曚腑鍒涘缓.gitlab-ci.yml鏂囦欢銆傝鏂囦欢灏嗗畾涔塁I/CD娴佹按绾夸腑鐨勬瀯寤恒佹祴璇曞拰閮ㄧ讲姝ラ銆
瀹氫箟CI闃舵锛氬湪.gitlab-ci.yml鏂囦欢涓畾涔塁I闃舵銆侰I闃舵灏嗗畾涔夊浣曟瀯寤哄拰娴嬭瘯鎮ㄧ殑浠g爜銆備緥濡傦紝鎮ㄥ彲浠ヤ娇鐢―ocker瀹瑰櫒鏉ユ瀯寤哄拰娴嬭瘯浠g爜銆
image: docker:latest
services:
- docker:dind
stages:
- build
- test
build:
stage: build
script:
- docker build -t my-image:latest .
test:
stage: test
script:
- docker run my-image:latest npm test
deploy:
stage: deploy
environment:
name: production
url: https://my-app.com
script:
- docker push my-image:latest
- ssh deploy@my-server 'docker pull my-image:latest && docker-compose up -d'
$ git add .gitlab-ci.yml
$ git commit -m "add gitlab-ci.yml"
$ git push origin main
鍦 GitLab CI/CD 涓紝.gitlab-ci.yml 鏂囦欢鏄畾涔 CI/CD 娴佹按绾跨殑鏂囦欢銆傝繖涓枃浠剁敱澶氫釜閮ㄥ垎缁勬垚锛屽寘鎷 variables銆乻tages銆乯obs 绛夈
涓嬮潰鏄 variables 閮ㄥ垎鐨勪竴浜涘父鐢ㄥ彉閲忓強鍏跺惈涔夛細
杩欎簺鍙橀噺鍙互鍦 .gitlab-ci.yml 鏂囦欢涓娇鐢紝浠ヤ究鍦ㄦ瀯寤恒佹祴璇曞拰閮ㄧ讲杩囩▼涓紩鐢ㄥ畠浠備緥濡傦細
stages:
– build
– test
– deploy
build:
stage: build
script:
– docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA .
test:
stage: test
script:
– docker run $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA npm test
deploy:
stage: deploy
script:
– ssh $CI_DEPLOY_USER@$CI_DEPLOY_SERVER ‘docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA && docker-compose up -d’
鍦ㄤ笂闈㈢殑绀轰緥涓紝鎴戜滑浣跨敤浜 $CI_COMMIT_SHORT_SHA 鍙橀噺鏉ユ爣璁 Docker 闀滃儚鐨勭増鏈紝浣跨敤 $CI_REGISTRY_IMAGE 鍙橀噺鏉ユ寚瀹 Docker 闀滃儚鐨勫悕绉帮紝浣跨敤 $CI_DEPLOY_USER 鍜 $CI_DEPLOY_SERVER 鍙橀噺鏉ラ儴缃插簲鐢ㄧ▼搴忋
鍙戣〃鍥炲