适用于AWS弹性beantalk环境的特定git分支 [英] Specific git branches for aws elastic beanstalk environments

查看:95
本文介绍了适用于AWS弹性beantalk环境的特定git分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前的情况.

  • 我正在使用AWS Elasticbeanstalk和eb cli 3.x工具进行部署.
  • 我已经创建了2个环境(开发和生产).并在我的git存储库中为每个环境(即master,production)提供一个分支
  • 我在git仓库中创建了.ebextensions和.elasticbeanstalk文件夹
  • .ebextensions文件夹具有特定于每个环境的配置文件(例如,设置,文件更改,环境变量等)
  • I am using AWS Elasticbeanstalk along with the eb cli 3.x tools for deployment.
  • I have created 2 environments (development and production). and one branch in my git repo for each environment (i.e. master , production)
  • I have created .ebextensions and .elasticbeanstalk folders in my git repo
  • the .ebextensions folder has config files that are specific to each environment (e.g. setups, files changes, environment variables . . etc)

我希望在每个环境中使用自己的git分支.

I wish to work on each environment in its own git branch.

我的困难

如果我必须部署到开发环境,它将变得非常简单

if i have to deploy to development env, it gets really simple

// make config changes in master branch
// git add, commit
// eb deploy
// thus development environment is updated

但是,如果我必须将其部署到生产中,那就是问题所在

But if i have to deploy to production is where the problem begins

git checkout production
git merge master // pulls config that is meant for development environment only
eb deploy 

我希望当我合并master分支中的更改时,我的所有代码都使用最新更改进行更新.但是.ebextensions和.elasticbeanstalk目录保持不变

I want that when i merge changes from the master branch, all my code updates with the latest changes. But the .ebextensions and .elasticbeanstalk directories remain untouched

在合并到生产分支时,如何告诉git忽略整个.ebextensions文件夹?

How do tell git to ignore the whole .ebextensions folder while merging into production branch ?

推荐答案

使用EB CLI 3.x

对于此版本,它相对简单.例如:

Using EB CLI 3.x

For this version it's relatively simple. For example:

mkdir HelloWorld                 # create new directory for project
cd HelloWorld                    # enter the new directory
git init                         # create git repository
eb init -p PHP                   # create new application

echo "<?php echo getenv("ENV_NAME"); ?>" > index.php
git add .gitignore index.php
git commit -m 'Initial commit.'

eb create dev-env                # create environment named dev-env
eb create prod-env               # create environment named prod-env

eb use dev-env                   # associate dev-env to current branch (master)
eb setenv ENV_NAME=DEV           # set env variable specific to dev-env

git checkout -b production       # create production branch and switch to it
eb use prod-env                  # associate prod-env to the current branch (production)
eb setenv ENV_NAME=PROD          # set env variable specific to prod-env

这不会在本地文件系统中的任何位置保存ENV_NAME. EB CLI直接更改实时EB实例.您可以使用 eb config save (由Nick Humrich建议)将当前运行环境的环境配置设置保存到.elasticbeanstalk/saved_configs/<env-name>.cfg.yml.由于每个环境都有其自己的文件,因此,除非在两个分支中都更改了其中之一,否则不应有任何冲突.另一种选择(请参阅 保护敏感信息 )是将它们添加到.gitignore.

This doesn't save ENV_NAME anywhere in the local filesystem. The EB CLI changes the live EB instance directly. You may use eb config save (as suggested by Nick Humrich) to save the environment configuration settings for the current running environment to .elasticbeanstalk/saved_configs/<env-name>.cfg.yml. Since each environment has its own file, you shouldn't have any conflicts, unless you change one of them in both branches. Another option (see Protecting sensitive informations) would be to add them to .gitignore.

一种方法是为每个环境(分支)使用不同的optionsettings文件. EB CLI 可以帮助您:-)

One way is to have distinct optionsettings files for each environment (branch). The EB CLI can help you with that :-)

在每个分支中运行eb init(请参见下文),并为每个分支选择一个不同的环境名称,因此最终将得到2个不同的.elasticbeanstalk/optionsettings.<env-name>文件.这样可以避免.elasticbeanstalk/上的冲突.

