首页 > 解决方案 > 使用 CodeDeploy 和 Bitbucket 管道在 EC2 上自动部署

问题描述

我有一个用Django(Python 框架)编写的项目和Bitbucket中的存储库

我必须使用以下操作设置 bitbucket 管道:

  1. 在暂存服务器中部署并提交到暂存分支
  2. 当发布只是来自主分支的草稿时,部署到生产服务器。

我不确定像 Github 这样的 Bitbucket 可以在哪里草稿。

我有以下bitbucket-pipelines.yml文件

image: python:3.7

pipelines:
  branches:
    staging:
      - step:
          deployment: staging
          script: 
            - apt-get update
            - apt-get install -y zip # required for packaging up the application
            - pip install boto3==1.3.0 # required for codedeploy_deploy.py
            - zip -r /tmp/artifact.zip * # package up the application for deployment
            - python codedeploy_deploy.py # run the deployment script

现在,在 Django 应用程序中,我使用.env它来提供凭据和设置。对于不同的环境,比如开发、登台和生产,我对每个环境都有不同的环境文件

development.env
staging.env
production.env

我需要.env根据部署类型重命名/复制相应的文件。

如何在 bitbucket 管道中设置它以执行此步骤?

appspec.yml内容为:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/project/
permissions:
  - object: /var/www/html
    pattern: "**"
    owner: ubuntu
    group: ubuntu
hooks:
  BeforeInstall:
    - location: scripts/clean_instance.sh
      timeout: 6000
      runas: root
  AfterInstall:
    - location: scripts/install_os_dependencies.sh
      timeout: 6000
      runas: root
    - location: scripts/install_python_dependencies.sh
      timeout: 6000
      runas: ubuntu
    - location: scripts/setup_environment.sh
      timeout: 6000
      runas: ubuntu
    - location: scripts/migrate.sh
      timeout: 6000
      runas: ubuntu
    - location: scripts/ngnix.sh
      timeout: 6000
      runas: ubuntu
  ApplicationStart:
    - location: scripts/start_application.sh
      timeout: 6000
      runas: ubuntu
  ApplicationStop:
    - location: scripts/stop_application.sh
      timeout: 6000
      runas: ubuntu

根据部署类型,我可以有多个 appspec.yml 文件吗?

标签: gitbitbucketaws-code-deploybitbucket-pipelines

解决方案


在应用程序版本名称下拥有多个目录可能是一个很好的解决方案。

1.0.0->
      appspec.yml
      files/
      scripts/

推荐阅读