Skip to content

Instantly share code, notes, and snippets.

@brbarnett
Created January 17, 2019 13:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brbarnett/c55c80dd63b89465cfd9bc6b74c0548e to your computer and use it in GitHub Desktop.
Save brbarnett/c55c80dd63b89465cfd9bc6b74c0548e to your computer and use it in GitHub Desktop.
Accessing Azure Artifacts from a docker container in a Pipelines build
resources:
- repo: self
queue:
name: Hosted Ubuntu 1604
variables:
- group: Artifacts
steps:
- task: Docker@1
displayName: 'Build an image'
inputs:
azureSubscriptionEndpoint: 'DemoSPNConnection'
azureContainerRegistry: demo.azurecr.io
arguments: '--build-arg ARTIFACTS_ENDPOINT=$(artifactsEndpoint) --build-arg ACCESS_TOKEN=$(artifactsAccessToken)'
- task: Docker@1
displayName: 'Push an image'
inputs:
azureSubscriptionEndpoint: 'DemoSPNConnection'
azureContainerRegistry: demo.azurecr.io
command: 'Push an image'
trigger:
batch: true
branches:
include:
- master
paths:
exclude:
- readme.md
resources:
- repo: self
queue:
name: Hosted Ubuntu 1604
variables:
- group: Artifacts
steps:
- task: Docker@1
displayName: 'Build an image'
inputs:
azureSubscriptionEndpoint: 'DemoSPNConnection'
azureContainerRegistry: demo.azurecr.io
arguments: '--build-arg ARTIFACTS_ENDPOINT=$(artifactsEndpoint) --build-arg ACCESS_TOKEN=$(artifactsAccessToken)'
trigger: none
FROM microsoft/dotnet:2.2.100-sdk AS build-env
WORKDIR /app
# Credit: https://stackoverflow.com/questions/53419491/azure-artifacts-gives-unauthorized-when-trying-to-build-dockerfile/53510966#53510966
# Personal access token to access Artifacts feed
ARG ACCESS_TOKEN
ARG ARTIFACTS_ENDPOINT
# Install the Credential Provider to configure the access
RUN wget -qO- https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh | bash
# Configure the environment variables
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS "{\"endpointCredentials\": [{\"endpoint\":\"${ARTIFACTS_ENDPOINT}\", \"password\":\"${ACCESS_TOKEN}\"}]}"
# copy everything and build the project
COPY ./src ./
RUN dotnet restore -s ${ARTIFACTS_ENDPOINT} api/*.csproj
RUN dotnet publish api/*.csproj -c Release -o out
# build runtime image
FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/api/out ./
ENTRYPOINT ["dotnet", "api.dll"]
EXPOSE 443
@shoaib-masood
Copy link

how the processes of build works in terms of containerized application because in our case it is magneto image which is 1.6GB and building that size of image is taking too much time how we can overcome this?

@lkurzyniec
Copy link

Instead of creating and using Access Token you can use NuGetAuthenticate task and then use VSS_NUGET_ACCESSTOKEN env variable.

  - task: NuGetAuthenticate@0

  - task: Docker@2
    inputs:
      ...
      arguments: '--build-arg ACCESS_TOKEN=$(VSS_NUGET_ACCESSTOKEN)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment