Search code examples
dockerjenkins.net-coreamazon-ecs

How to build .net core application in Jenkins using docker and push it to ECR?


Hi I am learning to build pipeline using Jenkins and AWS for my .net core application. I created sample application at https://github.com/niranjan2020/JenkinsPipeLine I downloaded Jenkins in my local and running at port 8080. I created Pipeline template for creating Jenkins pipeline.

pipeline {
    agent any

    options {
        skipDefaultCheckout true
    }

    stages {
        stage('checkout') {
            steps {
                checkout scm
            }
        }

        stage('First Stage') {
            steps {
                echo "Yay! First stage is executed"
            }
        }
stage('Build') {
           agent {
              docker {
          image 'microsoft/dotnet:2.1-sdk'
          args '-u root:root'
        }
      }

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

        // sh 'echo "{\\\"buildNumber\\\":\\\"${BUILD_NUMBER}\\\", \\\"sha\\\":\\\"need to populate\\\"}" > Jenkins/buildinfo.json'
        sh 'echo Hi'
        sh 'chmod a+rw -R .'
        stash name: 'Jenkins-out', includes: 'Jenkins/out/**'
      }
    }
}
}

I want to build my .net core application using docker and put the image to ECR. Using above code I am able to execute first stage but I am not able to execute second stage.

process apparently never started in C:\Program Files (x86)\Jenkins\workspace\ECS@2@tmp\durable-8251f4a8
(running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)

This error message I am getting. In the above repository I have not added the docker file. Do I need to add it? Can someone help me to figure it out this? Any help would be appreciated. Thanks


Solution

  • If you are want to push image on ECR, then you should put every dependency in your dockerfile, like in Jenkins you can run postscript which install depencey for you in containers but that is not the case in AWS ECR. Your Docker iamge should able to run without any post script.

    your Dockerfile will look like

    FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
    WORKDIR /app
    COPY *.csproj ./
    RUN dotnet restore
    COPY . ./
    RUN dotnet publish -c Release -o output
    # Runtime image
    FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
    WORKDIR /app
    COPY --from=build-env /app/output .
    ENTRYPOINT ["dotnet", "DotNetProject.dll"]
    

    the build-env will build your app at the first stage, and the second will copy the build app from the build stage.

    If you Jenkins server inside AWS VPC, assign the role to Jenkins server which has access to ECR.

    if running outside then feed the credential.

    enter image description here

    node {
      stage 'Checkout'
      git 'ssh://https://github.com/niranjan2020/JenkinsPipeLine'
    
      stage 'Docker build'
      docker.build('demo')
    
      stage 'Docker push'
      docker.withRegistry('https://1234567890.dkr.ecr.us-east-1.amazonaws.com', 'ecr:us-east-1:demo-ecr-credentials') {
        docker.image('demo').push('latest')
      }
    }
    

    update registry URL with your ECR.

    you can check further here.