Cara memuatkan pembolehubah daripada gitlab ke .env VueJS
P粉682987577
P粉682987577 2024-03-26 19:14:28
0
1
398

Saya mahu memuatkan pembolehubah Gitlab ke dalam .env.Production dalam aplikasi VueJS supaya saya boleh menggunakan process.env.VUE_APP_VARIABLE_NAME.

Apa yang saya lakukan:

  1. Buat beberapa pembolehubah dalam persediaan GitLab CI/CD
  2. Buat fail .env.product:
VUE_APP_EMAILJS_USER_ID=*
VUE_APP_EMAILJS_SERVICE_ID=*
VUE_APP_EMAILJS_TEMPLATE_ID=*

VUE_APP_FIREBASE_API_KEY=*
VUE_APP_FIREBASE_AUTH_DOMAIN=*
VUE_APP_FIREBASE_PROJECT_ID=*
VUE_APP_FIREBASE_STORAGE_BUCKET=*
VUE_APP_FIREBASE_MESSAGING_SENDER_ID=*
VUE_APP_FIREBASE_APP_ID=*
VUE_APP_FIREBASE_MEASUREMENT_ID=*
  1. my.gitlab-ci.yml
build-docker-image:
  stage: build
  before_script:
    # $CI_JOB_TOKEN is variable automatically added by Gitlab: see https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#variables-reference
    - echo $CI_JOB_TOKEN | docker login -u gitlab-ci-token registry.gitlab.com --password-stdin
  script:
    # build and push docker image to gitlab registry
    - docker pull $GITLAB_IMAGE || true # Allows us to use --cache-from
    - docker build 
      --build-arg VUE_APP_EMAILJS_USER_ID=$VUE_APP_EMAILJS_USER_ID
      --build-arg VUE_APP_EMAILJS_SERVICE_ID=$VUE_APP_EMAILJS_SERVICE_ID
      --build-arg VUE_APP_EMAILJS_TEMPLATE_ID=$VUE_APP_EMAILJS_TEMPLATE_ID
      --build-arg VUE_APP_FIREBASE_API_KEY=$VUE_APP_FIREBASE_API_KEY
      --build-arg VUE_APP_FIREBASE_AUTH_DOMAIN=$VUE_APP_FIREBASE_AUTH_DOMAIN
      --build-arg VUE_APP_FIREBASE_PROJECT_ID=$VUE_APP_FIREBASE_PROJECT_ID
      --build-arg VUE_APP_FIREBASE_STORAGE_BUCKET=$VUE_APP_FIREBASE_STORAGE_BUCKET
      --build-arg VUE_APP_FIREBASE_MESSAGING_SENDER_ID=$VUE_APP_FIREBASE_MESSAGING_SENDER_ID
      --build-arg VUE_APP_FIREBASE_APP_ID=$VUE_APP_FIREBASE_APP_ID
      --build-arg VUE_APP_FIREBASE_MEASUREMENT_ID=$VUE_APP_FIREBASE_MEASUREMENT_ID
      --cache-from $GITLAB_IMAGE -t $GITLAB_IMAGE:$CI_COMMIT_SHA .
    - docker push $GITLAB_IMAGE:$CI_COMMIT_SHA
  1. Tatarajah.Fail Docker
FROM node:14.18 as build-stage
WORKDIR /app
COPY package*.json ./
COPY yarn.lock ./
RUN yarn install
COPY . .

# pass Docker varibales to VueJS environment varibales
ARG VUE_APP_EMAILJS_USER_ID
ENV VUE_APP_EMAILJS_USER_ID $VUE_APP_EMAILJS_USER_ID
ARG VUE_APP_EMAILJS_SERVICE_ID
ENV VUE_APP_EMAILJS_SERVICE_ID $VUE_APP_EMAILJS_SERVICE_ID
ARG VUE_APP_EMAILJS_TEMPLATE_ID
ENV VUE_APP_EMAILJS_TEMPLATE_ID $VUE_APP_EMAILJS_TEMPLATE_ID

