如何使用docker.gitlab-ci.yml文件传递common/config/main-local.php用户名、host.password?
在这里,我已经做了每一件事,我可以使用Ubuntu服务器上的如何使用docker.gitlab-ci.yml文件传递common/config/main-local.php用户名、host.password?,docker,yii2,gitlab,yii2-advanced-app,Docker,Yii2,Gitlab,Yii2 Advanced App,在这里,我已经做了每一件事,我可以使用Ubuntu服务器上的php./init--env=Development--overwrite=All配置我的初始化文件one # # File is "indented" using multiple of 4 spaces # # Specify the docker image to use (only used if using docker runners) # See: http://doc.gitlab.com/ee/ci/docker/us
php./init--env=Development--overwrite=All
配置我的初始化文件one
#
# File is "indented" using multiple of 4 spaces
#
# Specify the docker image to use (only used if using docker runners)
# See: http://doc.gitlab.com/ee/ci/docker/using_docker_images.html
# From https://hub.docker.com/r/kaffineaddict/gitlabcakephp3/ - we could use image: kaffineaddict/gitlabcakephp3
image: php:7.2
# The docker services to configure
# See: http://doc.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-service
services:
#- mysql:5.7
# Define custom build variables
# For the default gitlab variables see: http://doc.gitlab.com/ce/ci/variables/README.html
# These can be used below, or they will also be ENV variables available within' any scripts
# you execute from the CI
variables:
#MYSQL_DATABASE: site_zoova
#MYSQL_ALLOW_EMPTY_PASSWORD: "1"
#MYSQL_ROOT_PASSWORD: ThisIsAStrongPassword#^2
# Define commands that run before each job's script
before_script:
- umask 022 # set permissions to default directory permissions of 755 and default file permissions are 644,
# Install ssh-agent if not already installed, it is required by Docker.
# (change apt-get to yum if you use a CentOS-based image)
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- docker-php-ext-install pdo pdo_mysql mysqli
# Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
# For Docker builds disable host key checking. Be aware that by adding that
# you are suspectible to man-in-the-middle attacks.
# WARNING: Use this only with the Docker executor, if you use it with shell
# you will overwrite your user's SSH config.
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
# Prepare the build environment. A way to overcome this is to create a script which installs all prerequisites prior the actual testing is done.
- bash ci/docker_install.sh > /dev/null
#- bash ci/docker_install.sh
# Prepare the build environment. A way to overcome this is to create a script which installs all prerequisites prior the actual testing is done.
- cd $CI_PROJECT_DIR && curl --silent --show-error https://getcomposer.org/installer | php
# Install composer dependencies
#- composer install --no-plugins --no-scripts
- cd $CI_PROJECT_DIR && php composer.phar config cache-files-dir /cache/composer
- cd $CI_PROJECT_DIR && php composer.phar install --no-plugins --no-scripts --optimize-autoloader
- cd $CI_PROJECT_DIR
# Folder and file manipulation
- 'which rsync || ( apt-get install rsync -y )'
- '[ -d $CI_PROJECT_DIR/tmp ] || mkdir -p $CI_PROJECT_DIR/tmp'
- rm -rf $CI_PROJECT_DIR/tmp/*
- find $CI_PROJECT_DIR -type d -exec chmod 0755 {} \;
- find $CI_PROJECT_DIR -type f -exec chmod 0644 {} \;
- chmod -R 777 $CI_PROJECT_DIR/tmp
- chmod +x $CI_PROJECT_DIR/ci/*
- chown www-data:www-data $CI_PROJECT_DIR/ -R
# Make sure these dirs/files are not writable
# setup application
- chmod go-w $CI_PROJECT_DIR
- |
# following 4 will done once on the server
# cp common/config/main-local.example common/config/main-local.php
# cp common/config/params.example common/config/params.php
# cp rest/web/index.example rest/web/index.php
#genetraing files and added to z_rsync_exclude_list
php ./init --env=Development --overwrite=All
php composer.phar update
yes | php yii migrate
#resolve errors through phpcs
# php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php backend --colors
# php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php common --colors
# php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php rest --colors
mkdir -p backend/web/assets
chmod 775 backend/web/assets
sudo chown www-data:www-data backend/web/assets
- |
# Define commands that run before after all builds
after_script:
#- find . -type d -exec chmod 0755 {} \; # Set directory permissions #moved in each stage
#- find . -type f -exec chmod 0644 {} \; # Set file permissions #moved in each stage
# Define list of files that should be cached between subsequent runs -
# Composer stores all downloaded packages in the vendor/ directory.
# temporary commented out - builds failed
cache:
paths:
- vendor/
# stages is used to define build stages that can be used by jobs
# The specification of stages allows for having flexible multi stage pipelines
# The next stage only executes if all elements of the previous stage succeed
# Typically used for compiled languages testing and/or to automate deployments
stages:
- development
- production
#
# Run test on all branches but master
#
development:
stage: development
only:
- dev
script:
- echo Running dev
# DO NOT COPY THIS KEY TO PUBLIC PLACES
- ssh-add <(echo "$SSH_PRIVATE_KEY_DEV")
#- echo Running tests...
# Ex: - phpunit --configuration phpunit_myapp.xml
#- vendor/bin/phpunit # TODO: uncomment me - in docker file we have it as /usr/local/bin/phpunit
# Sync the files to the server
- echo Using rsync to push changes to dev server...
- rsync -ap --stats -e "ssh -p $SSH_PORT_DEV" --exclude-from 'ci/z_rsync_exclude_list.txt' $CI_PROJECT_DIR/ $SSH_USER_DEV@$SSH_IP_DEV:$PROJECT_PATH_DEV
- echo Running shell scripts on remote server
- ssh -t -p $SSH_PORT_DEV $SSH_USER_DEV@$SSH_IP_DEV 'cd '"'$PROJECT_PATH_DEV'"';ci/shell-scripts-dev.sh'
# Done
- echo Done pushing changes to dev...
#Environment is used to define that a job deploys to a specific environment. This allows easy tracking of all deployments to your environments straight from GitLab.
#If environment is specified and no environment under that name exists, a new one will be created automatically.
environment: development
#Make sure we don't push to live if build has failed
allow_failure: false #default behaviour
#
# Send to live server if branch is master
#
production:
stage: production
only:
- master
script:
- echo Running prod
# DO NOT COPY THIS KEY TO PUBLIC PLACES
- ssh-add <(echo "$SSH_PRIVATE_KEY_PROD")
#- echo Running tests...
# Ex: - phpunit --configuration phpunit_myapp.xml
#- vendor/bin/phpunit #TODO: uncomment me - in docker file we have it as /usr/local/bin/phpunit
# Push to live server now
- echo Using rsync to push changes to live server...
- rsync -ap --stats -e "ssh -p $SSH_PORT_PROD" --exclude-from 'ci/z_rsync_exclude_list.txt' $CI_PROJECT_DIR/ $SSH_USER_PROD@$SSH_IP_PROD:$PROJECT_PATH_PROD
- echo Running shell scripts on remote server
- ssh -t -p $SSH_PORT_PROD $SSH_USER_PROD@$SSH_IP_PROD 'cd '"'$PROJECT_PATH_PROD'"';ci/shell-scripts-prod.sh'
# Done
- echo Done pushing changes to live...
#Environment is used to define that a job deploys to a specific environment. This allows easy tracking of all deployments to your environments straight from GitLab.
#If environment is specified and no environment under that name exists, a new one will be created automatically.
environment: production
##make sure development built before moving to production
dependencies:
- development
#
#文件使用4个空格的倍数“缩进”
#
#指定要使用的docker映像(仅在使用docker Runner时使用)
#见:http://doc.gitlab.com/ee/ci/docker/using_docker_images.html
#从https://hub.docker.com/r/kaffineaddict/gitlabcakephp3/ -我们可以使用图像:kaffineaddict/gitlabcakephp3
图片:php:7.2
#要配置的docker服务
#见:http://doc.gitlab.com/ee/ci/docker/using_docker_images.html#what-是服务吗
服务:
#-mysql:5.7
#定义自定义生成变量
#有关默认gitlab变量,请参见:http://doc.gitlab.com/ce/ci/variables/README.html
#这些变量可以在下面使用,也可以是“任何脚本”中可用的环境变量
#您可以从CI执行
变量:
#MYSQL_数据库:site_zoova
#MYSQL\u允许\u空\u密码:“1”
#MYSQL_ROOT_密码:此为strong密码#^2
#定义在每个作业脚本之前运行的命令
在脚本之前:
-umask 022#将权限设置为默认目录权限755,默认文件权限644,
#安装ssh代理如果尚未安装,Docker需要它。
#(如果使用基于CentOS的映像,请更改为转到yum)
-'哪个ssh代理| |(apt get update-y&&apt get install openssh client-y)'
-docker php ext安装pdo pdo_mysql mysqli
#运行ssh代理(在生成环境中)
-eval$(ssh代理-s)
#对于Docker构建,禁用主机密钥检查。请注意,通过添加
#你被中间人怀疑了。
#警告:如果与shell一起使用,则仅与Docker executor一起使用
#您将覆盖用户的SSH配置。
-mkdir-p~/.ssh
-“[[-f/.dockerenv]]和&echo-e”主机*\n\t三通键检查号\n\n“>~/.ssh/config”
#准备构建环境。克服此问题的一种方法是创建一个脚本,在实际测试完成之前安装所有先决条件。
-bash ci/docker_install.sh>/dev/null
#-bash ci/docker_install.sh
#准备构建环境。克服此问题的一种方法是创建一个脚本,在实际测试完成之前安装所有先决条件。
-cd$CI_项目_DIR&&curl--静默--显示错误https://getcomposer.org/installer |php
#安装编写器依赖项
#-composer安装--没有插件--没有脚本
-cd$CI_PROJECT_DIR&&php composer.phar配置缓存文件DIR/cache/composer
-cd$CI_PROJECT_DIR&&php composer.phar安装--无插件--无脚本--优化自动加载程序
-cd$CI_项目目录
#文件夹和文件操作
-'哪个rsync | |(apt get install rsync-y)'
-“[-d$CI_项目_DIR/tmp]| | mkdir-p$CI_项目_DIR/tmp”
-rm-rf$CI_项目总监/tmp/*
-查找$CI_PROJECT_DIR-type d-execchmod 0755{}\;
-查找$CI_PROJECT_DIR-type f-exec chmod 0644{}\;
-chmod-R 777$CI_项目总监/tmp
-chmod+x$CI\u项目目录/CI/*
-www.chown www.data:www.data$CI\u PROJECT\u DIR/-R
#确保这些目录/文件不可写
#设置应用程序
-chmod go-w$CI_项目目录
- |
#以下4项将在服务器上执行一次
#cp common/config/main-local.example common/config/main-local.php
#cp common/config/params.example common/config/params.php
#cp rest/web/index.example rest/web/index.php
#生成培训文件并添加到z_rsync_exclude_列表
php./init--env=Development--overwrite=All
php composer.phar更新
是| php yii迁移
#通过PHPC解决错误
#php./vendor/bin/phpcs--encoding=utf-8--extensions=php后端--colors
#php./vendor/bin/phpcs--encoding=utf-8--extensions=php-common--colors
#php./vendor/bin/phpcs--encoding=utf-8--extensions=php rest--colors
mkdir-p后端/web/assets
chmod 775后端/网络/资产
sudo chown www-data:www-data-backend/web/assets
- |
#定义在所有生成之前和之后运行的命令
在脚本之后:
#-找到-d型-exec chmod 0755{}\#设置每个阶段中移动的目录权限
#-找到-f型-exec chmod 0644{}\#设置文件权限#在每个阶段中移动
#定义应在后续运行之间缓存的文件列表-
#Composer将所有下载的软件包存储在供应商/目录中。
#临时注释掉-生成失败
隐藏物:
路径:
-卖主/
#阶段用于定义作业可以使用的生成阶段
#阶段规范允许具有灵活的多级管道
#下一阶段仅在前一阶段的所有元素成功时执行
#通常用于编译语言测试和/或自动化部署
阶段:
-发展
-生产
#
#在除主分支以外的所有分支上运行测试
#
发展:
阶段:发展
仅:
-发展
脚本:
-回声运行开发
#请勿将此密钥复制到公共场所
-ssh add似乎没有正确地将本地env变量解析到Yii配置中
您需要确保您的环境文件包含一些对这些变量的引用
<?php
return [
'components' => [
// uncomment the following to setup a local db or, any other db
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host={APP_DB_HOST};dbname={APP_DB_NAME}',
'username' => '{APP_DB_USERNAME}',
'password' => '{APP_DB_PASSWORD}',
'charset' => 'utf8',
],
// ...
],
];
@MuhammadOmerAslam兄弟我被困在这里两天了你的环境/dev/common/config/main local.php
看起来像什么?在构建过程中,您是否试图在任何时候解析它?这很好,但是我如何为prod和dev定义不同的变量?只需在shell-scripts-prod.sh和shell-scripts-dev.sh中使用不同的变量集,或者只需在各自的主机中编辑cofig本地文件,并且在部署时不覆盖它们。我相信,我只能在yml中定义变量在shell-secripts-dev.sh之前执行yml文件这里是我的shell脚本。德夫/bin/bash cd$(目录名$0)########################
sed -i "s/{APP_DB_HOST}/${APP_DB_HOST}/g" ${CI_PROJECT_DIR}/common/config/main-local.php
sed -i "s/{APP_DB_NAME}/${APP_DB_NAME}/g" ${CI_PROJECT_DIR}/common/config/main-local.php
sed -i "s/{APP_DB_USERNAME}/${APP_DB_USERNAME}/g" ${CI_PROJECT_DIR}/common/config/main-local.php
sed -i "s/{APP_DB_PASSWORD}/${APP_DB_PASSWORD}/g" ${CI_PROJECT_DIR}/common/config/main-local.php