尝试将提示值传递到 Azure DevOps 中的代码中

问题描述 投票:0回答:1

我试图在 Azure DevOps 中运行构建管道时从用户那里获取 userStory 值。 我想将该值用作我正在构建的 php 脚本中的变量。我用yaml尝试了下面的代码,还附上了错误。

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>

错误:

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
php azure-devops azure-pipelines
1个回答
0
投票

PHP 变量名称区分大小写。您尝试在 php 脚本中读取

User_Story
,但在管道中定义
USER_STORY
。在您的
index.php
中,使用
$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>

此外,在 Azure YAML 文件中,使用

${{ }}
读取参数。

- 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'

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.