ARG VUE_APP_FIREBASE_API_KEY
ENV VUE_APP_FIREBASE_API_KEY $VUE_APP_FIREBASE_API_KEY
ARG VUE_APP_FIREBASE_AUTH_DOMAIN
ENV VUE_APP_FIREBASE_AUTH_DOMAIN $VUE_APP_FIREBASE_AUTH_DOMAIN
ARG VUE_APP_FIREBASE_PROJECT_ID
ENV VUE_APP_FIREBASE_PROJECT_ID $VUE_APP_FIREBASE_PROJECT_ID
ARG VUE_APP_FIREBASE_STORAGE_BUCKET
ENV VUE_APP_FIREBASE_STORAGE_BUCKET $VUE_APP_FIREBASE_STORAGE_BUCKET
ARG VUE_APP_FIREBASE_MESSAGING_SENDER_ID
ENV VUE_APP_FIREBASE_MESSAGING_SENDER_ID $VUE_APP_FIREBASE_MESSAGING_SENDER_ID
ARG VUE_APP_FIREBASE_APP_ID
ENV VUE_APP_FIREBASE_APP_ID $VUE_APP_FIREBASE_APP_ID
ARG VUE_APP_FIREBASE_MEASUREMENT_ID
ENV VUE_APP_FIREBASE_MEASUREMENT_ID $VUE_APP_FIREBASE_MEASUREMENT_ID

# build the application
RUN yarn build

FROM nginx:1.19 as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf

Tetapi saya hanya menerima nilai nol untuk semua pembolehubah

Adakah sesiapa tahu apa yang salah dengan penyelesaian saya? Atau adakah anda mempunyai penyelesaian lain untuk mencapai apa yang saya mahu lakukan?

P粉682987577
P粉682987577

membalas semua(1)
P粉668113768

Saya rasa anda telah melakukan kerja yang baik tetapi terlupa untuk menghantar ENV pada Dockerfile Saya mempunyai konfigurasi yang sama tetapi menghantarnya menggunakan simbol =, contohnya:

FROM node:14.18 as build-stage
WORKDIR /app
COPY package*.json yarn.lock ./
RUN yarn install
COPY . .

# pass Docker varibales to VueJS environment varibales
ARG VUE_APP_FIREBASE_APP_ID \
    VUE_APP_EMAILJS_USER_ID  \
    VUE_APP_FIREBASE_API_KEY \
    VUE_APP_EMAILJS_SERVICE_ID \
    VUE_APP_EMAILJS_TEMPLATE_ID \
    VUE_APP_FIREBASE_PROJECT_ID \
    VUE_APP_FIREBASE_AUTH_DOMAIN \
    VUE_APP_FIREBASE_STORAGE_BUCKET \
    VUE_APP_FIREBASE_MEASUREMENT_ID \
    VUE_APP_FIREBASE_MESSAGING_SENDER_ID

ENV VUE_APP_FIREBASE_APP_ID=$VUE_APP_FIREBASE_APP_ID \
    VUE_APP_EMAILJS_USER_ID=$VUE_APP_EMAILJS_USER_ID \
    VUE_APP_FIREBASE_API_KEY=$VUE_APP_FIREBASE_API_KEY \
    VUE_APP_EMAILJS_SERVICE_ID=$VUE_APP_EMAILJS_SERVICE_ID \
    VUE_APP_EMAILJS_TEMPLATE_ID=$VUE_APP_EMAILJS_TEMPLATE_ID \
    VUE_APP_FIREBASE_PROJECT_ID=$VUE_APP_FIREBASE_PROJECT_ID \
    VUE_APP_FIREBASE_AUTH_DOMAIN=$VUE_APP_FIREBASE_AUTH_DOMAIN \
    VUE_APP_FIREBASE_STORAGE_BUCKET=$VUE_APP_FIREBASE_STORAGE_BUCKET \
    VUE_APP_FIREBASE_MEASUREMENT_ID=$VUE_APP_FIREBASE_MEASUREMENT_ID \
    VUE_APP_FIREBASE_MESSAGING_SENDER_ID=$VUE_APP_FIREBASE_MESSAGING_SENDER_ID

# build the application
RUN yarn build

FROM nginx:1.19 as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]

Untuk mengasingkan persekitaran pembangunan dan pengeluaran, anda boleh menambah DEV_ dan MAIN_ sebelum nama persekitaran pada fail Gitlab CI/CD env dan .gitlab-ci.yml anda, seperti:

