0
votes

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"]
1

1 Answers

2
votes

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!