gitlab CI/CD 設定筆記

一開始是因為同事需要用 gitlab runner build vue.js 的 project, 並將結果 push 回其他 branch,但是會有問題,最終發現只是因為個單引號的關係 Orz

反正也是記錄一下,主要是整個找bug 過程


如果是太舊的 gitlab 版本 (記得是 10.x之前),就只有 admin 可以產生有 write_repo 權限的 personal access token,且沒有 project access token 可以使用。而到了最新(13.x)的版本,非 admin 帳號也需要有 maintainer 以上權限才能建立 project access token。

從下面這個例子是一直用 echo 做輸出檢查。

deploy-dev:
  tags: 
    - stageserver
  only: 
    - bdev
  variables:
    GITLAB_USER_NAME: USERNAME
    GITLAB_USER_EMAIL: EMAIL_ADDRESS
    GITLAB_PROJ_TOKEN: "PROJECT_OR_PERSONAL_TOKEN"
  script:
    - git config --local user.name "${GITLAB_USER_NAME}"
    - git config --local user.email "${GITLAB_USER_EMAIL}"
    - echo ${GITLAB_USER_NAME}
    - echo ${GITLAB_USER_EMAIL}
    - echo ${GITLAB_PROJ_TOKEN}
    - git checkout bdev
    - git pull origin bdev
    - echo "THIS is a TEST" > $(date "+%Y%m%d%H%M%S").txt
    - git add $(date "+%Y%m%d%H%M%S").txt
    - git commit -m "add test"
    - echo https://${GITLAB_USER_NAME}:${GITLAB_PROJ_TOKEN}@GITLAB.SERVER.DOMAIN/NAMESPACE/proj.git/
    - git push https://${GITLAB_USER_NAME}:${GITLAB_PROJ_TOKEN}@GITLAB.SERVER.DOMAIN/NAMESPACE/proj.git/ master

然後同事的 vue project CI/CI script 在全部弄好之後變成這樣,為了這個搞兩天有點昏倒

stages:
  - lint
  - build
  - deploy

before_script:
  - npm -v
  - node -v

lint_auth:
  stage: lint
  tags:
    - stageserver
  script:
    - cd auth
    - npm ci
    - npm run lint
  only:
    refs:
      - merge_requests
    changes:
      - auth/src/**/*
  except:
    - release/master
    - release/b_stage

lint_purchase:
  stage: lint
  tags:
    - stageserver
  script:
    - cd purchase
    - npm ci
    - npm run lint
  only:
    refs:
      - merge_requests
    changes:
      - purchase/src/**/*
  except:
    - release/master
    - release/b_stage

lint_frontend:
  stage: lint
  tags:
    - stageserver
  script:
    - cd frontend
    - npm ci
    - npm run lint
  only:
    refs:
      - merge_requests
    changes:
      - frontend/src/**/*
  except:
    - release/master
    - release/b_stage

build_auth:
  stage: build
  tags:
    - stageserver
  script:
    - cd auth
    - npm ci
    - npm run build
    - echo "Finish build!"
  artifacts:
      name: "bundled_auth"
      expire_in: 1 week
      paths:
        - auth/dist/
  only:
    refs:
      - master
      - develop
    changes:
      - auth/src/**/*

build_purchase:
  stage: build
  tags:
    - stageserver
  script:
    - cd purchase
    - npm ci
    - npm run build
    - echo "Finish build!"
  artifacts:
      name: "bundled_purchase"
      expire_in: 1 week
      paths:
        - purchase/dist/
  only:
    refs:
      - master
      - develop
    changes:
      - purchase/src/**/*

build_frontend:
  stage: build
  tags:
    - stageserver
  script:
    - cd frontend
    - npm ci
    - npm run build
    - echo "Finish build!"
  artifacts:
      name: "bundled_frontend"
      expire_in: 1 week
      paths:
        - frontend/dist/
  only:
    refs:
      - master
      - develop
    changes:
      - frontend/src/**/*

