skip to Main Content

I’m currently dealing with a problem within my Azure DevOps pipeline where I’m trying to run and build a pipeline for repo ‘RepoX’, whereafter the Dockerfile tries to use the ‘.csproj’ of repo ‘RepoY’. However, ‘RepoY’ is added as ‘existing project reference’ to ‘RepoX’ within Visual Studio. When the dockerfile tries to use the .csproj of repo ‘Y’ I get a message that this file cannot be found. This is true, since both are located in different repositories, thus are separate projects. How can I make sure RepoX clones repo Y somehow or is able to use RepoY, so that the Dockerfile finds the .csproj of repo Y and builds it in order to push it to, for example Docker hub?

Projects overview

Azure Repo: RepoX
  ------ [RepoX directory]
  ------------ Controllers
  ------------ Properties
  ------------ appsettings.Development.json
  ------------ appsettings.json
  ------------ Program.cs
  ------------ RepoX.csproj
  ------ .dockerignore
  ------ .gitignore
  ------ azure-pipelines.yml
  ------ Dockerfile
  ------ RepoX.sln

Azure Repo: RepoY (Class Library)
  ------ [CoreClasses directory]
  ------------ ClassA
  ------------ ClassB

I tried to use checkout to retrieve the repoY in repoX, but somehow this is not persistent when the Dockerfile is trying to search the .csproj of repoY?

azure-pipelines.yml:

trigger:
- main

# resources:
# - repo: self <---------------- tried to turn this off as well, makes no difference...

variables:
  tag: '$(Build.BuildId)'

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: ubuntu-latest
    - task: Docker@2
      - checkout: self
        path: s/CheckOutFolder
      - checkout: git://ProjectA/RepoY
        path: s/CheckOutFolder/RepoX/RepoY
      - script: |
          ls
      displayName: Build an image
      inputs:
        command: build
        dockerfile: '$(Build.SourcesDirectory)/CheckOutFolder/Dockerfile'
        tags: |
          $(tag)

Dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["RepoX/RepoX.csproj", "RepoX/"]
RUN dotnet restore "RepoX/RepoX.csproj"
#RUN ls ../home/vsts/work/1/s/                                  <----- says no such directory exists...
#RUN ls ../home/vsts/work/1/s/CheckOutFolder/RepoX/RepoY        <----- says no such directory exists...
#RUN ls RepoX                                   <----- does not contain files of RepoY, although I used checkout? I don't get it..

COPY ["RepoX/RepoY/RepoY.csproj", "RepoX/"]                 <----- says cannot find file...
RUN dotnet restore "RepoX/RepoY.csproj"

COPY . .

WORKDIR "/src/RepoX"
RUN dotnet build "RepoX.csproj" -c Release -o /app/build

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

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

Can someone please help me with this?

2

Answers


  1. Chosen as BEST ANSWER

    I finally was able to solve the problem by the following .yml and Dockerfile configuration:

    azure-pipelines.yml

    trigger:
    - main
    resources:
    - repo: self
    variables:
      tag: '$(Build.BuildId)'
    stages:
    - stage: Build
      displayName: Build image
      jobs:
      - job: Build
        displayName: Build
        pool:
          vmImage: ubuntu-latest
        steps:
        # Copy current RepoX and RepoY projects
        # to a new (root) folder
        - checkout: self
        # change <MyProject> including removing the open/closed angled brackets to your project name
        - checkout: git://<MyProject>/RepoY
        - task: Docker@2
          displayName: Build an image
          inputs:
            command: build
            dockerfile: 'RepoX/Dockerfile'
            useDefaultContext: false
            # /root folder (contains: RepoX and RepoY)
            # Should be different from where the Dockerfile is located, 
            # because the build context should be placed on a higher 
            # level of directory structure to be able to access 
            # 'RepoY' project folder.
            buildContext: $(Build.Repository.LocalPath)
            tags: |
              $(tag)
    

    Dockerfile

    #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    WORKDIR /src
    
    # Copy RepoX and RepoY projects that are checked out in azure-pipeline.yml to /src
    COPY . .
    
    # Not needed to copy RepoX project to a new directory.. just restore the project.
    #COPY ["RepoX/RepoX/RepoX.csproj", "RepoX/"]
    RUN dotnet restore "RepoX/RepoX/RepoX.csproj"
    
    WORKDIR "/src/RepoX/RepoX"
    RUN dotnet build "RepoX.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "RepoX.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "RepoX.dll"]
    

    References:


  2. If RepoX and RepoY are in different project but in the same organization, you could use Inline syntax checkout to download those two repos like below:

    steps:
    - checkout: self
    - checkout: git://MyProject/RepoY # Azure Repos Git repository in the same organization
    

    Then, your source code is checked out into directories named after the repositories as a subfolder of s in (Agent.BuildDirectory). If (Agent.BuildDirectory) is C:agent_work1 and your repositories are named RepoX and RepoY, your code is checked out to C:agent_work1sRepoX and C:agent_work1sRepoY. Please define the right directory for your docker file. Here is the reference document: Inline syntax checkout.

    Update:

    You could add a copy file task to copy the .csproj file from RepoY to the folder where the dockerfile is located or even deeper by using match patterns. Here is an YAML example like below.

    - task: CopyFiles@2
      inputs:
        SourceFolder: 'C:agent_work1sRepoY'
        Contents: '**.csproj'
        TargetFolder: 'C:agent_work1sRepoX...'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search