[CLI, Backend] fix: unpublish fix (#354) #68
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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'apps/backend/**' | |
jobs: | |
build_and_deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- 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: ${{ secrets.AWS_REGION }} | |
- name: Login to Amazon ECR | |
run: | | |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }} | |
- name: Check latest image version in ECR | |
id: check_version | |
env: | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} | |
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }} | |
run: | | |
VERSION=$(cat apps/backend/package.json | jq -r '.version') | |
LATEST_ECR_VERSION=$(aws ecr describe-images --repository-name $ECR_REPOSITORY_BACKEND_NAME --region $AWS_REGION --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' --output text) | |
if [ "$VERSION" == "$LATEST_ECR_VERSION" ]; then | |
echo "::set-output name=should_deploy::false" | |
exit 0 | |
else | |
echo "::set-output name=should_deploy::true" | |
fi | |
- name: Build Docker image | |
if: steps.check_version.outputs.should_deploy == 'true' | |
env: | |
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }} | |
run: | | |
docker build -f apps/backend/Dockerfile --no-cache --progress=plain -t $ECR_REPOSITORY_BACKEND_NAME . | |
- name: Tag Docker image | |
if: steps.check_version.outputs.should_deploy == 'true' | |
env: | |
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} | |
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }} | |
run: | | |
VERSION=$(cat apps/backend/package.json | jq -r '.version') | |
docker tag $ECR_REPOSITORY_BACKEND_NAME:latest $ECR_REGISTRY/$ECR_REPOSITORY_BACKEND_NAME:$VERSION | |
- name: Push Docker image to Amazon ECR | |
if: steps.check_version.outputs.should_deploy == 'true' | |
env: | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} | |
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }} | |
run: | | |
VERSION=$(cat apps/backend/package.json | jq -r '.version') | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY_BACKEND_NAME:$VERSION | |
- name: Deploy to EC2 | |
if: steps.check_version.outputs.should_deploy == 'true' | |
env: | |
AWS_SSH_KEY: ${{ secrets.AWS_SSH_KEY }} | |
AWS_EC2_HOST: ${{ secrets.AWS_EC2_HOST }} | |
AWS_EC2_USER_NAME: ${{ secrets.AWS_EC2_USER_NAME }} | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} | |
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }} | |
run: | | |
echo "$AWS_SSH_KEY" > private_key.pem | |
chmod 600 private_key.pem | |
ssh -o StrictHostKeyChecking=no -i private_key.pem $AWS_EC2_USER_NAME@$AWS_EC2_HOST ' | |
export AWS_REGION='"$AWS_REGION"' | |
export ECR_REGISTRY='"$ECR_REGISTRY"' | |
export ECR_REPOSITORY_BACKEND_NAME='"$ECR_REPOSITORY_BACKEND_NAME"' | |
aws ecr get-login-password --region $AWS_REGION | sudo docker login --username AWS --password-stdin $ECR_REGISTRY | |
LATEST_IMAGE_VERSION=$(aws ecr describe-images --repository-name $ECR_REPOSITORY_BACKEND_NAME --region $AWS_REGION --query "sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]" --output text) | |
echo "Latest image version: $LATEST_IMAGE_VERSION" | |
cd nginx/ | |
sed -i "s|studio-backend:[^[:space:]]*|studio-backend:$LATEST_IMAGE_VERSION|g" docker-compose.yml | |
if [ $? -eq 0 ]; then | |
echo "Image version updated successfully in docker-compose.yml." | |
else | |
echo "Failed to update image version in docker-compose.yml." | |
exit 1 | |
fi | |
sudo docker compose down | |
sudo docker compose pull | |
sudo docker compose up -d | |
' |