Create environment variable as file using Qovery API

Hi guys,

I’m trying to use this API method:

curl --location 'https://api.qovery.com/application/:applicationId/environmentVariable' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Token TOKEN' \
--data '{
  "key": "NGINX_CONFIG_FILE",
  "value": "...",
  "mount_path": "/etc/nginx/nginx.conf"
}'

to create an environment variable as file, but I’m struggling to load the file contents to the “value” property in the JSON body of the request.

I saw in the Qovery web console that the file contents get transformed to something like this:

"value": "user nginx;\nworker_processes auto;\n#pid /run/nginx.pid;\n\nevents {\n\t#worker_connections 4096;\n\tmulti_accept on;\n}\n\nhttp {\n\n\t##\n\t# Basic Settings\n\t##\n\n\tsendfile on;\n\ttcp_nopush on;\n\ttcp_nodelay on;\n\tkeepalive_timeout 90;\n\ttypes_hash_max_size 2048;\n\tserver_tokens off;\n\n  client_max_body_size 5120M;\n\t# server_names_hash_bucket_size 64;\n\t# server_name_in_redirect off;\n\n\tinclude /etc/nginx/mime.types;\n\tdefault_type application/octet-stream;\n\n\t##\n\t# SSL Settings\n\t##\n\n\tssl_protocols TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE\n\tssl_prefer_server_ciphers on;\n\n\t##\n\t# Logging Settings\n\t##\n\n\tlog_format   main '$remote_addr - $remote_user [$time_local]  $status '\n    '\"$request\" $body_bytes_sent \"$http_referer\" '\n    '\"$http_user_agent\" \"$http_x_forwarded_for\" '\n\t  '$connection_requests - $connection_time - $request_time';\n\n\taccess_log /var/log/nginx/access.log main;\n\terror_log  /var/log/nginx/error.log;\n\n\t##\n\t# Gzip Settings\n\t##\n\n\tgzip on;\n\n\tgzip_vary on;\n\tgzip_proxied any;\n\tgzip_comp_level 6;\n\tgzip_buffers 16 8k;\n\tgzip_http_version 1.1;\n\tgzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;\n\n\t##\n\t# Virtual Host Configs\n\t##\n\tupstream app {\n\t\tserver localhost:8080;\n\t}\n\n\tupstream grpc {\n\t\tserver localhost:8081;\n\t}\n\n  server {\n    listen 80;\n\n    server_name getduna.com;\n\n    include /etc/nginx/common/cloudfront.conf ;\n\n    location = /favicon.ico {\n      return 204;\n      access_log     off;\n      log_not_found  off;\n    }\n\n    # grpc\n    location /grpc/ {\n      proxy_redirect     off;\n      proxy_set_header   Host $host;\n      proxy_set_header   X-Real-IP $remote_addr;\n      proxy_set_header   X-Forwarded-For $http_x_forwarded_for;\n      proxy_set_header   X-Forwarded-Host $server_name;\n      proxy_pass http://grpc/;\n    }\n\n    # go api\n    location / {\n      proxy_redirect     off;\n      proxy_set_header   Host $host;\n      proxy_set_header   X-Real-IP $remote_addr;\n      proxy_set_header   X-Forwarded-For $http_x_forwarded_for;\n      proxy_set_header   X-Forwarded-Host $server_name;\n      proxy_pass http://app;\n    }\n  }\n}"

I tried reading the file using cat and transforming the output with awk and sed but sadly is not working.

Any advice on how to accomplish this would be really appreciated, preferable using bash.
Thanks for your help :pray:

Hi @san8cloud :wave: , what error do you have when you run this command? (Can you add -vvv option to your cURL query and show the output here? [:warning: paste just the HTTP status and body response]

1 Like

Hi @rophilogene , thanks for answering. I made it work, here is how I did it, for anyone wondering:

#!/bin/bash
set -ex

QOVERY_API="https://api.qovery.com"
ENV_FILE='{"var_name":"NGINX_CONFIG_FILE","mount_path":"/etc/nginx/nginx.conf","file_path":"nginx/nginx.eph.conf"}'
QOVERY_APP_ID="xxx-xxx-xxx-xxx"
QOVERY_CLI_ACCESS_TOKEN="qov_xxx"
REQUEST_DATA=request-data.$RANDOM.tmp

jq --null-input --arg VAR_NAME "$(echo $ENV_FILE | jq --raw-output '.var_name')" \
    --arg FILE_CONTENT "$(cat nginx.conf)" \
    --arg MOUNT_PATH "$(echo $ENV_FILE | jq --raw-output '.mount_path')" \
   '{"key":$VAR_NAME,"value":$FILE_CONTENT,"mount_path":$MOUNT_PATH}' > $REQUEST_DATA

curl --include --request POST \
    --header "Accept: application/json" \
    --header "Content-Type: application/json; charset=utf8" \
    --header "Authorization: Token $QOVERY_CLI_ACCESS_TOKEN" \
    --data @$REQUEST_DATA \
    "$QOVERY_API/application/$QOVERY_APP_ID/environmentVariable"

In this scenario I uploaded a nginx conf file updated dynamically in a pipeline using the internal domain from another apps deployed in previous steps.

1 Like

Well done @san8cloud - I wonder if there is a way to simplify it a bit. I’ll take a deeper look this week. Probably we can find something but the most important is that it works :slight_smile:

Thank you for sharing this with us :pray:

1 Like

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