

Amazon 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`區段中定義兩個變數 `VAR2``VAR1`和 。

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

下列範例說明如何定義兩個變數 `REPOSITORY-URI`和 `TIMESTAMP`，並使用 `Outputs`區段匯出它們。

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

下列範例示範如何在 中指定`VAR1`變數`MyBuildAction`，然後使用 在相同的動作中參考它`$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>

下列範例說明如何在 中指定`TIMESTAMP`變數`BuildActionA`、使用 `Outputs` 屬性匯出變數，然後使用 `BuildActionB` 在 中參考變數`${BuildActionA.TIMESTAMP}`。

```
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 主控台的**秘密**頁面上指定，才能用於工作流程定義檔案。如需詳細資訊，請參閱[使用秘密遮罩資料](workflows-secrets.md)。

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