GitLab在Docker容器中发布ASP.NET Core 6 Web应用的CI/CD管道配置

w41d8nur  于 2023-03-01  发布在  Docker
关注(0)|答案(1)|浏览(99)

首先我需要说明的是,我是CI/CD和Gitlab的初学者,尽管如此,我还是设法拼凑出了(使用网络上的例子)这个“几乎”可以工作的管道配置:

image: mcr.microsoft.com/dotnet/sdk:6.0

variables:
  OBJECTS_DIRECTORY: 'obj'
  NUGET_PACKAGES_DIRECTORY: '.nuget'
  SOURCE_CODE_PATH: '*/*/'
  TAG_LATEST: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:latest
  TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHORT_SHA

stages:
  - build
  - test
  - docker
  - deploy

cache:
  key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
  paths:
    - '$SOURCE_CODE_PATH$OBJECTS_DIRECTORY/project.assets.json'
    - '$SOURCE_CODE_PATH$OBJECTS_DIRECTORY/*.csproj.nuget.*'
    - '$NUGET_PACKAGES_DIRECTORY'
  policy: pull-push

before_script:
  - 'dotnet restore --packages $NUGET_PACKAGES_DIRECTORY'

build:
  stage: build
  script:
  - 'dotnet build --no-restore'

tests:
  stage: test
  script:
    - 'dotnet test --no-restore'

docker:
  stage: docker
  image: docker:latest
  services:
    - docker:18.09.7-dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  only:
    - main
before_script:
    - apt-get update
    - apt-get install docker -y
  script:
    - docker login -u "gitlab-ci-token" -p "$CI_JOB_TOKEN" $CI_REGISTRY
    - docker build --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" --tag "$CI_REGISTRY_IMAGE:latest" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
    - docker push "$CI_REGISTRY_IMAGE:latest"

deploy:
  stage: deploy
  image: alpine:latest
  services:
  - docker:dind

  script:
  - chmod og= $ID_RSA
  - apk update && apk add openssh-client
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY"
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker pull $TAG_COMMIT"
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker container rm -f atlas-portal-2.0 || true"
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker run -d -p 80:80 --name atlas-portal-2.0 $TAG_COMMIT"
  environment: 
    name: production
  only:
  - main

当我运行这个管道时,它成功地到达了“docker”阶段,我希望在这里构建docker映像并将其存储在gitlab容器注册表中,但我得到了以下错误:
“/bin/bash:第149行:Docker:找不到命令”
我知道我需要在docker镜像中“安装”docker,这个镜像将被构建并存储在gitlab中,但是我不知道该如何去做。
而一旦我过了这个“ Docker ”阶段,我就有信心“部署”阶段也会失败。我是个新手...

5kgi1eie

5kgi1eie1#

您的docker作业应如下所示:

docker:
  stage: docker
  image: docker:latest
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: tcp://docker:2375
  only:
    - main
before_script:
    - docker login -u "gitlab-ci-token" -p "$CI_JOB_TOKEN" $CI_REGISTRY
  script:
    - docker build --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" --tag "$CI_REGISTRY_IMAGE:latest" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
    - docker push "$CI_REGISTRY_IMAGE:latest"

因为您已经使用了安装了docker的映像,所以不需要运行apt-get update && apt-get install docker -y,只需运行命令即可。
还有一条建议,最好为每个作业定义image值,除非你对所有作业都使用相同的值。因此,你在文件顶部定义的使用dotnet的值最好放在buildtests作业中。如果你不想重复代码,你可以使用变量来重用该值,例如:

variables:
    DOTNET_IMAGE: mcr.microsoft.com/dotnet/sdk:6.0

...

build:
    image: $DOTNET_IMAGE

...

tests:
    image: $DOTNET_IMAGE

相关问题