如何在声明性Jenkins文件中将输入参数作为变量传递?

时间:2019-08-08 20:56:37

标签: jenkins jenkins-pipeline jenkins-groovy

我正在尝试在我们的jenkins管道中添加一个阶段,在该阶段将要求用户从要部署的最新5个版本中进行选择。我无法将选择作为变量传递。有谁知道该怎么做?

//SETTINGS_PAGE.xaml
<Listview ItemsSource="{x:Bind settings.settings_vector, Mode=OneWay}">
    <ItemsControl.ItemTemplate>
        <DataTemplate x:DataType="local:SETTING">
            <Grid Height="40">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{x:Bind description, Mode=OneWay}" Grid.Column="0" VerticalAlignment="Center" FontSize="18"></TextBlock>
                <TextBox Text="{x:Bind value, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" FontSize="18"></TextBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</Listview>


这将引发此异常:

    stages {
        stage('User Input') {
            environment {
                jars = 'v1 v2 v3'
            }

            input {
                message "What are we deploying today?"
                ok "Deploy"
                id "tag_id"
                parameters {
                    choice(choices: ${jars}, description: 'Select a tag for this build', name: 'TAG')
                }
            }
            steps {
                echo "Deploying ${TAG}. Have a nice day."
            }
        }

我还尝试用脚本块替换环境块;步骤块中的脚本块;然后尝试在单选按钮中提示groovy.lang.MissingPropertyException: No such property: jars for class: WorkflowScript 的{​​{1}}而不是执行命令。

1 个答案:

答案 0 :(得分:1)

您可以尝试

List<String> CHOICES = [];
pipeline {
    agent any
stages {
        stage('User Input') {


            steps {

                script {
                        CHOICES = ["tag1", "tag2", "tag3"];    
                        env.YourTag = input  message: 'What are we deploying today?',ok : 'Deploy',id :'tag_id',
                                        parameters:[choice(choices: CHOICES, description: 'Select a tag for this build', name: 'TAG')]
                        }           
                echo "Deploying ${env.YourTag}. Have a nice day."
            }
        }
}
}

输出

[Pipeline] {
[Pipeline] stage
[Pipeline] { (User Input)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Input requested
Approved by Admin
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
Deploying tag2. Have a nice day.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS