使用FTP for .NET Core的Gitlab CI / CD

时间:2019-06-10 22:40:21

标签: docker continuous-integration gitlab asp.net-core-2.0

我正在尝试自动构建,测试和部署我的.NET Core应用程序,到目前为止,它可以构建和测试,但不会部署。 Gitlab管道显示该工作已成功完成,但实际上并未成功。这是我的Dockerfile:

FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /source

RUN curl -sL https://deb.nodesource.com/setup_11.x |  bash - \
    && apt-get install -y nodejs


COPY ./src/*.csproj .
RUN dotnet restore

COPY ./src/ ./

RUN dotnet publish "./Spa.csproj" --output "./dist" --configuration Release --no-restore

FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /source/dist .

RUN apt-get update \
    && apt-get install -y php-cli

COPY deployment.ini /app
EXPOSE 80
ENTRYPOINT ["dotnet", "Spa.dll"]

这是我的.gitlab-ci.yml文件的样子:

# ### Define variables
#
variables:
 OBJECTS_DIRECTORY: 'obj'    
 NUGET_PACKAGES_DIRECTORY: '.nuget'
 SOURCE_CODE_PATH: 'src/'             

# ### Define stage list
stages:
 - build
 - test
 - deploy

cache:
 # Per-stage and per-branch caching.
  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'
    - '**/node_modules/'

build:
  image: docker:stable
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t registry.gitlab.com/chinloyal/spa .
    - docker push  registry.gitlab.com/chinloyal/spa

tests:
  image: microsoft/dotnet:2.2-sdk
  stage: test
  before_script:
    - curl -sL https://deb.nodesource.com/setup_11.x |  bash -
    - apt-get install -y nodejs
  script:
    - dotnet test --no-restore Tests/

deploy:
  image: docker:stable
  services:
    - docker:dind
  stage: deploy
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker pull registry.gitlab.com/chinloyal/spa
    - docker run --name spa -p 80:80 -d registry.gitlab.com/chinloyal/spa
  script:
    - docker exec -d spa curl -S "https://gitlab.com/chinloyal/ftp-deploy-tool/raw/master/deployment.phar" --output deployment.phar
    - docker exec -d spa bash -c "echo remote = $FTP_HOST >> deployment.ini"
    - docker exec -d spa bash -c "echo user = $FTP_USER >> deployment.ini"
    - docker exec -d spa bash -c "echo password = $FTP_PASSWORD >> deployment.ini"
    - docker exec -d spa php deployment.phar deployment.ini
  environment:
    name: production
  only:
    - master

此行docker exec -d spa php deployment.phar deployment.ini是假设从docker容器内部上传文件的那一行。但是我相信,因为Gitlab在该行之后立即结束了该过程。然后容器内的过程就结束了。

我尝试使用注册表映像(registry.gitlab.com/chinloyal/spa)作为部署映像,但是每次尝试使用它时,它只会在Gitlab上开始运行项目,直到超时或取消为止。

由于我是共享服务器,所以我只能通过ftp访问服务器。我正在使用的ftp工具是here。我以前尝试过,所以我知道它有用。

1 个答案:

答案 0 :(得分:0)

我发现,所有docker exec命令都使用了-d标志,所以我删除了-d标志,以便gitlab可以显示正在下载和上传的文件的输出。所以我将此docker exec -d spa curl -S "https://gitlab.com/chinloyal/ftp-deploy-tool/raw/master/deployment.phar" --output deployment.phar更改为docker exec spa curl -S "https://gitlab.com/chinloyal/ftp-deploy-tool/raw/master/deployment.phar" --output deployment.phar

我也将docker exec -d spa php deployment.phar deployment.ini更改为docker exec spa php deployment.phar deployment.ini

因此,在他们以分离模式运行之前,由于gitlab认为他们已经完成并继续执行下一个命令,因此删除-d标志会让gitlab等待。