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.
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:
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:
Did you try locally this terraform command ? According to the terraform output, setting the branch variable value through -var may work ?
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.
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
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.
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
@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.