Env_var: Duplicate resource on another branch

,

What I try to accomplish:

I want to create environment variables with different values per branch with the API.
This is possible within the portal:

The problem

I get a 409 conflict error when I try to create a new variable with a name which exists for another branch.

{
    "@type": "error",
    "error_type": "duplicate_resource",
    "error_message": "resource already exists"
}

The query I execute:

# The first query, non existing variable, this is fine and executes with a 201 Response
curl --location --request POST 'https://api.travis-ci.com/repo/xxxxxx/env_vars' \
--header 'Authorization: token xxxxxx' \
--header 'Travis-API-Version: 3' \
--header 'Content-Type: application/json' \
--data-raw '{
    "env_var.name": "Test_var",
    "env_var.value": "testvalue",
    "env_var.public": false,
    "env_var.branch": "development"
}'

# The second query, fails with a 409.
curl --location --request POST 'https://api.travis-ci.com/repo/xxxxxx/env_vars' \
--header 'Authorization: token xxxxxx' \
--header 'Travis-API-Version: 3' \
--header 'Content-Type: application/json' \
--data-raw '{
    "env_var.name": "Test_var",
    "env_var.value": "testvalue2",
    "env_var.public": false,
    "env_var.branch": "test"
}'

I tried with patching the given env var, but this results in overwriting the env_var from the other branch.

Is this a bug? Or am I missing something?

Additionally, the type of the branch property is missing in the env_var and env_vars documentation

Some variant of:

if [[ "$TRAVIS_BRANCH" == "master" ]]; then
 export MY_VAR=master
else
 export MY_VAR=not-master
fi

Should work.

Did you read my question?

I want to set an environment variable with the api.

Not inside a travis build.

If you try any random string for the branch_name, do you get the same result? Which is:

The requested URL returned error: 409 

I did read your question to be clear, the question itself is a bit vague.

Try reading the following:

https://developer.travis-ci.com/resource/env_var

Yes, I tried different branch names, it is not possible to create a variable with the same name for different branches with the api.

I did read the documentation, as stated in my question, there is the type of the branch property missing, but i guess it should be a string?

Are you able to perform the following two queries?

curl --location --request POST 'https://api.travis-ci.com/repo/xxxxxx/env_vars' \
--header 'Authorization: token xxxxxx' \
--header 'Travis-API-Version: 3' \
--header 'Content-Type: application/json' \
--data-raw '{
    "env_var.name": "variable",
    "env_var.value": "testvalue",
    "env_var.public": false,
    "env_var.branch": "branch1"
}'

curl --location --request POST 'https://api.travis-ci.com/repo/xxxxxx/env_vars' \
--header 'Authorization: token xxxxxx' \
--header 'Travis-API-Version: 3' \
--header 'Content-Type: application/json' \
--data-raw '{
    "env_var.name": "variabl",
    "env_var.value": "testvalue2",
    "env_var.public": false,
    "env_var.branch": "branch2"
}'

I am facing the same issue which I didn’t expect, exactly because it is allowed on the UI. Has anybody found a solution to create the variable with the same name but for a different branch?

Not yet.

I’ve opened a support ticket a few days ago, no solution so far.

1 Like