Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to declare variables in yml file?

KIM JEONG JIN December 26, 2021

I set variables on "Add variables" and write it, but I was failed run pipeline.

for example,

> Add variables

Name : ACCOUNT_ID, AWS_REGION

> yml file

- IMAGE="$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/biblely-back"

> build console message

The push refers to repository [$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/biblely-back]

1 answer

1 accepted

0 votes
Answer accepted
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 28, 2021

Hi @KIM JEONG JIN,

I would like to ask a few questions so we can better help you:

1. Are the variables ACCOUNT_ID and AWS_REGION defined as Workspace variables?
Or as Repository variables? Or as Deployment variables?

2. Are these variables Secured? If you have defined them as Secured variables, then their value will not be displayed in the Pipelines log.

3. If I understand correctly, you define another variable named IMAGE in your yml file. The definition you used should work.

- Are you then using the variable IMAGE in the same pipelines step where you define it?
- Could you let us know what command in your script uses this variable, and also the full output of the command that fails? (Please feel free to sanitize any sensitive/private data from the output prior to sharing it here)

Kind regards,
Theodora

KIM JEONG JIN December 28, 2021

Thank you @Theodora Boudale 

1. I defined Repository variables.

2. It was secured, and same pipeline

3. In bitbucket-pipelines.yml, it used define and not fail under example

- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"

- error script

 ``` 

IMAGE="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/biblely-back"    <1s
+ IMAGE="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/biblely-back"
```

4. conclusion

so, I want to define like this,

- IMAGE="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com",

but it was failed.

All variables are secured.

I wonder, it was correct expression like this, when I define Repository variables on yml.

- ${ACCOUNT_ID} in script

- "${ACCOUNT_ID}" in string

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 29, 2021

Hi @KIM JEONG JIN,

Thank you for the info.

The way you declare the variable is correct. Either of the following should work:

- IMAGE="$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/biblely-back"

- IMAGE="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"


In the build log, you see the following:

IMAGE="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/biblely-back" <1s
+ IMAGE="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/biblely-back"

because the variables ACCOUNT_ID and AWS_REGION are Secured. When you have Secured variables, their values will be masked/hidden like this in the Build log.

However, their values are still used when you define IMAGE; they are masked in the build log because they are Secured variables, but their values are used in the definition of IMAGE.

You can confirm this the following way:

After you define the IMAGE variable in the yml, add another command

- echo $IMAGE > artifact.txt

and then define the file artifact.txt as an artifact in that step, e.g.

- step:
script:
- IMAGE="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/biblely-back"
- echo $IMAGE > artifact.txt
artifacts:
- artifact.txt

After you run a build, download the file artifact.txt from the tab Artifacts next to the Build log, and you should see the value of IMAGE in there. (Please keep in mind that the file artifact.txt can be downloaded by anyone with access to your repo)

Is it clear why this is occurring?

If you still have questions, please feel free to let me know.

Kind regards,
Theodora

Like KIM JEONG JIN likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events