Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any possibility to use CodeDeploy environment variables in section files of AppSpec file

I have website, which stored on AWS EC2 servers.

We have 2 servers, one for production environment and another one for development and staging environments.

Development and staging environments located in different folders. For example development env stored in /var/www/development, while staging stored in /var/www/staging.

I'd like to use AWS CodeDeploy to upload files directly from bitbucket. I put AppSpec file, which copy source code to /var/www/html folder and install all dependencies and configurations. But I want my AppSpec file to copy source code to /var/www/development or to /var/www/staging depending on Development group, that was selected.

Is there is any way to do it or, maybe, there are some better approach in my situation?

like image 429
Liauchuk Ivan Avatar asked Aug 06 '17 19:08

Liauchuk Ivan


2 Answers

The appspec.yml is a bit inflexible so use the following to deploy code in to different folders on the same instance.

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/my-temp-dir
permissions:
  - object: /var/www/my-temp-dir
    owner: ec2-user
    group: ec2-user
hooks:
  BeforeInstall:
    - location: ci/integrations-deploy-pre.sh
      runas: root
  AfterInstall:
    - location: ci/integrations-deploy-post.sh
      runas: root

Inside of my integrations-deploy-post.sh file, I then use the CodeDeploy environment variables to move the files in to the place I need them to be;

#!/bin/bash
    
if [ "$DEPLOYMENT_GROUP_NAME" == "Staging" ]
then
    cp -R /var/www/my-temp-dir /var/www/my-staging-dir
    chown -R ec2-user:ec2-user /var/www/my-staging-dir

    # Insert other commands that need to run...
fi

if [ "$DEPLOYMENT_GROUP_NAME" == "UAT" ]
then
    cp -R /var/www/my-temp-dir /var/www/my-uat-dir
    chown -R ec2-user:ec2-user /var/www/my-uat-dir

    # Insert other commands that need to run...
fi

NOTE: In my integrations-deploy-post.sh You'll also need the commands you want to run on production. Removed for simplicity.

like image 79
ajtrichards Avatar answered Sep 21 '22 16:09

ajtrichards


The recommended way to change AppSpec or custom scripts behavior is to utilize environment variables provided by the CodeDeploy agent. You have access to the deployment group name and the application name.

if [ "$DEPLOYMENT_GROUP_NAME" == "Staging" ]; then
  # Copy to /var/www/staging
elif [ "$DEPLOYMENT_GROUP_NAME" == "Development" ]; then
  # Copy to /var/www/development
elif [ "$DEPLOYMENT_GROUP_NAME" == "Production" ]; then
  # Copy to /var/www/html
else
  # Fail the deployment
fi
like image 37
EmptyArsenal Avatar answered Sep 24 '22 16:09

EmptyArsenal