Search code examples
phpazure-devopsazure-pipelines

Trying to pass prompt value into the code in Azure DevOps


I am trying to get userStory value from user while running build pipeline in Azure DevOps. I want to use that value as a variable into the php script that I am building. I tried below code with yaml, Also attached is the error.

trigger:
- main

pool:
  vmImage: ubuntu-latest

variables:
  phpVersion: 8.1

parameters:
  - name: UserStory
    displayName: Enter User Story ID.
    type: string
    default: ''

steps:
- script: |
    sudo update-alternatives --set php /usr/bin/php$(phpVersion)
    php -version
  displayName: 'Use PHP version $(phpVersion)'

- script: |
    echo "Running script for user story number $(UserStory)"
    export USER_STORY=$(UserStory)
    ls -la
    php index.php
  displayName: 'Run index.php with user story ID'

index.php

    <html>
     <head>
       <title>PHP Test</title>
     </head>
     <body>
     <?php 
       $user_story1 = getenv('User_Story');
       echo '<p>Hello World</p>'; 
       echo "<p>User Story Number: $user_story1</p>";
     ?> 
     </body>
    </html>

Error:

Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/abb29e36-8507-4480-aae8-c2995d6af1c8.sh
/home/vsts/work/_temp/abb29e36-8507-4480-aae8-c2995d6af1c8.sh: line 1: UserStory: command not found
/home/vsts/work/_temp/abb29e36-8507-4480-aae8-c2995d6af1c8.sh: line 2: UserStory: command not found
Running script for user story number 
total 24
drwxr-xr-x 3 vsts docker 4096 Oct  8 07:26 .
drwxr-xr-x 6 vsts docker 4096 Oct  8 07:26 ..
drwxr-xr-x 8 vsts docker 4096 Oct  8 07:26 .git
-rw-r--r-- 1 vsts docker  985 Oct  8 07:26 README.md
-rw-r--r-- 1 vsts docker  518 Oct  8 07:26 azure-pipelines.yml
-rw-r--r-- 1 vsts docker  210 Oct  8 07:26 index.php
<html>
 <head>
   <title>PHP Test</title>
</head>
 <body>
 <p>Hello World</p><p>User Story Number: </p> 
 </body>
</html>

Finishing: Run index.php with user story ID

Solution

  • PHP variable names are case-sensitive. You are trying to read User_Story in your php scripts, but define USER_STORY in the pipeline. In your index.php, read environment variable using $user_story1 = getenv('USER_STORY');.

    <html>
    <head>
      <title>PHP Test</title>
    </head>
    <body>
      <?php 
        $user_story1 = getenv('USER_STORY');
        echo '<p>Hello World</p>'; 
        echo "<p>User Story Number: $user_story1</p>";
      ?> 
    </body>
    </html>
    
    

    Besides, in your Azure YAML file, read parameters using ${{ }}.

    - script: |
        echo "Running script for user story number ${{parameters.UserStory}}"
        ls -la
        export USER_STORY=${{parameters.UserStory}}
        php index.php
      displayName: 'Run index.php with user story ID'
    

    Result:

    enter image description here