1

I am using Azure Pipelines and have successfully built a pipeline to build and deploy a docker image to azure app services.

Now I am trying to figure out how to build a docker image in a pipeline and then publish it to the artifact staging directory so I can push it to the azure container registry instead. I have looked all over google to see how this is done with no luck, this is going to be a three part question.

I am successfully building a dockerfile in my pipeline with the exception that I can not figure out how to delete a temporary file (keep getting access denied)

Removing intermediate container 543fb86650c4
 ---> 3ccb8c3a555e
Step 10/28 : RUN del -f "./NuGet.Config"
 ---> Running in c2ba7a3266a4
C:\src\NuGet.Config
Access is denied.

The image build continues from there and succeeds but I am trying to publish an artifact, I want to take the built image and pass it to the release pipeline to be pushed to azure container registry. The publish artifact fails because it cant find the image, and I am really not sure how to find it?

##[warning]Directory 'd:\a\1\a' is empty. Nothing will be added to build artifact 'Email'.

Maybe I am going about this all wrong? If so, please suggest an alternative, just remember I want to push my docker image to the azure container registry instead of the azure app service.

Question 1: How can I delete the file within the container in the pipeline task?

Question 2: How do I push a docker image to the Azure container registry instead of the Azure app service?

I am using the UI to configure my pipeline and do not have a full yaml file to share but here is the yaml from the view option:

pool:
  name: Azure Pipelines
variables:
  Parameters.ArtifactName: 'Email'

steps:
- task: Docker@2
  displayName: 'Build an image'
  inputs:
    containerRegistry: 'Azure Container'
    repository: email
    command: build
    Dockerfile: Services/Email/Email/Dockerfile
    buildContext: Services/Email
    tags: latest
    arguments: '-o $(build.artifactstagingdirectory)'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
    ArtifactName: '$(Parameters.ArtifactName)'

Question 3: Once I get the file to publish to the artifact staging directory how would I access it to push the image to the container registry?

UPDATE TO INCLUDE DOCKERFILE

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /src
COPY ["Email/Email.csproj", "Email/"]
COPY ./NuGet.Config ./
RUN dotnet restore "Email/Email.csproj" --configfile ./NuGet.Config
RUN del "./NuGet.Config"
COPY . .
WORKDIR "/src/Email"
RUN dotnet build "Email.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Email.dll"]
Devnsyde
  • 1,297
  • 1
  • 13
  • 34
  • Please follow this link https://learn.microsoft.com/en-us/azure/devops/pipelines/ecosystems/containers/acr-template?view=azure-devops – Vivek Kumar Feb 15 '20 at 06:28

1 Answers1

3

How can I delete the file within the pipeline task?

You can use Delete Files task to delete files or folders from the agent working directory. For below example.

 - task: DeleteFiles@1
   displayName: 'Delete files'
   inputs:
     SourceFolder: '$(Build.SourcesDirectory)'
     Contents: NuGet.Config

The commands in dockerfile are executed in the containers, delete files command cannot delete the files on local drive. For deleting files within the container you can check the methods discussed in this thread.

Update:

2,How do I push the docker image build over to the release pipeline?

You can run all docker commands in a powershell task. So you can build your docker image in the powershell task and use docker image save command to save it to a place. Then publish it to use PublishBuildArtifacts task. In this way you donot need to use docker tasks

Please check below example:

- powershell: |
   docker build . -t python/test:tag1
   docker image save python/test:tag1 -o $(Build.ArtifactStagingDirectory)/imagetest.tar

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'

In above powershell task. I first used docker build command to build the image and tag it python/test:tag1, then i used docker image save to save the image to $(Build.ArtifactStagingDirectory)/imagetest.tar. Then i can publish it use PublishBuildArtifacts task.

Update:

How can I delete the file within the container in the pipeline task.

You can specify the Administrator to del the files inside a container to fix the access denied error. Add USER Administrator in your dockerfile before RUN del command. Check below example:

USER Administrator
RUN del remove.txt

Hope above helps!

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Thank you for the thorough explanation. The reason I want to split the build from the push is so in the build pipeline I can build and in the release pipeline I can push to the container registry. I am going to give this a try today and see if it solves my problems. – Devnsyde Feb 17 '20 at 15:35
  • Ok I tried your answer on the delete question. My Question is how to delete that file from my container, it works from VisualStudio when I release to the container registry but doesnt work in the pipeline. I followed the link you gave for deleting in a container and the only thing that seemed to fit was that I was using a force flag. I removed the force flag and I am getting the exact same error: `Step 10/28 : RUN del "./NuGet.Config" ---> Running in 30d7456f281f C:\src\NuGet.Config Access is denied.` – Devnsyde Feb 17 '20 at 16:08
  • As for the second part I want to build and push in two seperate commands because I want to build in the build pipeline and then I want to push in the release pipeline. I was using the publish artifact because I need to give something to the release pipeline. How do I push the docker image build over to the release pipeline? – Devnsyde Feb 17 '20 at 16:10
  • Hi @Devnsyde you can use run `docker build` command to build your image in the powershell task, instead of building the image using docker task. And using `docker image save` to save the image to $(build.artifactstagingdirectory). Then you can use publish build artifact task and you can use the image in the release pipeline. – Levi Lu-MSFT Feb 18 '20 at 02:04
  • Hi @Devnsyde I have updated above answer, Please check it out. – Levi Lu-MSFT Feb 18 '20 at 02:07
  • This looks like it could work. I am having some issues with pathing to my csproj that I am working through. I'll let you know. Thanks again! – Devnsyde Feb 19 '20 at 15:39
  • Alright it works all the way through and is now pushing to the ACR however I am still getting the access denied when trying to delete the nuget config file from the dockerfile `Step 10/19 : RUN del "./NuGet.Config" ---> Running in e3c193e73f50 C:\src\NuGet.Config Access is denied. ` – Devnsyde Feb 19 '20 at 18:33
  • What is your base image? Have you tried use `rm` or `find /path -delete` – Levi Lu-MSFT Feb 20 '20 at 01:55
  • I'll update my question with my dockerfile when I get a chance. I use `RUN del "..."`and it works on my local machine just will not work in azure devops. My base image is windows 1809. – Devnsyde Feb 21 '20 at 06:05
  • Hi @Levi Lu-MSFT I have included my dockerfile in my original question, the container is windows so I use `DEL` instead of `RM` and I am still getting access denied when the line runs in devops, Everything else is working and I am successfully pushing images to the Azure Container Registry. I am waiting on the fix for the access denied before accepting this answer as the solution. – Devnsyde Feb 26 '20 at 20:03
  • 2
    Hi @Devnsyde Sorry for the late reply. I finally found the fix, you can specify the administrator user to execute the del command by adding `USER administrator` before `RUN del`. Please check my update. – Levi Lu-MSFT Feb 27 '20 at 05:05
  • Hi @Levi Lu-MSFT That did the trick! Thank you so much for all of your help, I **really** appreciate it! – Devnsyde Feb 28 '20 at 16:54