How I can define the location of the deployed app in AwsCodeploy based upon environment?

17 Views Asked by At

Due to cost I am using a EC-2 where both production and staging reside upon seperate vhosts:

  1. For Staging I use the following files:
    • Nginx Vhost: /etc/nginx/sites-available/staging.conf
    • WWW Root: /var/www/staging
  2. For Production I use the following files:
    • Nginx Vhost: /etc/nginx/sites-available/production.conf
    • WWW Root: /var/www/production

For my deployment I use a single codeploy app but a seperate deployment group for each environment. And my appspec.yml is:

version: 0.0
os: linux

files:
  - source: /
    destination: /var/www/staging

hooks:
  ApplicationStop:
    - location: deploy/application-stop.sh
      runas: root
  BeforeInstall:
    - location: deploy/clean-files.sh
      runas: root
  AfterInstall:
    - location: deploy/install_app.sh
      runas: root
  ApplicationStart:
    - location: deploy/application-start.sh
      runas: root

How I can specify the nessesary deployment path based upon the deployment group. Is there a way to have parameterized deployment so I can set the nessesary paths where app will be deployed upon?

1

There are 1 best solutions below

0
Dimitrios Desyllas On

An approach I followed is the follwing:

Upon build step I defined a plaintext environmental variable named ENVIRONMENT then yupon my build step I did the following (I use aws codebuild):

cp appspec.$ENVIRONMENT.yml appspec.yml

And I placed 2 appspec files:

  1. For production named appspec.production.yml
  2. For staging named appspec.staging.yml

And upon ENVIRONMENT variable I place the appropriate value:

  1. production for production
  2. staging for staging

Then upon my deployed artifacts in my buildspec I placed:


# rest ob buildspec.

artifacts:
  files:
   # rest of files here
   - appspec.yml

Furthermore In my case each appspec retrieves the nessesary webhook scripts from the following folders:

  • deploy/production for production
  • deploy/staging for staging

Then upon scripts and the seperate appspec files I place the correct paths for vhost and where app will be deployed upon. I same do upon each script in deploy folder.

For a new environment (named development) I can:

  1. copy-paste the deploy/production into deploy/development
  2. modify the scripts a bit
  3. make the nessesary vhosts and folders upon the server
  4. Make a new build step having ENVIRONMENT into development

To sum up the idea is to use multiple appspec files with a naming convention and copy the correct one into ethe epxected appspec.yml file. Then upon each appspec and webhook script I place the correct paths for vhost and application.


Note:

For the build scripts, I think I could have each script load a config file or source a config script that uses the existing environmental variables in order to load the appropriate values. But I have not tested yet.