• Daily Archives: 2022年12月2日

一个最基础的gitlab自动编译vue项目并发布到docker的ci配置

# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

stages:          # List of stages for jobs, and their order of execution.
  - build
#  - test
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - npm install
    - npm run build
    - docker build -t yuexi_admin .
    - echo "Compile complete."

#unit-test-job:   # This job runs in the test stage.
#  stage: test    # It only starts when the job in the build stage completes successfully.
#  script:
#    - echo "Running unit tests... This will take about 60 seconds."
#    - sleep 60
#    - echo "Code coverage is 90%"

#lint-test-job:   # This job also runs in the test stage.
#  stage: test    # It can run at the same time as unit-test-job (in parallel).
#  script:
#    - echo "Linting code... This will take about 10 seconds."
#    - sleep 10
#    - echo "No lint issues found."

deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  environment: production
  script:
    - echo "Deploying application..."
    - docker stop yuexi_admin
    - docker rm yuexi_admin
    - docker run -d -p 8611:80 --name yuexi_admin yuexi_admin
    - echo "Application successfully deployed."

使用gilab-ci运行docker命令时报Got permission denied权限错误的问题

如上图所示,报了权限错误。

方法一:临时修改权限(重启后会失效)

是将/var/run/docker.sock的权限设置为666,如下图所示:

方法二:将当前用户组授权到docker中

先查看原来的docker用户组的情况:

cat /etc/group | grep docker

应该会如下图所示:

然后使用以下指令将需要权限的用户($USER为当前登录的用户,如果是gitlab-runner执行则要替换成gitlab-runner用户)加入到docker组:

sudo gpasswd -a $USER docker

执行后再次使用cat命令查看情况,正常来说会如下图所示:

然后使用 service docker restart 命令重启docker即可。

close