Fix other related Azure Issues

James Hizon 60 Reputation points
2025-03-12T03:32:11.85+00:00

Hm. I am having additional issues related to "Missing environment variables." I am assuming that maybe somewhere it is because I need to add these variables inside both the build and the run phase inside my Dockerfile, given that I cannot find a slot to update this inside the Azure DevOps portal.

@RyanHill

Azure DevOps
{count} votes

Accepted answer
  1. Ryan Hill 29,936 Reputation points Microsoft Employee
    2025-03-12T22:51:02.5866667+00:00

    Hey @James Hizon

    Thanks for meeting with me offline to see your exact issue. We were able to resolve your issue by splitting your buildAndPush task into two separate steps per this troubleshooting step that referenced this stackoverflow post.

    Once we got your arguments added, you were able to successfully run your yaml pipeline.


1 additional answer

Sort by: Most helpful
  1. Suresh Chikkam 315 Reputation points Microsoft External Staff
    2025-03-12T07:44:07.17+00:00

    Hi @James Hizon ,

    I have seen the previous question conversation and providing the below response to help with the issue of missing environment variables.

    One thing you can try is passing the environment variables when defining the pipeline YAML, you can include the variables and check they are passed as build arguments to Docker. This way, when the image is built, it already has the required variables embedded. In your azure-pipelines.yml

    variables:
      DATABASE_URL: $(DATABASE_URL)
      API_KEY: $(API_KEY)
    steps:
    - task: Docker@2
      inputs:
        command: 'buildAndPush'
        repository: '<your-acr-name>.azurecr.io/<your-image-name>'
        dockerfile: '**/Dockerfile'
        containerRegistry: '<your-acr-service-connection>'
        tags: 'latest'
        arguments: '--build-arg DATABASE_URL=$(DATABASE_URL) --build-arg API_KEY=$(API_KEY)'
    

    If you haven't already, make sure that DATABASE_URL and API_KEY are actually defined in Azure DevOps under the pipeline variables section. You can find this by going into your pipeline in Azure DevOps, clicking "Edit," navigating to the "Variables" tab, and adding them there.

    enter image description here

    Add variables.

    enter image description here

    Theres is another approach using a .env file. Instead of manually passing variables in the pipeline, the build process can pull them from a file. You can generate this file dynamically in the pipeline.

    
    - script: echo "DATABASE_URL=$(DATABASE_URL)\nAPI_KEY=$(API_KEY)" > .env
    
      displayName: 'Create .env file'
    
    - task: Docker@2
    
      inputs:
    
        command: 'buildAndPush'
    
        repository: '<your-acr-name>.azurecr.io/<your-image-name>'
    
        dockerfile: '**/Dockerfile'
    
        containerRegistry: '<your-acr-service-connection>'
    
        tags: 'latest'
    
        arguments: '--env-file .env'
    
    

    If you want to modify the Dockerfile to handle this use ARG for build-time variables and ENV for runtime variables.

    
    ARG DATABASE_URL
    
    ARG API_KEY
    
    ENV DATABASE_URL=$DATABASE_URL
    
    ENV API_KEY=$API_KEY
    
    RUN echo "Database URL: $DATABASE_URL"
    
    RUN echo "API Key: $API_KEY"
    
    

    Hope it helps!


    Please do not forget to click "Accept the answer” and Yes wherever the information provided helps you, this can be beneficial to other community members.

    User's image

    If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.