在 Linux 中使用 GitLab CI/CD 的实操指南
一 架构与准备
二 编写第一个 .gitlab-ci.yml
stages:
- build
- test
- deploy
variables:
NODE_ENV: production
build_job:
stage: build
image: node:20
script:
- npm ci
- npm run build
cache:
paths:
- node_modules/
artifacts:
paths:
- dist/
expire_in: 1 week
test_job:
stage: test
image: node:20
script:
- npm test -- --ci
cache:
paths:
- node_modules/
deploy_job:
stage: deploy
image: alpine:latest
before_script:
- apk add --no-cache openssh-client
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $SERVER_IP >> ~/.ssh/known_hosts
script:
- ssh -o StrictHostKeyChecking=no -i $SSH_PRIVATE_KEY $SERVER_USER@$SERVER_IP \
"cd $PROJECT_PATH && git pull origin main && npm install && npm run build"
environment:
name: production
url: https://your-domain.com
when: manual
tags:
- linux
三 变量与密钥及安全实践
四 触发与排错
五 进阶场景与优化