折腾一天了也搞不定前端生产环境和开发环境部署。。
# 阶段
stages:
  # - install
  - build
  - deploy_test
  - deploy_production

cache:
  paths:
    - node_modules/

# 打包项目
build:
  stage: build
  tags:
    - h5
  script:
    - pnpm install
    - |
      if [ -z "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
        echo "Not a merge request. Using CI_COMMIT_REF_NAME"
        TARGET_BRANCH=$CI_COMMIT_REF_NAME
      else
        TARGET_BRANCH=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
      fi
      echo $TARGET_BRANCH
      if [ "$TARGET_BRANCH" = "dev" ]; then
        echo "Building for test environment..."
        pnpm build:test-h5
      elif [ "$TARGET_BRANCH" = "master" ]; then
        echo "Building for production environment..."
        pnpm build:prod-h5
      else
        echo "Unknown branch: $TARGET_BRANCH"
        exit 1
      fi
  # 将此阶段产物传递至下一阶段
  artifacts:
    paths:
      - packages/h5/dist

# 部署到测试环境
deploy_test:
  stage: deploy_test
  tags:
    - h5
  only:
    - dev
  script:
    # 创建备份
    - mkdir -p $CI_PROJECT_DIR/test_h5_backup
    - cp -r /home/wentao/wentao_h5 $CI_PROJECT_DIR/test_h5_backup

    # 尝试部署到测试服务器
    - rsync -av --delete ${CI_PROJECT_DIR}/packages/h5/dist/ /home/wentao/wentao_h5/ || { mkdir -p $CI_PROJECT_DIR/test_h5_backup; rsync -av /home/wentao/wentao_h5/ $CI_PROJECT_DIR/test_h5_backup/; exit 1; }

  environment:
    name: testing
    url: https://testh5.wintaocloud.com

# 部署到生产环境
deploy_production:
  stage: deploy_production
  tags:
    - h5
  only:
    - master
  before_script:
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - ssh-keyscan -H 47.101.70.32 >> ~/.ssh/known_hosts
  script:
    - ssh root@47.101.70.32 'mkdir -p /home/wentao/wentao_h5_backup'
    - ssh root@47.101.70.32 'cp -r /home/wentao/wentao_h5 /home/wentao/wentao_h5_backup'
    - rsync -av --delete ${CI_PROJECT_DIR}/packages/h5/dist/ root@47.101.70.32:/home/wentao/wentao_h5/ || { ssh root@47.101.70.32 'rsync -av --delete /home/wentao/wentao_h5_backup/ /home/wentao/wentao_h5/' && exit 1; }
  environment:
    name: production
    url: https://h5.wintaocloud.com
举报· 26 次点击
登录 注册 站外分享
快来抢沙发
0 条回复  
返回顶部