0
votes

I have a project written in Django (Python Framework) and repository in Bitbucket

I have to setup a bitbucket pipeline with the following actions:

  1. Deploy in Staging server and commits are made to staging branch
  2. Deploy to Production server when a release is a draft from the master branch only.

I'm not sure where release can be draft in Bitbucket like Github.

I have the following bitbucket-pipelines.yml file

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

Now, In the Django application, I'm using .env to serve credentials and settings. For a different environment, say development, staging, and production, I have different environment files for each

development.env
staging.env
production.env

I need to rename/copy the respective file to .env depending on the deployment type.

How can I set up this in bitbucket pipeline to carry out this step?

appspec.yml contents are:

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

Can I have multiple appspec.yml files depending on the deployment type?

2

2 Answers

0
votes

Having multiple directories under application version name might be a good solution.

1.0.0->
      appspec.yml
      files/
      scripts/
0
votes

If you can specify the naming conventions for the entire team, then a convention based approach can solve this.

First of all, make the deployment branches have the same name as environment. As mentioned in your sample code when the branch name is "staging" the application will pick up staging.env for deployment. In bitbucket pipeline you can refer to the branch name using $BITBUCKET_BRANCH default variable.

https://confluence.atlassian.com/bitbucket/variables-in-pipelines-794502608.html

Bitbucket Pipelines provides a set of default variables as well as the ability to define your own variables. You can mark variables as secured for additional security of your passwords, tokens, and other values. You can also update your variables when you run a pipeline manually.

Secondly, replace your reference to the env file with a variable in your environment setup script. Eg:$filename.env

Pass the branch name to your python script codedeploy_deploy.py and replace the variable in your setup script with this.

This solution holds good only as long as you can maintain the conventions.