Docker Build+Push ECR / Helm Deploy #7
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
name: Docker Build+Push ECR | |
on: | |
workflow_dispatch: | |
env: | |
region: us-east-2 | |
jobs: | |
build-and-push: | |
name: Build and Push to ECR | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} | |
aws-region: ${{ env.region }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Fetch Parameter from SSM and set as env variable | |
run: | | |
ECR_REPO=$(aws ssm get-parameter --name "/ecr_repo_name" --query "Parameter.Value" --output text --region ${{ env.region }}) | |
echo "ECR_REPO=$ECR_REPO" >> $GITHUB_ENV | |
- name: Build, Tag, and Push the Image to Amazon ECR | |
id: build-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: ${{ env.ECR_REPO }} | |
IMAGE_TAG: ${{ github.sha }} | |
run: | | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest mysql-connect-app | |
docker push -a $ECR_REGISTRY/$ECR_REPOSITORY | |
deploy: | |
name: Deploy Helm chart | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} | |
aws-region: ${{ env.region }} | |
- uses: azure/[email protected] | |
- name: Install kubectl | |
uses: azure/setup-kubectl@v3 | |
id: install | |
- name: Fetch Parameter from SSM and set as env variable | |
run: | | |
ECR_REPO=$(aws ssm get-parameter --name "/ecr_repo_name" --query "Parameter.Value" --output text --region ${{ env.region }}) | |
echo "ECR_REPO=$ECR_REPO" >> $GITHUB_ENV | |
ECR_REGISTRY_URL=$(aws ssm get-parameter --name "/ecr_registry_url" --query "Parameter.Value" --output text --region ${{ env.region }}) | |
echo "ECR_REGISTRY_URL=$ECR_REGISTRY_URL" >> $GITHUB_ENV | |
CLUSTER_NAME=$(aws ssm get-parameter --name "/cluster_name" --query "Parameter.Value" --output text --region us-east-2) | |
echo "CLUSTER_NAME=$CLUSTER_NAME" >> $GITHUB_ENV | |
DB_ADDRESS=$(aws ssm get-parameter --name "/db_address" --query "Parameter.Value" --output text --region us-east-2) | |
echo "DB_ADDRESS=$DB_ADDRESS" >> $GITHUB_ENV | |
DB_USER=$(aws ssm get-parameter --name "/db_user" --query "Parameter.Value" --output text --region us-east-2) | |
echo "DB_USER=$DB_USER" >> $GITHUB_ENV | |
DB_PASSWORD=$(aws ssm get-parameter --name "/db_password" --with-decryption --query "Parameter.Value" --output text --region us-east-2) | |
echo "DB_PASSWORD=$DB_PASSWORD" >> $GITHUB_ENV | |
- name: Update kubeconfig | |
run: aws eks update-kubeconfig --name ${{ env.CLUSTER_NAME }} --region ${{ env.region }} | |
- name: Deploy with Helm | |
run: | | |
helm upgrade --install mysql-app ./mysql-connect-helm-chart \ | |
--set namespace="mysql-namespace" \ | |
--set mysql.dbHost="${{ env.DB_ADDRESS }}" \ | |
--set mysql.dbUser="${{ env.DB_USER }}" \ | |
--set mysql.dbPassword="${{ env.DB_PASSWORD }}" \ | |
--set mysql.image.repository="${{ env.ECR_REGISTRY_URL }}" \ | |
--set mysql.image.tag="${{ github.sha }}" |