Run eb init from each branch (see below) and choose a different environment name for each one, so you'll end up with 2 distinct .elasticbeanstalk/optionsettings.<env-name> files. This way you avoid the conflicts on .elasticbeanstalk/.

mkdir MyApp
cd MyApp

2.初始化Git存储库

git init .

3.设置开发环境(主分支)

eb init

注意:当它要求您提供环境名称时,请选择一个名称来标识它是开发环境还是生产环境.

NOTE: When it asks you to provide an environment name, choose a name that identifies whether it's a development or production environment.

Enter your AWS Access Key ID (current value is "<redacted>"): 
Enter your AWS Secret Access Key (current value is "<redacted>"): 
Select an AWS Elastic Beanstalk service region.
Available service regions are:
<redacted>
Select (1 to 8): 1
Enter an AWS Elastic Beanstalk application name
(auto-generated value is "MyApp"): MyApp      
Enter an AWS Elastic Beanstalk environment name
(auto-generated value is "MyApp-env"): MyApp-dev
Select an environment tier.
Available environment tiers are:
1) WebServer::Standard::1.0
2) Worker::SQS/HTTP::1.0
Select (1 to 2): 1
Select a solution stack.
Available solution stacks are:
<redacted>
Select (1 to 59): 32
Select an environment type.
Available environment types are:
1) LoadBalanced
2) SingleInstance
Select (1 to 2): 2
Create an RDS DB Instance? [y/n]: n
Attach an instance profile (current value is "[Create a default instance profile]"):
<redacted>
Select (1 to 5): 4

4.创建一个新的生产分支

git checkout -b production

5.设置生产环境

eb init

重复步骤3,但选择其他环境名称.这将创建不同的.elasticbeanstalk/optionsettings.<env-name>文件.

Repeat step 3 but pick a different environment name. This will create distinct .elasticbeanstalk/optionsettings.<env-name> file.

对于两种环境,您应该使用相同的app.config. option_settings部分是唯一可能在环境之间有所不同的内容.但是据我所知,每个环境不能有不同的option_settings,那么我们怎么做呢?

You should use the same app.config for both environments. The only thing that may diverge between environments is the option_settings section. But as far as I know, you can't have different option_settings per environment, so how can we do it?

好吧,这是我目前尚无最佳解决方案的地方,但是我会告诉您如何做到的.我添加了我需要的所有option_name并使用占位符值,例如:

Well, that's something I don't have an optimal solution yet, but I'll tell you how I do it. I add all option_name's I need and use placeholder values, for example:

option_settings:
  - option_name: MY_CONFIG
    value: CHANGEME

然后,我通过AWS Elastic Beanstalk管理面板手动更改它们的值.转到Application > Configuration > Software Configuration > Environment Properties.

Then later I change their values manually through the AWS Elastic Beanstalk admin panel. Go to Application > Configuration > Software Configuration > Environment Properties.

另一种可能性是拥有一个由您的container_commands运行的自定义脚本.该脚本可以通过其主机名(或另一个唯一值)识别EC2实例,并自动加载环境变量(例如source <hostname>.env).

Another possibility would be to have a custom script which is run by your container_commands. This script could identify the EC2 instance by its hostname (or another unique value) and automatically load the environment variables (e.g. source <hostname>.env).

您需要遵守的唯一规则是:除非您不在乎,您的存储库不得包含敏感信息,例如凭据.

The only rule you need to obey is this: Your repository MUST NOT contain sensitive informations like credentials, unless you don't care.

例如,应用程序希望通过环境变量读取RDS凭据,因此您将它们放在option_settings中.但是您不希望其他贡献者看到他们,是吗?我建议使用占位符的解决方案在这方面很方便.

For example, an application expects to read RDS credentials via environment variables, so you put them in option_settings. But you don't want other contributors to see them, do you? The solution I propose using placeholder is convenient on this aspect.

这篇关于适用于AWS弹性beantalk环境的特定git分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