Skip to content

updated ci cd

updated ci cd #7

Workflow file for this run

# build.yml
on:
# pull_request:
# paths:
# - "**"
push:
# paths:
# - "**"
branches: # array of glob patterns matching against refs/heads. Optional; defaults to all
- develop # triggers on pushes that contain changes in develop
- staging # triggers on pushes that contain changes in staging
- master # triggers on pushes that contain changes in master
name: Build
# https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
env:
AWS_DEFAULT_REGION: ${{ vars.AWS_DEFAULT_REGION }}
AWS_DEFAULT_OUTPUT: ${{ vars.AWS_DEFAULT_OUTPUT }}
AWS_ACCESS_KEY_ID: ${{ vars.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
ECR_USERNAME: ${{vars.ECR_USERNAME}}
CONTAINER_IMAGE_DEVELOP: studio-api:develop
CONTAINER_IMAGE_STAGE: studio-api:stage
CONTAINER_IMAGE_STUDIO: studio-api:studio
CONTAINER_IMAGE_PROD: studio-api:prod
CRON_IMAGE_DEVELOP: studio-cron:develop
AWS_ACCOUNT_ID: ${{vars.AWS_ACCOUNT_ID}}
DEV_SSH_HOST: ${{secrets.DEV_SSH_HOST}}
H5P_DEV_BRANCH: develop
H5P_STAGE_BRANCH: staging
H5P_PROD_BRANCH: master
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
jobs:
build-and-push:
name: Build and deploy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@main
- name: Setup ECR
run: |
# Login to AWS ECR
aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username $ECR_USERNAME --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
- name: Build and tag the image
run: |
# Build and tag the image
if [ ${{ github.ref }} = 'refs/heads/develop' ]; then
docker build \
-t $CONTAINER_IMAGE_DEVELOP \
-t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$CONTAINER_IMAGE_DEVELOP -f Dockerfile.8.1 ./
docker build \
-t $CRON_IMAGE \
-t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$CRON_IMAGE -f Dockerfile.cron.8.1 --build-arg H5PBRANCH=$H5P_DEV_BRANCH ./
elif [ ${{ github.ref }} = 'refs/heads/master' ]; then
echo "In Master"
else
echo "No specific config."
fi
- name: Push Image
run: |
# Push image to AWS ECR
if [ ${{ github.ref }} = 'refs/heads/develop' ]; then
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$CONTAINER_IMAGE_DEVELOP
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$CRON_IMAGE_DEVELOP
elif [ ${{ github.ref }} = 'refs/heads/master' ]; then
echo "In Master"
else
echo "No specific config."
fi
- name: Deploy
run: |
# Deploy to Instance
export DOCKER_IMAGE_NAME=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$CONTAINER_IMAGE_DEVELOP
export DOCKER_CRON_IMAGE_NAME=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$CRON_IMAGE_DEVELOP
echo "$SSH_PRIVATE_KEY" | base64 -d > ssh_key.pem
chmod 600 ssh_key.pem
if [ ${{ github.ref }} = 'refs/heads/develop' ]; then
ssh -o StrictHostKeyChecking=no -i ssh_key.pem ${DEV_SSH_HOST} "aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username $ECR_USERNAME --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com && cd /curriki && docker service update currikistack_currikiprod-api"
elif [ ${{ github.ref }} = 'refs/heads/master' ]; then
echo "In Master"
else
echo "No specific config."
fi