GitHub上Travis CI加密环境变量被覆盖问题求助
Hey there, let's get this sorted out for you! The problem you're hitting comes down to how Travis CI interprets the env.matrix section in your .travis.yml file.
Why the Override Happens
When you ran travis encrypt ... --add env.matrix twice, you added two separate entries to the matrix array. Each entry in env.matrix represents a distinct build environment—not multiple variables within the same environment. So instead of having both variables available in one build, Travis treats them as two separate build configurations, each with only one of your secrets. That's why you're seeing the second one "override" the first—you're likely only running the latest build environment, or the matrix is spinning up separate jobs each with just one variable.
Solutions to Get Both Variables Working
Option 1: Use env.global (Recommended for Shared Secrets)
If you need both secrets available in every build (which sounds like your use case), switch to the env.global section instead of matrix. This tells Travis to load both variables for all your builds:
- First, remove the existing
env.matrixsection from your.travis.yml. - Run these two commands to add your secrets as global variables:
travis encrypt FIRST_SECRET=your_first_value --add env.global travis encrypt SECOND_SECRET=your_second_value --add env.global
Your .travis.yml will end up looking like this:
env: global: - secure: kQeMLwvGVBl... - secure: h7SXfIif5Y...
Now both secrets will be available in every build job.
Option 2: Combine Secrets in a Single matrix Entry
If you specifically need to use env.matrix (for example, if you're testing multiple environment combinations), you need to put both encrypted secrets in the same matrix entry instead of separate ones:
You can do this in two ways:
- Manual Edit: Update your
.travis.ymlto combine the entries:env: matrix: - secure: kQeMLwvGVBl... secure: h7SXfIif5Y... - Single Encryption Command: Encrypt both variables at once in one command, which creates a single secure entry containing both secrets:
travis encrypt "FIRST_SECRET=your_first_value SECOND_SECRET=your_second_value" --add env.matrix
This way, the single matrix entry will include both secrets in the same build environment.
Quick Check
After making these changes, commit your updated .travis.yml and trigger a new build. You should now see both environment variables available in your build process (just make sure you don't print them out accidentally!).
内容的提问来源于stack exchange,提问作者Jeff Lowery

