最近项目使用Gitlab CI/CD进行自动化构建的一些记录,所有项目均使用Git Tag触发
Vue项目打包并暂存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| build-job: stage: build image: node:16 rules: - if: $CI_COMMIT_TAG script: - npm install - npm run build artifacts: name: "${CI_COMMIT_TAG}" expire_in: 3 days paths: - dist cache: paths: - node_modules/ - dist/
|
npm安装后打包,在artifacts暂存3天,构建完成后可以在pipeline点击下载,文件名为Git Tag
修改版本号提交
1 2 3 4 5 6 7 8 9 10 11
| version-job: stage: version image: node:16 rules: - if: $CI_COMMIT_TAG script: - npm -git-tag-version=false version $CI_COMMIT_TAG - git config --global user.name ci - git config --global user.email ci@email.com - git commit -am "Update verison $CI_COMMIT_TAG" - git push "https://access_token_name:${CI_PUSH_TOKEN}@gitlab.com/project_name.git" HEAD:$CI_COMMIT_REF_NAME
|
将package.json中的版本号修改为Git Tag版本号,并提交代码库
这里需要生成gitlab的access token,并且配置到CI/CD的Variables中使用
打包文件上传到测试服务器
1 2 3 4 5 6 7 8 9 10 11 12
| deploy-job: stage: deploy image: node:16 rules: - if: $CI_COMMIT_TAG before_script: - 'which ssh-agent || ( apk update && apk add openssh-client)' - eval $(ssh-agent -s) - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - script: - ssh -o StrictHostKeyChecking=no -p 22 user@host "rm -rf xxxx/* && exit" - scp -o StrictHostKeyChecking=no -P 22 -r dist/* user@host :xxxx/
|
安装ssh后,通过ssh删除原有文件,然后scp复制到远程服务器
需要配置ssh key并配置到CI/CD的Variables中使用
Electron打包windows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| build-job: stage: build image: electronuserland/builder:wine rules: - if: $CI_COMMIT_TAG script: - npm install - npm run build artifacts: name: "${CI_COMMIT_TAG}" expire_in: 3 days paths: - build cache: paths: - node_modules/ - dist/ - build/
|
因为直接构建windows会提示缺少wine,安装起来比较麻烦,可以直接使用镜像electronuserland/builder:wine
进行构建
Docker镜像并上传Harbor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| build-job: stage: build rules: - if: $CI_COMMIT_TAG image: name: anjia0532/kaniko-project.executor:debug entrypoint: [""] script: - echo "{\"auths\":{\"$HARBOR_URL\":{\"auth\":\"$(echo -n $HARBOR_USERNAME:$HARBOR_PASSWORD | base64)\"}}}" > /kaniko/.docker/config.json - /kaniko/executor --context "${CI_PROJECT_DIR}" --dockerfile "${CI_PROJECT_DIR}/Dockerfile" --destination "${DOCKER_IMAGE_NAME}:${CI_COMMIT_TAG}" --snapshotMode=redo --use-new-run
|
借助kaniko
,将项目打包成docker镜像后,上传到Harbor镜像仓库
Docker镜像部署k8s
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| deploy-job: stage: deploy rules: - if: $CI_COMMIT_TAG image: name: bitnami/kubectl:1.27 entrypoint: [""] script: - kubectl config get-contexts - kubectl config use-context ${CONTEXT_NAME} - kubectl config view - kubectl --namespace develop get deployments - kubectl --namespace develop describe deployments/${DEPLOYMENT_NAME} - kubectl --namespace develop set image deployments/${DEPLOYMENT_NAME} ${CONTAINER_NAME}=${DOCKER_IMAGE_NAME}:${CI_COMMIT_TAG}
|
借助kubectl
,将k8s部署容器替换为最新构建的镜像
To Be Continue