

亚马逊 CodeCatalyst 不再向新买家开放。现有客户可以继续正常使用该服务。有关更多信息，请参阅 [如何从中迁移 CodeCatalyst](migration.md)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 变量示例
<a name="workflows-working-with-variables-ex"></a>

以下示例演示了如何在工作流定义文件中定义和引用变量。

有关变量的更多信息，请参阅[在工作流中使用变量](workflows-working-with-variables.md)。

**Topics**
+ [示例：使用 Inputs 属性定义变量](#workflows-working-with-variables-ex-define-inputs)
+ [示例：使用 Steps 属性定义变量](#workflows-working-with-variables-ex-define-steps)
+ [示例：使用 Outputs 属性导出变量](#workflows-working-with-variables-ex-export-outputs)
+ [示例：引用在同一操作中定义的变量](#workflows-working-with-variables-ex-refer-current)
+ [示例：引用在其他操作中定义的变量](#workflows-working-with-variables-ex-refer-other)
+ [示例：引用密钥](#workflows-working-with-variables-ex-refer-secret)

## 示例：使用 Inputs 属性定义变量
<a name="workflows-working-with-variables-ex-define-inputs"></a>

以下示例向您演示了如何在 `Inputs` 部分中定义两个变量 `VAR1` 和 `VAR2`。

```
Actions:
  Build:
    Identifier: aws/build@v1
    Inputs:
      Variables:
      - Name: VAR1
        Value: "My variable 1"
      - Name: VAR2
        Value: "My variable 2"
```

## 示例：使用 Steps 属性定义变量
<a name="workflows-working-with-variables-ex-define-steps"></a>

以下示例向您演示了如何在 `Steps` 部分中显式定义 `DATE` 变量。

```
Actions:
  Build:
    Identifier: aws/build@v1
    Configuration:    
      Steps:
        - Run: DATE=$(date +%m-%d-%y)
```

## 示例：使用 Outputs 属性导出变量
<a name="workflows-working-with-variables-ex-export-outputs"></a>

以下示例向您演示了如何使用 `Outputs` 部分定义两个变量 `REPOSITORY-URI` 和 `TIMESTAMP` 并导出它们。

```
Actions:
  Build:
    Identifier: aws/build@v1
    Inputs:
      Variables:
        - Name: REPOSITORY-URI
          Value: 111122223333.dkr.ecr.us-east-2.amazonaws.com/codecatalyst-ecs-image-repo
    Configuration:
      Steps:
        - Run: TIMESTAMP=$(date +%m-%d-%y-%H-%m-%s) 
    Outputs:
      Variables:
        - REPOSITORY-URI
        - TIMESTAMP
```

## 示例：引用在同一操作中定义的变量
<a name="workflows-working-with-variables-ex-refer-current"></a>

以下示例向您演示了如何在 `MyBuildAction` 中指定变量 `VAR1`，然后使用 `$VAR1` 在同一个操作中引用该变量。

```
Actions:
  MyBuildAction:
    Identifier: aws/build@v1
    Inputs:
      Variables:
        - Name: VAR1
          Value: my-value
    Configuration:
      Steps:
        - Run: $VAR1
```

## 示例：引用在其他操作中定义的变量
<a name="workflows-working-with-variables-ex-refer-other"></a>

以下示例向您演示了如何在 `BuildActionA` 中指定变量 `TIMESTAMP`，使用 `Outputs` 属性导出变量，然后使用 `${BuildActionA.TIMESTAMP}` 在 `BuildActionB` 中进行引用。

```
Actions:
  BuildActionA:
    Identifier: aws/build@v1
    Configuration:    
      Steps:
        - Run: TIMESTAMP=$(date +%m-%d-%y-%H-%m-%s) 
    Outputs:
      Variables:
        - TIMESTAMP
  BuildActionB:
    Identifier: aws/build@v1
    Configuration:
      Steps:
        - Run: docker build -t my-ecr-repo/image-repo:latest .      
        - Run: docker tag my-ecr-repo/image-repo:${BuildActionA.TIMESTAMP}
        
        # Specifying just '$TIMESTAMP' here will not work 
        # because TIMESTAMP is not a variable 
        # in the BuildActionB action.
```

## 示例：引用密钥
<a name="workflows-working-with-variables-ex-refer-secret"></a>

以下示例演示如何引用 `my-password` 密钥。`my-password` 是密钥的键。此密钥的密钥和相应的密码值必须先在 CodeCatalyst 控制台的 S **ec** rets 页面上指定，然后才能在工作流程定义文件中使用。有关更多信息，请参阅 [使用密钥遮蔽数据](workflows-secrets.md)。

```
Actions:
  BuildActionA:
    Identifier: aws/build@v1
    Configuration:    
      Steps:
        - Run: curl -u LiJuan:${Secrets.my-password} https://example.com
```