How can I pass the PR_ID to a lifecycle job?

INFORMATION

Relevant information to this issue:

ISSUE

In my blueprint for my preview environment, I added a lifecycle that is just like the aws-rds-with-terraform example. But in my case instead of creating/deleting an RDS instance it creates/deletes a Cloudflare Tunnel using their Terraform provider. That part is working just fine, I’m able to create a tunnel, access policies and update the DNS CNAME.

Right now the CNAME is hard-coded, but I want to use the PR_ID as mentioned here. And that is the part I’m stuck in, how to get the PR_ID and pass it to the terraform.

In my Dockerfile I tried this:

ENV QOVERY_API_TOKEN=$TF_VAR_qovery_api_token
ENV CLOUDFLARE_ZONE=$TF_VAR_cloudflare_zone
ENV CLOUDFLARE_ZONE_ID=$TF_VAR_cloudflare_zone_id
ENV CLOUDFLARE_ACCOUNT_ID=$TF_VAR_cloudflare_account_id
ENV CLOUDFALRE_API_TOKEN=$TF_VAR_cloudflare_api_token
ENV CLOUDFLARE_ACCESS_GROUP=$TF_VAR_cloudflare_access_group

# Copy the shell script to the container
ADD get_pr_id.sh /usr/local/bin/get_pr_id.sh

# Run the shell script to get the PR ID and set it as an ENV variable
RUN chmod +x /usr/local/bin/get_pr_id.sh \
    && export PR_ID=$(get_pr_id.sh) \
    && echo "export PR_ID=$(get_pr_id.sh)" >> /etc/profile

ADD . .
RUN terraform init

ENTRYPOINT [ "/bin/sh" ]

Where get_pr_id.sh looks like:

#!/bin/bash

# Fetch the PR ID using Qovery CLI
PR_ID=$(qovery application env list -n "app" --show-values | grep "QOVERY_PROJECT_ID" | awk '{print $10}')

# Output the PR ID
echo "$PR_ID"

And I’ve had no success. I’ve looked at the Qovery Terraform Provider but I can’t get my head around how to get the PR_ID from the newly created Preview Environment.

Help would be appreciated to be able to use the PR_ID in Terraform to be able to create the DNS CNAME.

Hello @moisesrodriguez,

For every preview env created, there should be a QOVERY_PULL_REQUEST_ID built_in variable available for all your environment services. You can check in your service’s environment variables such as:

This doesn’t seem to be in our documentation, we’ll update it.

Have a nice day

Hi @Melvin_Zottola,

While we figure this one out Started with Preview Environments - Bitbucket - #6 by moisesrodriguez I went ahead and made my preview environment create automatically on every PR.

However it seems the env variable is not available for the job.

I’m using it in my dockerfile like this

And I can see that indeed the env QOVERY_PULL_REQUEST_ID does exist

but not when I need it. Can you confirm this is the case? and how can we solve it.

In case it is of any help, this is the order of my job in the pipeline

And how it’s triggered

Hey @moisesrodriguez,

Happy to hear that the env preview succeeded.

The environment variable should be available for any service, including your job.
Looking at your current job deployed in your cluster, the variable is well present in the env:
image

Did you try locally this terraform command ? According to the terraform output, setting the branch variable value through -var may work ?

Hi @Melvin_Zottola ,

Yes I tried locally the command and it works as is. That’s why I’m curious why I got that error, because if the env variable is available when the terraform ran, then it shouldn’t have given me that error.

Out of curiosity, could you do a test to verify it is related to the QOVERY_PULL_REQUEST_ID ? (I’m not sure it’s related)

Can you edit your job Dockerfile to replace

key=branch-${QOVERY_PULL_REQUEST_ID}/terraform.tfstate

by

key=branch-XXX/terraform.tfstate

where XXX is your current PR id for this preview env.

Hi @Melvin_Zottola,

When I run it locally it correctly creates the Terraform state file in my S3 backend with whatever I put in there. Also when I clone manually my environment as mentioned here Environment Preview Cloning Error - #2 by Melvin_Zottola , that same step instead of using

key=branch-${QOVERY_PULL_REQUEST_ID}/terraform.tfstate

I use

key=branch-${BITBUCKET_BRANCH}/terraform.tfstate

And it works just fine. Only when using the builtin preview feature, I get the error mentioned in this topic.

The variable ${QOVERY_PULL_REQUEST_ID} I don’t use it only for the Terraform state file name, I use too for creating a CNAME record in my DNS and that is when I get the error.

Here is another example of the job not getting the QOVERY_PULL_REQUEST_ID

LINK

Hi @Melvin_Zottola,

I figured it out. Apparently Terraform expects env variables to be named TF_VAR_xxx, so I had to use variable interpolation to make it work. I did this

and now it works as expected.

2 Likes

Well done @moisesrodriguez, thanks for the details :+1:

Note that you can also create directly an alias on QOVERY_PULL_REQUEST_ID instead of an interpolated variable.

1 Like

@Melvin_Zottola I cannot use an ALIAS because the variable QOVERY_PULL_REQUEST_ID does not exist in my blueprint environment since it’s not an environment from a Pull Request. But using variable interpolation did the trick because in the PR environment it does exist.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.