6
votes

I am trying to install a pip package from Azure Artifacts as part of a Docker image(with Docker@2 task) but whatever I try does not work.

It looks like my pip inside Docker cannot authenticate against Azure Artifacts whatever I try. Closest I got is with

RUN pip install keyring artifacts-keyring
ENV ARTIFACTS_KEYRING_NONINTERACTIVE_MODE true
RUN pip install <> --index-url https://pkgs.dev.azure.com/<>/_packaging/<>/pypi/simple/

but in my Azure devops, i keep getting

ERROR: Could not find a version that satisfies the requirement <> (from versions: none)
ERROR: No matching distribution found for <>

Also - Azure documentation on this seems to very poor, if I switch ENV ARTIFACTS_KEYRING_NONINTERACTIVE_MODE false it prompts my Azure DevOps pipeline to authenticate intercatively which is not what I want.

How can I install a Python package published in Azure Artifacts as part of my Azure Pipeline Docker task automatically?

2

2 Answers

9
votes

How can I install a Python package published in Azure Artifacts as part of my Azure Pipeline Docker task automatically?

We could use the PipAuthenticate task to populates the PIP_EXTRA_INDEX_URL environment variable:

It authenticates with your artifacts feed and per the docs, will store the location of a config file that can be used to connect in the PYPIRC_PATH environment variable.

Then pass it in the build arg:

arguments: --build-arg INDEX_URL=$(PIP_EXTRA_INDEX_URL)

You could check this document Consuming Azure Pipelines Python artifact feeds in Docker for some more details.

Hope this helps.

5
votes

To add to the accepted answer, here is a somewhat more complete code example:

azure-pipelines.yml

- task: PipAuthenticate@1
  inputs:
    artifactFeeds: 'my_artifacts_feed'
    # 'onlyAddExtraIndex' populates PIP_EXTRA_INDEX_URL env variable
    onlyAddExtraIndex: True

- task: Docker@2
  displayName: 'Build Docker Image'
  inputs:
    command: build
    dockerfile: $(dockerfilePath)
    tags: |
      $(tag)
    arguments: --build-arg INDEX_URL=$(PIP_EXTRA_INDEX_URL)

Dockerfile

FROM python:3.8-buster

# add an URL that PIP automatically searches (e.g., Azure Artifact Store URL)
ARG INDEX_URL
ENV PIP_EXTRA_INDEX_URL=$INDEX_URL

COPY requirements.txt
RUN pip install -r requirements.txt