JuergenGutsch / blog

Personal blog about web development based on .NET and .NET Core
https://asp.net-hacker.rocks/
Other
31 stars 17 forks source link

Trying BitBucket Pipelines with ASP.NET Core #92

Open JuergenGutsch opened 5 years ago

JuergenGutsch commented 5 years ago

Written on 07.12.2017 20:32:28

URL: http://asp.net-hacker.rocks/2017/12/08/bitbucket-pipelines.html

JuergenGutsch commented 5 years ago

Comment written by JILLA RAJANI on 18.03.2018 18:00:17

Hi,

I am in need of similar kind of deployment. Bit bucket with Azure Web App. Am facing an issue in configuring the .yml file with the project in bit bucket. Project is in sub folder of root folder and there is no article which details of how to mention sub folder path to build and execute. Can you please help me here ?

My .sln is in after two sub folders after src (root).

Structure is:
src
Folder1
Folder2
Folder3
Folder4
.sln file
And .yml file is located just under src.

JuergenGutsch commented 5 years ago

Comment written by Jürgen Gutsch on 27.03.2018 07:04:00

Hi,
I would say the .yml file should be in root, to get it running.
Did you try to set the paths like this:
./src/Folder1/Folder2/my-super-awesome.sln
(unix style)

JuergenGutsch commented 5 years ago

Comment written by J Khalaf on 24.09.2018 10:03:11

For some reason, the build fails for me. I get the following:

https://uploads.disquscdn.c...

My solution file is called Phonden.sln! Also when I locally run `dotnet build Phoneden.sln`, it builds fine!

JuergenGutsch commented 5 years ago

Comment written by Jürgen Gutsch on 24.09.2018 12:39:41

The error says the project file doesn't exist. Maybe you need to check the path to the solution file. Use the relative path to the solution file, based on the current execution folder:
E.g. If you repo has an subfolder "src" you need to call
dotnet build "src/Phoneden.sln"

JuergenGutsch commented 5 years ago

Comment written by J Khalaf on 24.09.2018 14:04:54

I get that the error says the project file doesn't exist, but I am not sure why it says that. I checked and I don't see a problem! Here's a screenshot.

https://uploads.disquscdn.c...

JuergenGutsch commented 5 years ago

Comment written by Jürgen Gutsch on 28.09.2018 14:05:47

Where ist the environment variable $Project_Name set? Does it contain the right solution name?

JuergenGutsch commented 5 years ago

Comment written by J Khalaf on 10.10.2018 20:39:21

Hi Jürgen Gutsch apologies for the delay. Yes, it is set in the

bitbucket-pipelines.yml

file. The file has this:


# This is a sample build configuration for .NET Core.
# Check our guides at https://confluence.atlassian.com/x/5Q4SMw for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.

image: microsoft/dotnet:sdk

pipelines:
default:
- step:
caches:
- dotnetcore
script: # Modify the comma`nds below to build your repository.
- export PROJECT_NAME=Phoneden
- dotnet restore
- dotnet build $PROJECT_NAME
JuergenGutsch commented 5 years ago

Comment written by tyskland on 11.10.2018 13:56:27

Great guide. I am just wondering how the Dockerfile would look like if you run the dotnet commands directly in the pipeline?

Run a step 2 with: image: atlassian/pipelines-awscli in the bitbucket-pipeline.yml file
to be able to use the AWS CLI to push and update the container in aws

And a Dockerfile something like this?

FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY . ./
EXPOSE xxx
ENTRYPOINT [“dotnet”, “xxx.dll”]

I mean instead of the standard dotnetcore Dockerfile.

JuergenGutsch commented 5 years ago

Comment written by tyskland on 12.10.2018 12:01:54

Doesn´t seem to work for me at least

JuergenGutsch commented 5 years ago

Comment written by tyskland on 15.10.2018 10:01:00

The reason is because i am trying to push it as a docker container microservice to AWS ECS

JuergenGutsch commented 5 years ago

Comment written by Jürgen Gutsch on 15.10.2018 10:13:21

Thanks for your comment :)

Maybe I don't get you right. Do you want to run the atlassian/pipelines-awscli image as micro service container on AWS? Does this make sense? This is special for the pipeline build, isn't it?

Wouldn't it make sense to create an image based on microsoft/dotnet:2.1-aspnetcore-runtime using the docket CLI in the pipeline?

JuergenGutsch commented 5 years ago

Comment written by tyskland on 15.10.2018 11:50:13

For example.
I use the pipeline to run docker build and aws cli commands for pushing the docker image to aws repository and deploy it to the microservice cluster.

All dotnet commands and pulling of the dotnet sdk and runtime images are done in the dockerfile.

You seem to everything in the bitbucket pipeline and that seems much more efficient. My way of doing takes about 4min for the pipeline to finish (and that´s with a very simple microservice without unit tests)

But I don´t get your way of doing it working with the dockerfile ( i think my take on this whole things is inefficient and illogical)

bitbucket-pipelines.yml
___________________________________
image: atlassian/pipelines-awscli
#options:
# docker: true

pipelines:
default:
- step:
script:
- echo "This script runs on all branches that don't have any specific pipeline assigned in 'branches'."
branches:
xxx*:
- step:
services:
- docker
script:
- eval $(aws ecr get-login --no-include-email | sed 's|https://||')

- docker build -t xxx .

# Tag and push my docker image to ECR
- docker tag xxx-service
- docker push xxx-service

# Register the ECS task definition and capture the version

- export IMAGE_NAME=xxx
#this is with hard memory limit
- export TASK_VERSION=$(aws ecs register-task-definition --family xxx --container-definitions "[{\"name\":\"xxx\",\"image\":\"$IMAGE_NAME\",\"portMappings\":[{\"containerPort\":80,\"hostPort\":xxx,\"protocol\":\"tcp\"}],\"memory\":128,\"essential\":true}]" | jq --raw-output '.taskDefinition.revision')

# Set ECS service to desired count 0
- aws ecs update-service --cluster xxx --service file-service --desired-count 0
# Set ECS service to desired count 1 and assign the new task-definition ## testing
- aws ecs update-service --cluster xxx --service xxx --task-definition xxx$TASK_VERSION --desired-count 1

Dockerfile
_______________________________
# Builder stage
FROM microsoft/dotnet:2.1-sdk AS builder-stage

WORKDIR /app

#Copy project from the source to the docker container filesystem
COPY xxxt.Api/*.csproj ./xxx.Api/
COPY NuGet.Config ./

#Install all Dependencies
RUN dotnet restore -nowarn:msb3202,nu1503 xxx/

#Copy all remaining files to our image
COPY . ./

#Publish our application
RUN dotnet publish xxx.Api/*.csproj -c Release -o /app/out

EXPOSE xxx

#Runtime stage
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime-stage

WORKDIR /app
COPY --from=builder-stage /app/out .

#Entry point
ENTRYPOINT [ "dotnet" ]
CMD [ "xxx.Api.dll" ]

JuergenGutsch commented 5 years ago

Comment written by tyskland on 15.10.2018 13:54:02

With your approach I could just cache dotnetcore like you did. That seems very good. But how would the Dockerfile look like with your approach?

JuergenGutsch commented 5 years ago

Comment written by tyskland on 16.10.2018 15:39:11

what do you think of my approach?

JuergenGutsch commented 5 years ago

Comment written by tyskland on 17.10.2018 11:19:05

I am trying that, let´s say i use the microsoft/dotnet:2.1-aspnetcore-runtime in the pipeline and then the same image in the Dockerfile it pulls it again. This doesn´t make sense to me

JuergenGutsch commented 5 years ago

Comment written by Jürgen Gutsch on 17.10.2018 13:42:57

Hi @tuskland:disqus
I'm not sure that I can really help you, because I'm not really a Docker expert and don't really get what you are trying to do. I just use Docker here because BitBucket Pipelines is using it to setup the build and test environments.

Maybe someone else knows a little more about Docker and is able to help you. I very much apologize for that.

JuergenGutsch commented 5 years ago

Comment written by tyskland on 19.10.2018 11:15:51

Don´t worry. It´s just me who is bad at explaining. I will try to research this some more. So hopefully I can run the commands you are using.

I will try to to use 2 steps pipeline.
Step 1: with your commands
(and somehow copy the artifact from step 1 and copy it into the docker)
Step 2: with docker build

JuergenGutsch commented 5 years ago

Comment written by jack berry on 23.10.2018 05:42:03

“I am extremely pleased with your service of aseiamicrofanance company, your suggestions have helped me get a very good home loan deal. I never believe this will come true until i risk it, it would be pleased to recommend your services to my friends, i just got a loan from them they are for real (aseiamicrofanance@gmail.com) ...''

JuergenGutsch commented 5 years ago

Comment written by Chris on 06.11.2018 19:14:48

Looks like you are missing the project name extension:
export PROJECT_NAME=Phoneden.sln

I stumbled on this thread while also dealing with my own. I also had to ensure the directory paths were perfect (mostly the correct case as Windows doesn't care, but Linux sure does).

JuergenGutsch commented 5 years ago

Comment written by tyskland on 07.11.2018 09:51:17

i ended up combining dotnet21sdk and awscli images and uploading it to docker hub. It works fine, then i run

- dotnet publish -c Release -o ../release --version-suffix=$VERSION_NUMBER

then i just build the docker image by copying the release folder in the pipeline and use the dotnet21 runtime alpine.

FROM microsoft/dotnet:2.1-aspnetcore-runtime-alpine
WORKDIR /app
COPY release ./
EXPOSE 10002
ENTRYPOINT [ "dotnet" ]
CMD [ "xxx.dll" ]

now i average the pipeline in 1m10s for a full build, incl tests, upload and deploy to aws
(edited: this is faster than taking the artifact from step 1 and using awscli image for a second step)

thanks for the better insight into the pipelines

kulashaker30 commented 4 years ago

Is it possible to install/image dotnet sdk via bitbucket pipeline?

JuergenGutsch commented 4 years ago

Hi @kulashaker30 You don't need to. Just use the official Docker image with the latest SDK