deploy_auth:
  stage: deploy
  tags:
    - stageserver
  variables:
    GITLAB_USER_NAME: USERNAME
    GITLAB_USER_EMAIL: EMAIL_ADDRESS
    GITLAB_PERSONAL_TOKEN: "PROJECT_OR_PERSONAL_TOKEN"
    DEV_BRANCH_NAME: "release/b_stage"
    PROD_BRANCH_NAME: "release/master"
    COMMIT_MSG: "build: build auth from $CI_COMMIT_BRANCH at $CI_COMMIT_TIMESTAMP"
  dependencies:
    - build_auth
  script:
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - if [ "$CI_COMMIT_BRANCH" == "develop" ]; then DEPLOYED_BRANCH_NAME="$DEV_BRANCH_NAME"; else DEPLOYED_BRANCH_NAME="$PROD_BRANCH_NAME"; fi
    - git branch -D ${DEPLOYED_BRANCH_NAME}
    - git checkout ${DEPLOYED_BRANCH_NAME}
    - git add .
    - 'git commit -m "${COMMIT_MSG}"'
    - git log --stat -2
    - git push http://${GITLAB_USER_NAME}:${GITLAB_PERSONAL_TOKEN}@NAMESPACE/NAMESPACE/webproj.git/ release/b_stage
    - echo "Finish deploy auth from $CI_COMMIT_BRANCH at $CI_COMMIT_TIMESTAMP!"
  only:
    refs:
      - master
      - develop
    changes:
      - frontend/src/**/*

deploy_purchase:
  stage: deploy
  tags:
    - stageserver
  variables:
    GITLAB_USER_NAME: USERNAME
    GITLAB_USER_EMAIL: EMAIL_ADDRESS
    GITLAB_PERSONAL_TOKEN: "PROJECT_OR_PERSONAL_TOKEN"
    DEV_BRANCH_NAME: "release/b_stage"
    PROD_BRANCH_NAME: "release/master"
    COMMIT_MSG: "build: build purchase from $CI_COMMIT_BRANCH at $CI_COMMIT_TIMESTAMP"
  dependencies:
    - build_purchase
  script:
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - if [ "$CI_COMMIT_BRANCH" == "develop" ]; then DEPLOYED_BRANCH_NAME="$DEV_BRANCH_NAME"; else DEPLOYED_BRANCH_NAME="$PROD_BRANCH_NAME"; fi
    - git branch -D ${DEPLOYED_BRANCH_NAME}
    - git checkout ${DEPLOYED_BRANCH_NAME}
    - git add .
    - 'git commit -m "${COMMIT_MSG}"'
    - git log --stat -2
    - git push http://${GITLAB_USER_NAME}:${GITLAB_PERSONAL_TOKEN}@GITLAB.SERVER.DOMAIN/NAMESPACE/webproj.git/ release/b_stage
    - echo "Finish deploy purchase from $CI_COMMIT_BRANCH at $CI_COMMIT_TIMESTAMP!"
  only:
    refs:
      - master
      - develop
    changes:
      - frontend/src/**/*
 
deploy_frontend:
  stage: deploy
  tags:
    - stageserver
  variables:
    GITLAB_USER_NAME: USERNAME
    GITLAB_USER_EMAIL: EMAIL_ADDRESS
    GITLAB_PERSONAL_TOKEN: "PROJECT_OR_PERSONAL_TOKEN"
    DEV_BRANCH_NAME: "release/b_stage"
    PROD_BRANCH_NAME: "release/master"
    COMMIT_MSG: "build: build frontend from $CI_COMMIT_BRANCH at $CI_COMMIT_TIMESTAMP"
  dependencies:
    - build_frontend
  script:
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - if [ "$CI_COMMIT_BRANCH" == "develop" ]; then DEPLOYED_BRANCH_NAME="$DEV_BRANCH_NAME"; else DEPLOYED_BRANCH_NAME="$PROD_BRANCH_NAME"; fi
    - git branch -D ${DEPLOYED_BRANCH_NAME}
    - git checkout ${DEPLOYED_BRANCH_NAME}
    - git add .
    - 'git commit -m "${COMMIT_MSG}"'
    - git log --stat -2
    - git push http://${GITLAB_USER_NAME}:${GITLAB_PERSONAL_TOKEN}@GITLAB.SERVER.DOMAIN/NAMESPACE/webproj.git/ release/b_stage
    - echo "Finish deploy frontend from $CI_COMMIT_BRANCH at $CI_COMMIT_TIMESTAMP!"
  only:
    refs:
      - master
      - develop
    changes:
      - frontend/src/**/*