如何在詹金斯建立dotnetcore项目?

时间:2019-10-19 09:38:48

标签: docker jenkins .net-core docker-build

嗨,我正在尝试使用jenkins构建dotnetcore项目。下面是我的文件结构。

locationservices.api
  locationservices.api
  locationservices.api.sln
    Dockerfile
    Jenkinsfile
    locationservices.api.csproj
    startup.cs
    //rest of the files

下面是我的詹金斯文件。

stage('Build') {
      agent {
        docker {
          image 'microsoft/dotnet:2.1-sdk'
          args '-u root:root'
        }
      }

      steps {
        sh 'apt update'
        sh 'apt install -y make'
        sh 'apt install -y apt-transport-https'

        sh 'echo "{\\\"buildNumber\\\":\\\"${BUILD_NUMBER}\\\", \\\"sha\\\":\\\"need to populate\\\"}" > locationservices.api/buildinfo.json'
        sh 'dotnet publish -c Release -o out'
        sh 'chmod a+rw -R .'
        stash name: 'mws-out', includes: 'locationservices.api/out/**'
      }
    }

    stage('Upload to ECR') {
      when {
        branch 'master'
        expression {
          currentBuild.result == null || currentBuild.result == 'SUCCESS'
        }
      }

      agent {
        label 'ec2-amazonlinux-v1'
      }

      steps {
        unstash name: 'mws-out'

        script {
          docker.build("location/location-service:v_${env.BUILD_NUMBER}", "--build-arg http_proxy=${env.http_proxy} --build-arg https_proxy=${env.https_proxy} .")
          docker.withRegistry('path', "ecr") {
            docker.image("location/location-service:v_${env.BUILD_NUMBER}").push("v_${env.BUILD_NUMBER}")
            docker.image("location/location-service:v_${env.BUILD_NUMBER}").push("latest")
          }
        }
      }

下面是我的dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["locationservices.api.csproj", "locationservices.api/"]
RUN dotnet restore "locationservices.api.csproj"
COPY . .
WORKDIR "/src/locationservices.api"
RUN dotnet build "locationservices.api.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "locationservices.api.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "locationservices.api.dll"]

在管道中运行时,出现以下错误。

Step 6/17 : WORKDIR /src
 ---> Using cache
 ---> 4cc3caf28136
Step 7/17 : COPY locationservices.api/locationservices.api.csproj locationservices.api/
 ---> Using cache
 ---> 2ecf1c39cc4a
Step 8/17 : RUN dotnet restore "locationservices.api.csproj"
 ---> Running in ba3d3b8d630a
[91mDid you mean to run dotnet SDK commands? Please install dotnet SDK from:
  https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
[0mThe command '/bin/sh -c dotnet restore "locationservices.api.csproj"' returned a non-zero code: 145

我不确定为什么我无法在jenkins中构建项目。恢复时失败。有人可以帮我解决问题吗?任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

我非常确定您的Jenkins运行的计算机/服务器没有安装netcore SDK,这是构建netcore应用程序所必需的。您可以找到SDK文件here