Skip to content

Cloud Demo Environment Provisioning #64

Cloud Demo Environment Provisioning

Cloud Demo Environment Provisioning #64

name: Cloud Demo Environment Provisioning
on:
workflow_dispatch:
inputs:
name:
description: 'Username'
required: true
type:
description: 'Type of environment to provision (eap or production)'
required: true
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-east-1
KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
ECR_REPOSITORY: ld-core-demo
KUBECTL_VERSION: "v1.23.0"
DEMO_NAMESPACE: ${{ github.event.inputs.name }}
LD_PROJECT_KEY: ${{ github.event.inputs.name }}-ld-demo
jobs:
provisioning_demo_environment:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.9"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r ./.github/workflows/demo_provisioning_scripts/requirements.txt
- name: Set LD_API_KEY
run: echo "LD_API_KEY=$(if [ '${{ github.event.inputs.type }}' == 'production' ]; then echo '${{ secrets.LD_API_KEY }}'; elif [ '${{ github.event.inputs.type }}' == 'eap' ]; then echo '${{ secrets.LD_EAP_API_KEY }}'; else echo '${{ secrets.LD_API_KEY }}'; fi)" >> $GITHUB_ENV
- name: Create / Setup LaunchDarkly Project
id: ld_project_setup
run: |
echo "Creating and Setting up LaunchDarkly project for namespace: ${{ env.DEMO_NAMESPACE }}"
python ./.github/workflows/demo_provisioning_scripts/DemoBuilder.py
- name: Create .env file for demo pod
run: |
touch ./.env.production
echo NEXT_PUBLIC_LD_CLIENT_KEY=${{ env.LD_CLIENT_KEY }} >> ./.env.production
echo LD_SDK_KEY=${{ env.LD_SDK_KEY }} >> ./.env.production
echo DB_URL=${{ secrets.DB_URL }} >> ./.env.production
echo LD_API_KEY=${{ env.LD_API_KEY }} >> ./.env.production
echo DESTINATIONENV=production >> ./.env.production
echo PROJECT_KEY=${{ env.LD_PROJECT_KEY }} >> ./.env.production
echo AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY }} >> ./.env.production
echo AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} >> ./.env.production
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ld-core-demo
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:${{ env.DEMO_NAMESPACE }}-${{ github.run_id }} .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:${{ env.DEMO_NAMESPACE }}-${{ github.run_id }}
- name: Update K8s Deploy File
run: python ./.github/workflows/update_k8s_deploy_file.py
env:
NAMESPACE: ${{ env.DEMO_NAMESPACE }}
URL: ${{ env.DEMO_NAMESPACE }}.launchdarklydemos.com
IMAGE: ${{ steps.login-ecr.outputs.registry }}/ld-core-demo:${{ env.DEMO_NAMESPACE }}-${{ github.run_id }}
- name: Check Namespace in Kubernetes
uses: kodermax/kubectl-aws-eks@master
with:
args: get namespace ${{ env.DEMO_NAMESPACE }} &>/dev/null && echo "namespace_exists=true" >> $GITHUB_ENV || echo "namespace_exists=false" >> $GITHUB_ENV
- name: Create Namespace In Kubernetes
if: env.namespace_exists == 'false'
uses: kodermax/kubectl-aws-eks@master
with:
args: create namespace ${{ env.DEMO_NAMESPACE }}
- name: Applying deploy file to Kubernetes
uses: kodermax/kubectl-aws-eks@master
with:
args: apply -f ./.github/workflows/deploy_files/deploy.yaml -n ${{ env.DEMO_NAMESPACE }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Add Route 53 Record
run: |
HOSTED_ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name "launchdarklydemos.com." --query "HostedZones[0].Id" --output text)
RECORD_SET_JSON='{
"Comment": "Creating Alias record that points to ALB",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "${{ env.DEMO_NAMESPACE }}.launchdarklydemos.com",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "Z35SXDOTRQ7X7K",
"DNSName": "k8s-ldcoredemolb-93af62c3af-1176236694.us-east-1.elb.amazonaws.com",
"EvaluateTargetHealth": false
}
}
}
]
}'
aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --change-batch "$RECORD_SET_JSON"
env:
AWS_REGION: 'us-east-1'
- name: Delete the deploy file
run: rm -rf ./.github/workflows/deploy_files
- name: Remove .env file
run: rm ./.env.production
- name: LaunchDarkly Code References
uses: launchdarkly/[email protected]
with:
accessToken: ${{ env.LD_API_KEY }}
projKey: ${{ env.LD_PROJECT_KEY }}
repoName: main