我在 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'
索引.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 变量名区分大小写。您尝试
User_Story
在 php 脚本中读取,但USER_STORY
在管道中定义。在您的 中index.php
,使用 读取环境变量$user_story1 = getenv('USER_STORY');
。此外,在您的 Azure YAML 文件中,使用 读取参数
${{ }}
。结果: