Skip to content
Snippets Groups Projects
Commit 79de340a authored by Etienne Baqué's avatar Etienne Baqué :red_circle:
Browse files

Added CF script

parent 94668c86
No related branches found
No related tags found
1 merge request!1Added start and stop scripts
Pipeline #193106624 passed
......@@ -2,7 +2,8 @@ image: ruby:2.3
stages:
- build
- deploy
- review
- production
build_site:
stage: build
......@@ -16,15 +17,46 @@ build_site:
except:
- master
deploy_to_ec2_review:
stage: deploy
image: registry.gitlab.com/ebaque/cloud-deploy/aws-ecs:latest
variables:
CI_AWS_S3_TARGET_LOCATION: 's3://jekylldemobucket/jekylldemo.zip'
CI_AWS_S3_SOURCE_LOCATION: 'public'
CI_AWS_EC2_APPLICATION_NAME: 'JekyllDemo_App'
CI_AWS_EC2_DEPLOYMENT_FILE: 'aws/create_deployment.json'
variables:
CI_AWS_S3_PUSH_FILE: 'aws/s3_push.json'
CI_AWS_EC2_DEPLOYMENT_FILE: 'aws/create_deployment.json'
CI_AWS_CF_CREATE_STACK_FILE: 'aws/cf_create_stack.json'
CI_AWS_CF_STACK_NAME: 'JekyllDemo'
AUTO_DEVOPS_PLATFORM_TARGET: EC2
.push-and-deploy:
image: 'registry.gitlab.com/ebaque/cloud-deploy/aws:latest'
script:
- ec2 push-and-deploy
except:
- master
- cloudformation create-stack
- ec2 push-to-s3
- ec2 deploy-to-ec2
review_ec2:
extends: .push-and-deploy
stage: review
environment:
name: review/$CI_COMMIT_REF_NAME
rules:
- if: '$AUTO_DEVOPS_PLATFORM_TARGET != "EC2"'
when: never
- if: '$CI_KUBERNETES_ACTIVE'
when: never
- if: '$REVIEW_DISABLED'
when: never
- if: '$CI_COMMIT_BRANCH == "master"'
when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
production_ec2:
extends: .push-and-deploy
stage: production
environment:
name: production
rules:
- if: '$AUTO_DEVOPS_PLATFORM_TARGET != "EC2"'
when: never
- if: '$CI_KUBERNETES_ACTIVE'
when: never
- if: '$CI_COMMIT_BRANCH != "master"'
when: never
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
......@@ -2,21 +2,4 @@ version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/apps/jekyll-demo
hooks:
# BeforeInstall:
# - location: scripts/install_dependencies.sh
# timeout: 300
# runas: ubuntu
# AfterInstall:
# - location: scripts/change_permissions.sh
# timeout: 300
# runas: ubuntu
ApplicationStart:
- location: scripts/start_server.sh
timeout: 300
runas: ubuntu
ApplicationStop:
- location: scripts/stop_server.sh
timeout: 300
runas: ubuntu
destination: /usr/share/nginx/html/jekyll-demo
{
"Description": "Create a demo EC2 instance",
"Parameters": {
"ApplicationName": {
"Type": "String",
"Default": "JekyllApp"
},
"EC2DeploymentGroupName": {
"Type": "String",
"Default": "JekyllApp_DepGroup"
},
"EC2InstanceName": {
"Type": "String",
"Default": "DeployEC2Demo"
}
},
"Resources": {
"EC2InstanceDemo": {
"Type": "AWS::EC2::Instance",
"Properties": {
"AvailabilityZone": "us-east-1f",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/xvda",
"Ebs": {
"DeleteOnTermination": "true",
"VolumeSize": "8",
"VolumeType": "gp2"
}
}
],
"ImageId": "ami-0c94855ba95c71c99",
"InstanceType": "t2.micro",
"IamInstanceProfile": {
"Ref": "IAMRole"
},
"KeyName": "ebaque_ec2_key_pair",
"NetworkInterfaces": [
{
"Description": "Network interface",
"DeviceIndex": "0",
"SubnetId": "subnet-8710eb89",
"PrivateIpAddress": "172.31.48.4",
"GroupSet": [
{
"Ref": "SecurityGroupDemo"
}
]
}
],
"Tags": [
{"Key": "Name", "Value": { "Ref": "EC2InstanceName" } }
],
"UserData": { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -xe\n",
"yum update -y\n",
"amazon-linux-extras install nginx1 -y\n",
"amazon-linux-extras install ruby2.6 -y\n",
"sudo yum install git -y\n",
"gem install bundler\n",
"cd /home/ec2-user\n",
"wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install\n",
"chmod +x ./install\n",
"sudo ./install auto\n",
"sudo mkdir /usr/share/nginx/html/jekyll-demo\n",
"# Install the files and packages from the metadata\n",
"/opt/aws/bin/cfn-init ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource EC2InstanceDemo ",
" --region ", { "Ref" : "AWS::Region" }, "\n"
]]}}
},
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"files": {
"/etc/nginx/conf.d/jekylldemo.conf": {
"content" : { "Fn::Join" : ["", [
"server {\n",
"listen 80 default_server;\n",
"listen [::]:80 default_server;\n",
"root /usr/share/nginx/html/jekyll-demo;\n",
"index index.html;\n",
"location / {\n",
"try_files $uri $uri/ =404;\n\n",
"}\n",
"}\n"
]]},
"mode" : "000755",
"owner" : "root",
"group" : "root"
},
"/etc/nginx/sites-enabled/jekylldemo": {
"content" : "/etc/nginx/sites-available/jekylldemo",
"mode" : "120755"
}
},
"commands": {
"01-unlink-ngix-default": {
"command": "sudo unlink /etc/nginx/sites-enabled/default",
"cwd": "/etc/nginx/sites-enabled"
},
"02-restart-nginx": {
"command": "sudo service nginx restart"
}
},
"services" : {
"sysvinit" : {
"nginx" : {
"enabled" : "true",
"ensureRunning" : "true",
"files" : ["/etc/nginx/nginx.conf"],
"sources" : ["/usr/share/nginx/html"]
}
}
}
}
}
}
},
"IAMRole": {
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Path": "/",
"Roles": ["CodeDeployDemo-EC2-Instance-Profile"]
}
},
"SecurityGroupDemo": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupName": "security_group_for_ec2_demo",
"GroupDescription": "Security group for EC2 demo",
"VpcId": "vpc-06510c7c",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": 22,
"ToPort": 22,
"CidrIp": "0.0.0.0/0",
"Description": "SSH"
},
{
"IpProtocol": "tcp",
"FromPort": 80,
"ToPort": 80,
"CidrIp": "0.0.0.0/0",
"Description": "HTTP"
}
]
}
},
"EC2ApplicationName": {
"Type" : "AWS::CodeDeploy::Application",
"Properties": {
"ApplicationName": {"Ref" : "ApplicationName"}
}
},
"EC2DeploymentGroup": {
"Type" : "AWS::CodeDeploy::DeploymentGroup",
"Properties": {
"ApplicationName": {"Ref" : "ApplicationName"},
"DeploymentGroupName": { "Ref": "EC2DeploymentGroupName" },
"DeploymentConfigName": "CodeDeployDefault.OneAtATime",
"Ec2TagFilters": [{
"Key": "Name",
"Value": { "Ref": "EC2InstanceName" },
"Type": "KEY_AND_VALUE"
}],
"ServiceRoleArn": "arn:aws:iam::392034512401:role/CodeDeployServiceRole"
},
"DependsOn": "EC2ApplicationName"
}
}
}
{
"applicationName": "JekyllDemo_App",
"applicationName": "JekyllApp",
"deploymentConfigName": "CodeDeployDefault.OneAtATime",
"deploymentGroupName": "JekyllDemo_DepGroup",
"deploymentGroupName": "JekyllApp_DepGroup",
"revision": {
"revisionType": "S3",
"s3Location": {
......
{
"applicationName": "JekyllApp",
"source": "public",
"s3Location": "s3://jekylldemobucket/jekylldemo.zip"
}
#!/bin/bash
GIT_REPO=git@gitlab.com:ebaque/jekyll-demo.git
TMP_GIT_CLONE=$HOME/tmp/jekyll-demo
PUBLIC_WWW=/home/ubuntu/apps/jekyll-demo
git clone $GIT_REPO $TMP_GIT_CLONE
cd $TMP_GIT_CLONE
bundle install
bundle exec jekyll build --source $TMP_GIT_CLONE --destination $PUBLIC_WWW
rm -Rf $TMP_GIT_CLONE
# GIT_REPO=https://gitlab.com/ebaque/jekyll-demo.git
# TMP_GIT_CLONE=/tmp/jekyll-demo
# PUBLIC_WWW=/home/ec2-user/apps/jekyll-demo
# git clone $GIT_REPO $TMP_GIT_CLONE
# cd $TMP_GIT_CLONE
# bundle install
# bundle exec jekyll build --source $TMP_GIT_CLONE --destination $PUBLIC_WWW
# rm -Rf $TMP_GIT_CLONE
#
# wget -O /tmp/jekylldemo.zip https://jekylldemobucket.s3.amazonaws.com/jekylldemo.zip
# unzip /tmp/jekylldemo.zip
# pwd > /tmp/testdeploy
# ls -lrta >> /tmp/testdeploy
#
# sudo cp -R .. /home/ec2-user/apps/jekyll-demo/
echo 'start test'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment