Update build.yml #9
This file contains hidden or 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 | |
| on: | |
| push: | |
| branches: | |
| - main # or specify your branch here | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v2 | |
| - name: Set up Python | |
| uses: actions/setup-python@v2 | |
| with: | |
| python-version: '3.8' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: Set up AWS Credentials | |
| run: | | |
| echo "${{ secrets.AWS_ACCESS_KEY_ID }}" > aws_access_key_id | |
| echo "${{ secrets.AWS_SECRET_ACCESS_KEY }}" > aws_secret_access_key | |
| aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws configure set region ${{ secrets.AWS_REGION }} | |
| - name: Log in to AWS ECR | |
| run: | | |
| aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.IMAGE_REGISTRY }} | |
| - name: Build Docker image | |
| run: | | |
| LATEST_SHA=$(git rev-parse HEAD) | |
| docker build -t ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA . | |
| docker tag ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:latest | |
| - name: Push Docker image to AWS ECR | |
| run: | | |
| docker push ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA | |
| docker push ${{ secrets.IMAGE_REGISTRY }}:latest | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build-and-push # Ensures the deploy job runs only after the build-and-push job | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v2 | |
| - name: Set up AWS Credentials | |
| run: | | |
| echo "${{ secrets.AWS_ACCESS_KEY_ID }}" > aws_access_key_id | |
| echo "${{ secrets.AWS_SECRET_ACCESS_KEY }}" > aws_secret_access_key | |
| aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws configure set region ${{ secrets.AWS_REGION }} | |
| - name: Update kubeconfig for EKS | |
| run: | | |
| aws eks update-kubeconfig --name ${{ secrets.CLUSTER_NAME }} --region ${{ secrets.AWS_REGION }} | |
| - name: Check if the deployment exists | |
| id: deployment | |
| run: | | |
| DEPLOYMENT_EXISTS=$(kubectl get deployment ${{ secrets.DEPLOYMENT_NAME }} -n default --ignore-not-found) | |
| echo "Deployment exists: $DEPLOYMENT_EXISTS" | |
| echo "::set-output name=exists::$DEPLOYMENT_EXISTS" | |
| - name: Create or update deployment in EKS | |
| run: | | |
| if [[ "${{ steps.deployment.outputs.exists }}" == "null" ]]; then | |
| echo "Deployment does not exist. Creating deployment." | |
| kubectl create deployment ${{ secrets.DEPLOYMENT_NAME }} --image=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default | |
| else | |
| echo "Deployment exists. Updating deployment." | |
| kubectl set image deployment/${{ secrets.DEPLOYMENT_NAME }} ${{ secrets.DEPLOYMENT_NAME }}=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default | |
| fi |