Created
January 11, 2020 12:47
-
-
Save bayu-code-lab/47e1ecfb8dc302471a1fff0441c2d1c9 to your computer and use it in GitHub Desktop.
CI/CD Django Bitbucket to AWS Elastic Beanstalk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A Bitbucket Builds template for deploying | |
an application to AWS Elastic Beanstalk | |
joshcb@amazon.com | |
v1.0.0 | |
""" | |
from __future__ import print_function | |
import os | |
import sys | |
from time import strftime, sleep | |
import boto3 | |
from botocore.exceptions import ClientError | |
branch = os.getenv('BITBUCKET_BRANCH') | |
ENVIRONMENT_MAP = { | |
'master': os.getenv('ENVIRONMENT_NAME'), | |
# 'development': 'development' | |
} | |
VERSION_LABEL = strftime("%Y%m%d%H%M%S") | |
BUCKET_KEY = os.getenv('APPLICATION_NAME') + '/' + VERSION_LABEL + \ | |
'-bitbucket_builds.zip' | |
def upload_to_s3(artifact): | |
""" | |
Uploads an artifact to Amazon S3 | |
""" | |
try: | |
client = boto3.client('s3') | |
except ClientError as err: | |
print("Failed to create boto3 client.\n" + str(err)) | |
return False | |
try: | |
client.put_object( | |
Body=open(artifact, 'rb'), | |
Bucket=os.getenv('S3_BUCKET'), | |
Key=BUCKET_KEY | |
) | |
except ClientError as err: | |
print("Failed to upload artifact to S3.\n" + str(err)) | |
return False | |
except IOError as err: | |
print("Failed to access artifact.zip in this directory.\n" + str(err)) | |
return False | |
return True | |
def create_new_version(): | |
""" | |
Creates a new application version in AWS Elastic Beanstalk | |
""" | |
try: | |
client = boto3.client('elasticbeanstalk') | |
except ClientError as err: | |
print("Failed to create boto3 client.\n" + str(err)) | |
return False | |
try: | |
response = client.create_application_version( | |
ApplicationName=os.getenv('APPLICATION_NAME'), | |
VersionLabel=VERSION_LABEL, | |
Description='New build from Bitbucket', | |
SourceBundle={ | |
'S3Bucket': os.getenv('S3_BUCKET'), | |
'S3Key': BUCKET_KEY | |
}, | |
Process=True | |
) | |
except ClientError as err: | |
print("Failed to create application version.\n" + str(err)) | |
return False | |
try: | |
if response['ResponseMetadata']['HTTPStatusCode'] is 200: | |
return True | |
else: | |
print(response) | |
return False | |
except (KeyError, TypeError) as err: | |
print(str(err)) | |
return False | |
def deploy_new_version(): | |
""" | |
Deploy a new version to AWS Elastic Beanstalk | |
""" | |
try: | |
client = boto3.client('elasticbeanstalk') | |
except ClientError as err: | |
print("Failed to create boto3 client.\n" + str(err)) | |
return False | |
try: | |
response = client.update_environment( | |
ApplicationName=os.getenv('APPLICATION_NAME'), | |
EnvironmentName=ENVIRONMENT_MAP.get(branch), | |
VersionLabel=VERSION_LABEL, | |
) | |
except ClientError as err: | |
print("Failed to update environment.\n" + str(err)) | |
return False | |
print(response) | |
return True | |
def main(): | |
" Your favorite wrapper's favorite wrapper " | |
if not upload_to_s3('/tmp/artifact.zip'): | |
sys.exit(1) | |
if not create_new_version(): | |
sys.exit(1) | |
# Wait for the new version to be consistent before deploying | |
sleep(5) | |
if not deploy_new_version(): | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image: python:3.7.6 | |
pipelines: | |
branches: | |
master: | |
- step: | |
name: build and package application before runnning deployment script and running deployment script | |
deployment: production | |
caches: | |
- pip | |
script: | |
- apt-get update # to install required zip | |
- apt-get install -y zip # required for packaging up the application | |
- pip install boto3==1.3.0 # required for beanstalk_deploy.py | |
- zip -rv /tmp/artifact.zip ./* .ebextensions .elasticbeanstalk # package up the application for deployment | |
- python beanstalk_deploy.py # run the deployment script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
we need to upgrade python version any help for it