gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:57:57 26 4
gpt4 key购买 nike

我正在尝试自动构建,测试和部署我的.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。我之前尝试过,所以我知道它有效。

最佳答案

我知道了,所有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等待。

关于docker - 使用FTP for .NET Core的Gitlab CI/CD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56534347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com