build-docker-image-develop:
  stage: build
  before_script:
    # $CI_JOB_TOKEN is variable automatically added by Gitlab: see https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#variables-reference
    - echo $CI_JOB_TOKEN | docker login -u gitlab-ci-token registry.gitlab.com --password-stdin
  script:
    # build and push docker image to gitlab registry
    - docker pull $GITLAB_IMAGE || true # Allows us to use --cache-from
    - docker build 
      --build-arg VUE_APP_EMAILJS_USER_ID=$DEV_VUE_APP_EMAILJS_USER_ID
      --build-arg VUE_APP_FIREBASE_APP_ID=$DEV_VUE_APP_FIREBASE_APP_ID
      --build-arg VUE_APP_FIREBASE_API_KEY=$DEV_VUE_APP_FIREBASE_API_KEY
      --build-arg VUE_APP_EMAILJS_SERVICE_ID=$DEV_VUE_APP_EMAILJS_SERVICE_ID
      --build-arg VUE_APP_EMAILJS_TEMPLATE_ID=$DEV_VUE_APP_EMAILJS_TEMPLATE_ID
      --build-arg VUE_APP_FIREBASE_PROJECT_ID=$DEV_VUE_APP_FIREBASE_PROJECT_ID
      --build-arg VUE_APP_FIREBASE_AUTH_DOMAIN=$DEV_VUE_APP_FIREBASE_AUTH_DOMAIN
      --build-arg VUE_APP_FIREBASE_MEASUREMENT_ID=$DEV_VUE_APP_FIREBASE_MEASUREMENT_ID
      --build-arg VUE_APP_FIREBASE_STORAGE_BUCKET=$DEV_VUE_APP_FIREBASE_STORAGE_BUCKET
      --build-arg VUE_APP_FIREBASE_MESSAGING_SENDER_ID=$DEV_VUE_APP_FIREBASE_MESSAGING_SENDER_ID
      --cache-from $GITLAB_IMAGE -t $GITLAB_IMAGE:$CI_COMMIT_SHA .
    - docker push $GITLAB_IMAGE:$CI_COMMIT_SHA
  only:
    - develop
  tags:
    - docker-build

#### production

build-docker-image-main:
  stage: build
  before_script:
    # $CI_JOB_TOKEN is variable automatically added by Gitlab: see https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#variables-reference
    - echo $CI_JOB_TOKEN | docker login -u gitlab-ci-token registry.gitlab.com --password-stdin
  script:
    # build and push docker image to gitlab registry
    - docker pull $GITLAB_IMAGE || true # Allows us to use --cache-from
    - docker build 
      --build-arg VUE_APP_EMAILJS_USER_ID=$MAIN_VUE_APP_EMAILJS_USER_ID
      --build-arg VUE_APP_FIREBASE_APP_ID=$MAIN_VUE_APP_FIREBASE_APP_ID
      --build-arg VUE_APP_FIREBASE_API_KEY=$MAIN_VUE_APP_FIREBASE_API_KEY
      --build-arg VUE_APP_EMAILJS_SERVICE_ID=$MAIN_VUE_APP_EMAILJS_SERVICE_ID
      --build-arg VUE_APP_EMAILJS_TEMPLATE_ID=$MAIN_VUE_APP_EMAILJS_TEMPLATE_ID
      --build-arg VUE_APP_FIREBASE_PROJECT_ID=$MAIN_VUE_APP_FIREBASE_PROJECT_ID
      --build-arg VUE_APP_FIREBASE_AUTH_DOMAIN=$MAIN_VUE_APP_FIREBASE_AUTH_DOMAIN
      --build-arg VUE_APP_FIREBASE_MEASUREMENT_ID=$MAIN_VUE_APP_FIREBASE_MEASUREMENT_ID
      --build-arg VUE_APP_FIREBASE_STORAGE_BUCKET=$MAIN_VUE_APP_FIREBASE_STORAGE_BUCKET
      --build-arg VUE_APP_FIREBASE_MESSAGING_SENDER_ID=$MAIN_VUE_APP_FIREBASE_MESSAGING_SENDER_ID
      --cache-from $GITLAB_IMAGE -t $GITLAB_IMAGE:$CI_COMMIT_SHA .
    - docker push $GITLAB_IMAGE:$CI_COMMIT_SHA
  only:
    - main
  tags:
    - docker-build
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan