在Gitlab中传递--build-arg或在两步Dockerfile中使用环境变量

问题描述 投票:1回答:1

我的最小测试项目布局如下所示。

├── deployment
│   ├── build.sh
│   └── nginx
│       └── nginx.conf
├── Dockerfile
├── next.config.js
├── package.json
├── package-lock.json
└── pages
    ├── _app.js
    └── index.js

Dockerfile的内容:

FROM node as build-stage

ARG K8S_SECRET_PUB
ENV K8S_SECRET_PUB ${K8S_SECRET_PUB}

ARG SRV
ENV SRV ${SRV}

WORKDIR /app
COPY package*json /app/
RUN npm install --production
COPY ./ /app/
RUN npm run export


FROM nginx:1.15-alpine

RUN rm /etc/nginx/nginx.conf
COPY --from=build-stage /app/out /www
COPY deployment/nginx/nginx.conf /etc/nginx/
EXPOSE 5000

目标是将环境变量K8S_SECRET_PUB和SRV传递给构建过程。 npm run export执行next build && next export以获取nginx服务器应该服务的静态文件。

next.config.js的内容:

require('dotenv').config();

module.exports = {
  serverRuntimeConfig: {
    srv: process.env.SRV
  },
  publicRuntimeConfig: {
    pub: process.env.K8S_SECRET_PUB
  }
};

pages / _app.js的内容:

import App from 'next/app';
import getConfig from 'next/config';

const { serverRuntimeConfig, publicRuntimeConfig } = getConfig();

class MyApp extends App {
  render() {
    return (
      <div>
        <h1>
          {serverRuntimeConfig.srv || 'SRV not accessible from client :p'}
        </h1>
        <h1>{publicRuntimeConfig.pub || 'PUB not set'}</h1>
      </div>
    );
  }
}

export default MyApp;

当通过docker build --build-arg K8S_SECRET_PUB=puppy --build-arg SRV=serverval -t my_image .在本地构建docker图像时,我可以通过docker run -p 5000:5000 my_image启动容器。

访问正在运行的容器具有预期的结果。检查文件系统进一步显示拾取传递的构建参数并相应地写入文件。

testing the docker image locally

但是,当我将此代码推送到Gitlab时,部署的nginx如下所示:

running it on Gitlab

我想要完成的是通过设置 - > CI / CD下的Gitlab UI定义的环境变量被拾取并在Dockerfile中定义的构建阶段中使用。由于我们对Auto Dev感到满意,我们还没有创建和检查.gitlab-ci.yml文件。


更新#1

稍微修补一下后,我现在可以访问环境变量,但是我失去了Auto DevOps的便利性。

我添加了一个deployment/build.sh这个内容:

#!/bin/sh
docker build --build-arg K8S_SECRET_PUB="${K8S_SECRET_PUB}" --build-arg SRV="${SRV}" -t my_image .

我也开始使用.gitlab-ci.yml,其中包含:

stages:
    - build
    - review
    - deploy
    - clean

image: docker:latest

services:
    - docker:dind

build:
    stage: build
    script:
        - sh ./deployment/build.sh
        - mkdir image
        - docker save my_image > image/my_image.tar
    artifacts:
        paths:
            - image

将存储库推送到Gitlab后,管道成功,我可以下载工件,解压缩,通过docker load -i image/my_image.tar加载并运行它。当然,页面加载了Gitlab CI / CD UI中定义的变量。但是,现在我已经失去了部署过程的所有其他步骤(这是我不想首先编写.gitlab-ci.yml的主要原因)。


更新#2

使用我在https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml找到的Auto DevOps模板,我进行了以下更改:

  • 评论出- template: Jobs/Build.gitlab-ci.yml线
  • 用docker图像替换alpine
  • CODE_QUALITY_DISABLED: "true"添加到变量部分,因为代码质量检查花费的时间太长
  • 添加上述尝试中的服务和构建部分

现在,我陷入了审查阶段。

Application should be accessible at: http://*my_image_url*
Waiting for deployment "review-branchname-abcxyz" rollout to finish: 0 of 1 updated replicas are available...
docker continuous-integration environment-variables gitlab next.js
1个回答
1
投票

在更新#2之后,这些是我为使其工作所做的更改。

重写了.gitlab-ci.yml:

image: docker:latest

variables:
  CI_APPLICATION_TAG: $CI_COMMIT_SHA
  CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG
  CODE_QUALITY_DISABLED: "true"

  KUBERNETES_VERSION: 1.11.9
  HELM_VERSION: 2.13.1

  DOCKER_DRIVER: overlay2

  ROLLOUT_RESOURCE_TYPE: deployment

stages:
  - build
  - test
  - deploy # dummy stage to follow the template guidelines
  - review
  - dast
  - staging
  - canary
  - production
  - incremental rollout 10%
  - incremental rollout 25%
  - incremental rollout 50%
  - incremental rollout 100%
  - performance
  - cleanup

include:
#  - template: Jobs/Build.gitlab-ci.yml
  - template: Jobs/Test.gitlab-ci.yml
  - template: Jobs/Code-Quality.gitlab-ci.yml
  - template: Jobs/Deploy.gitlab-ci.yml
  - template: Jobs/Browser-Performance-Testing.gitlab-ci.yml
  - template: Security/DAST.gitlab-ci.yml
  - template: Security/Container-Scanning.gitlab-ci.yml
  - template: Security/Dependency-Scanning.gitlab-ci.yml
  - template: Security/License-Management.gitlab-ci.yml
  - template: Security/SAST.gitlab-ci.yml

# Override DAST job to exclude master branch
dast:
  except:
    refs:
      - master

services:
    - docker:dind

build:
    stage: build
    script:
        - sh ./deployment/build.sh

从我找到的模板中使用更多,并重写了deploy / build.sh:

if ! docker info &>/dev/null; then
  if [ -z "$DOCKER_HOST" -a "$KUBERNETES_PORT" ]; then
    export DOCKER_HOST='tcp://localhost:2375'
  fi
fi

if [[ -n "$CI_REGISTRY_USER" ]]; then
  echo "Logging to GitLab Container Registry with CI credentials..."
  docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
fi

if [[ -f Dockerfile ]]; then
  echo "Building Dockerfile-based application..."
else
  echo "Building Heroku-based application using gliderlabs/herokuish docker image..."
  cp /build/Dockerfile Dockerfile
fi

docker build --build-arg K8S_SECRET_PUB="${K8S_SECRET_PUB}" --build-arg SRV="${SRV}" --tag "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" .

docker push "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG"
© www.soinside.com 2019 - 2024. All rights reserved.