gpt4 book ai didi

docker - Gitlab CI将环境变量传递给Docker构建

转载 作者:行者123 更新时间:2023-12-02 18:28:55 26 4
gpt4 key购买 nike

我想将https://gitlab.com/ric_harvey/nginx-php-fpm用作docker executor的基础Gitlab CI镜像。但是此镜像具有许多配置,例如。 WEBROOT。我需要将此WEBROOT设置为我自己的值。是否可以在Gitlab CI中运行它?

我已经尝试了(无法工作):

  • 变量:gitlab-ci.yml中的定义
  • 中的
  • 设置变量WEBROOT gitlab-ci.yml中的before_script
  • 在Gitlab UI中添加环境变量WEBROOT->项目-> CI / CD->环境变量

  • 一切似乎为时已晚,我需要将docker的启动命令编辑为:
    docker run -e "WEBROOT=xxx" ...


    image: richarvey/nginx-php-fpm:1.1.1

    variables:
    WEBROOT: "/build/domotron/cloud/www" <- this wont work

    before_script:
    ## Install ssh-agent if not already installed, it is required by Docker.
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

    ## Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)

    ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    ## We're using tr to fix line endings which makes ed25519 keys work
    ## without extra base64 encoding.
    ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null

    ## Create the SSH directory and give it the right permissions
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    ## Setup git
    - git config --global user.email "email"
    - git config --global user.name "User"

    ## Use ssh-keyscan to scan the keys of your private server.
    - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    stages:
    - test

    Codeception:
    stage: test
    services:
    - name: selenium/standalone-chrome
    alias: chrome
    script:
    - curl -sS https://getcomposer.org/installer | php
    - php composer.phar install --no-interaction
    - php vendor/bin/codecept run

    最佳答案

    就您不能为构建器镜像重载entrypoint而言:
    https://docs.gitlab.com/runner/executors/docker.html#the-image-keyword

    The Docker executor doesn’t overwrite the ENTRYPOINT of a Docker image.



    我建议您基于 richarvey/nginx-php-fpm:1.1.1创建自己的图像,并将其用于构建。

    您可以在管道中迈出第一步,在其中准备所需的工具,例如自己的构建器:

    gitlab-ci.yaml
    stages:
    - prepare
    - build
    - ...
    prepare-build-dockers:
    stage: prepare
    image: docker:stable
    script:
    - export WEBROOT
    - build -t my-builder Dockerfiles

    Dockerfiles / Dockerfile
    FROM richarvey/nginx-php-fpm:1.1.1

    Btw gitlab现在支持自定义docker注册表,因此拥有自己的镜像进行构建/测试/部署是一个好习惯。

    关于docker - Gitlab CI将环境变量传递给Docker构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53556252/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com