How Can I deploy from Bitbucket to AWS to different instances and different folders?

375 views Asked by At

I have two instances in AWS. One for production and one for homologation. I deploy automatically with CodeDeploy. I have two branches on BitBucket, the master and homolog. When I commit in the homolog deploy must go to the instance of homologation, and if I make a merge in the master deploy must be in the production stage.

To do the automatic deploy of Bitbucket to AWS there is a series of files that configure the deploy details. One of these files is the appspec.yml. According to AWS it is only possible to have an appspec.yml file.

This basic form file has the following structure:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  AfterInstall:
    - location: deploy-scripts/install_dependencies.py
      timeout: 300
      runas: root

The problem is that for each instance I have a destination folder.

If I do the deploy on the homolog instance the destination folder should be var/www/html and for the production instance it should be var/www/html/test/

I tried to do it as below:

version: 0.0
os: linux
files:
  - source: /
    destination: deploy-scripts/destination.py
hooks:
  AfterInstall:
    - location: deploy-scripts/install_dependencies.py
      timeout: 300
      runas: root

That's the destination.py:

if os.environ['APPLICATION_NAME'] == 'ahimsa-store-homolog':
    return '/var/www/html/'
elif os.environ['APPLICATION_NAME'] == 'ahimsa-store':
    return '/var/www/html/teste/'

The above option does not work. How can I do this?

2

There are 2 answers

0
Tanbouz On

The files section of appspec.yml doesn't run scripts.

  • Move the required files to a temporary folder using the files section
  • Create a script that will move these files from the temporary location to the required destination depending on your requirements. As you suggested, using os.environ['APPLICATION_NAME'] for example.
  • Make sure your script sets correct file permissions after moving the files.
  • Include that script in the AfterInstall section, so the script can find the new files "installed" in the temporary location you choose. Also make sure it is before installing the dependencies!
0
annamataws On

Another option is to have a different appspec in each branch. It would make merging more difficult, but it could help.