简体   繁体   English

如何在 VueJS 项目中在构建时使用环境变量

[英]How to use environment variables at build time in a VueJS project

I'm trying to use an environment variable during the build stage of a CI job for a VueJS app.我正在尝试在 VueJS 应用程序的 CI 作业的build阶段使用环境变量。 I'm using GitLab CI, and one of the environment variables that is made available is CI_COMMIT_SHORT_SHA ,我正在使用 GitLab CI,可用的环境变量之一是CI_COMMIT_SHORT_SHA

build:
  image: node:latest
  stage: build
  variables:
    CI_COMMIT_SHORT_SHA: "$CI_COMMIT_SHORT_SHA"
  artifacts:
    paths:
      - dist/
  script:
    - npm install --progress=false
    - npm run build
    - echo "Build Complete"

Here's how I'm trying to use this variable in Vue:这是我尝试在 Vue 中使用这个变量的方式:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p>This is a static site that is served with a CloudFront distribution in front of an S3 bucket.</p>
    <p>The site is updated through a GitLab CI/CD pipeline.</p>
    <p>Commit ref: {{ commit }}</p>
    <p>Using cache invalidation</p>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data(){
    return {
      commit: process.env.CI_COMMIT_SHORT_SHA
    }
  }
}
</script>

I'm not seeing this variable come through.我没有看到这个变量通过。 Is there something else I need to do in order to access the environment variable and display it in my component?为了访问环境变量并将其显示在我的组件中,我还需要做些什么吗?

If you're using webpack.config , you can set up DefinePlugin in a similar way.如果你使用webpack.config ,你可以用类似的方式设置DefinePlugin

In your webpack.config.js you would use a new plugin,在您的webpack.config.js您将使用一个新插件,

new webpack.DefinePlugin({
  /* 
    JSON.stringify(yourconfig) is highly recommened 
    to avoid overwriting existing keysother process.env
  */
  'process.env': JSON.stringify(config.prod), // or config.dev
}),

Where config.prod / config.dev is something like其中config.prod / config.dev是这样的

let config = {};
config.prod = require('./config/prod.env'); // imports ./config/prod.env.js
config.dev = require('./config/dev.env');

at the top of the file,在文件的顶部,

and the prod.env.js and dev.env.js files look something likeprod.env.jsdev.env.js文件看起来像

'use strict';
module.exports = {
  VUE_APP_MODE: 'MYMODE'
};

If you wanted to match the vue process more closely,you could filter out the object keys using RegExp /^VUE_APP_.*/ .如果你想更紧密地匹配 vue 进程,你可以使用 RegExp /^VUE_APP_.*/过滤掉对象键。

Then in the data section of your .vue file you can include these by using:然后在 .vue 文件的数据部分,您可以使用以下方法包含这些:

data: {
  VUE_APP_MODE: process.env.VUE_APP_MODE
}

As mentioned in https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-codehttps://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code 中所述

Only variables that start with VUE_APP_ will be statically embedded into the client bundle with webpack.DefinePlugin .只有这样开始的变量VUE_APP_将静态嵌入到与客户端捆绑webpack.DefinePlugin You can access them in your application code:您可以在应用程序代码中访问它们:

console.log(process.env.VUE_APP_SECRET)

After some research it seemed that the vue-cli-service build command only looks into the dot-files in the root of your project, and only processes these variables starting with VUE_APP_ (in various .env files)经过一些研究,似乎vue-cli-service build命令只查看项目根目录中的点文件,并且只处理这些以VUE_APP_开头的变量(在各种 .env 文件中)

You could set all the variables in the Gitlab CI options and then copy those variables to the .env file.您可以在 Gitlab CI 选项中设置所有变量,然后将这些变量复制到 .env 文件中。 Now, when vue-cli builds the project, it injects these values in the transpiled scripts.现在,当 vue-cli 构建项目时,它会在转译的脚本中注入这些值。

You could issue a command like this before you build the project:您可以在构建项目之前发出这样的命令:

env | grep 'VUE_APP_' > .env

I also use a staging environment that is built when pushing into the staging branch.我还使用了在推送到 staging 分支时构建的 staging 环境。 Therefore I have these variables set into Gitlab:因此,我将这些变量设置到 Gitlab 中:

  • VUE_APP_VAR1=foo VUE_APP_VAR1=foo
  • VUE_APP_VAR2=bar VUE_APP_VAR2=条形
  • VUE_ACCEPT_VAR1=foo VUE_ACCEPT_VAR1=foo
  • VUE_ACCEPT_VAR2=bar VUE_ACCEPT_VAR2=条形

Since vue-cli wants the variables to start with VUE_APP_ I do a replace with sed:由于 vue-cli 希望变量以VUE_APP_开头, VUE_APP_我用 sed 替换:

env | grep 'VUE_ACCEPT_' | sed 's/VUE_ACCEPT_/VUE_APP_/' > .env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM