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
I finally was able to solve the problem by the following .yml and Dockerfile configuration:
azure-pipelines.yml
Dockerfile
References:
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:
Then, your source code is checked out into directories named after the repositories as a subfolder of s in
(Agent.BuildDirectory)
. If(Agent.BuildDirectory)
isC:agent_work1
and your repositories are named RepoX and RepoY, your code is checked out toC:agent_work1sRepoX
andC: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.