bauca / PythonAPIAzureQuickstart

A quickstart template for building an API written in Python + FastAPI and deploying it to the Azure Cloud with CI/CD.
MIT License
4 stars 0 forks source link

Build pipeline failed with azure #5

Open yaoyaoTheCat opened 2 years ago

yaoyaoTheCat commented 2 years ago

Hi, Thanks for sharing. I have tried to setup the build pipeline in azure with azure container registery and get below error:

Step 6/19 : RUN poetry install
 ---> Running in f06e7d5e44e6

  RuntimeError

  Poetry could not find a pyproject.toml file in /backend or its parents

  at /usr/local/lib/python3.10/site-packages/poetry/core/factory.py:369 in locate
      365│             if poetry_file.exists():
      366│                 return poetry_file
      367│ 
      368│         else:
    → 369│             raise RuntimeError(
      370│                 "Poetry could not find a pyproject.toml file in {} or its parents".format(
      371│                     cwd
      372│                 )
      373│             )
The command '/bin/sh -c poetry install' returned a non-zero code: 1
##[error]The command '/bin/sh -c poetry install' returned a non-zero code: 1
##[error]The process '/usr/bin/docker' failed with exit code 1
Finishing: Build and push an image to container registry

My azure-pipelines.yml file looks like this ( I haven't figure out how to add the unit tests and deploy to the tasks yet):

trigger:
- main

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'xxxxxx'
  imageRepository: 'someimg'
  containerRegistry: 'somereporeg.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/deploy/docker/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)

any reason why? Thank you

bauca commented 2 years ago

Hi, It looks like while trying to build the image, the context is missing (docker can't find the Python files and the poetry config file), I believe adding the 'buildContext' argument, pointing to the correct path, should do the job. Give it a try:

trigger:
- main

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'xxxxxx'
  imageRepository: 'someimg'
  containerRegistry: 'somereporeg.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/deploy/docker/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        buildContext: $(Build.SourcesDirectory)
        tags: |
          $(tag)
bauca commented 2 years ago

Additionally: As you are using the Azure Container Registry, you'll also need to log into it to push you images there, before your buildAndPush task, you should add the login task, and reference your service connection.

steps:
- task: Docker@2
  displayName: Login to ACR
  inputs:
    command: login
    containerRegistry: dockerRegistryServiceConnection1
yaoyaoTheCat commented 2 years ago

Hi, It looks like while trying to build the image, the context is missing (docker can't find the Python files and the poetry config file), I believe adding the 'buildContext' argument, pointing to the correct path, should do the job. Give it a try:

trigger:
- main

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'xxxxxx'
  imageRepository: 'someimg'
  containerRegistry: 'somereporeg.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/deploy/docker/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        buildContext: $(Build.SourcesDirectory)
        tags: |
          $(tag)

Thanks I have tried, but still got the same error.

Installing collected packages: webencodings, pylev, ptyprocess, msgpack, lockfile, distlib, urllib3, tomlkit, six, shellingham, pyparsing, pycparser, poetry-core, platformdirs, pkginfo, pexpect, pastel, jeepney, idna, filelock, crashtest, charset-normalizer, certifi, cachy, virtualenv, requests, packaging, html5lib, clikit, cffi, requests-toolbelt, cryptography, cleo, cachecontrol, SecretStorage, keyring, poetry
Successfully installed SecretStorage-3.3.2 cachecontrol-0.12.11 cachy-0.3.0 certifi-2022.6.15 cffi-1.15.1 charset-normalizer-2.1.0 cleo-0.8.1 clikit-0.6.2 crashtest-0.3.1 cryptography-37.0.4 distlib-0.3.5 filelock-3.7.1 html5lib-1.1 idna-3.3 jeepney-0.8.0 keyring-23.7.0 lockfile-0.12.2 msgpack-1.0.4 packaging-20.9 pastel-0.2.1 pexpect-4.8.0 pkginfo-1.8.3 platformdirs-2.5.2 poetry-1.1.14 poetry-core-1.0.8 ptyprocess-0.7.0 pycparser-2.21 pylev-1.4.0 pyparsing-3.0.9 requests-2.28.1 requests-toolbelt-0.9.1 shellingham-1.4.0 six-1.16.0 tomlkit-0.11.1 urllib3-1.26.10 virtualenv-20.15.1 webencodings-0.5.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 22.0.4; however, version 22.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 47cff53bc6e3
 ---> 76fcaf310cd6
Step 6/19 : RUN poetry install
 ---> Running in 0e0b879b32b4

  RuntimeError

  Poetry could not find a pyproject.toml file in /backend or its parents

  at /usr/local/lib/python3.10/site-packages/poetry/core/factory.py:369 in locate
      365│             if poetry_file.exists():
      366│                 return poetry_file
      367│ 
      368│         else:
    → 369│             raise RuntimeError(
      370│                 "Poetry could not find a pyproject.toml file in {} or its parents".format(
      371│                     cwd
      372│                 )
      373│             )
The command '/bin/sh -c poetry install' returned a non-zero code: 1
##[error]The command '/bin/sh -c poetry install' returned a non-zero code: 1
##[error]The process '/usr/bin/docker' failed with exit code 1
Finishing: Build and push an image to container registry

Below is the content of the Dockerfile:

FROM python:3.10.4-slim-bullseye

RUN mkdir backend
WORKDIR /backend
COPY . /backend
RUN pip install poetry
RUN poetry install 
EXPOSE 80
ENTRYPOINT ["poetry","run","uvicorn","api.main_api:app","--host","0.0.0.0", "--port", "80"]
yaoyaoTheCat commented 2 years ago

Additionally: As you are using the Azure Container Registry, you'll also need to log into it to push you images there, before your buildAndPush task, you should add the login task, and reference your service connection.

steps:
- task: Docker@2
  displayName: Login to ACR
  inputs:
    command: login
    containerRegistry: dockerRegistryServiceConnection1

I'm using "Azure DevOps Organization" with service connections configured to connect to my azure subscription and container registry. do I still need to add the login step?
Thanks again!

yaoyaoTheCat commented 2 years ago

This is the project file structure Screenshot 2022-07-24 at 21 50 46