Update build.yml #7
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 | |
| jobs: | |
| build-and-test: | |
| 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 | |
| deploy-to-eks: | |
| runs-on: ubuntu-latest | |
| needs: build-and-test | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v2 | |
| - name: Set AWS Credentials | |
| run: | | |
| 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 eu-north-1 | |
| - name: Install tools | |
| run: | | |
| curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" | |
| chmod +x kubectl | |
| sudo mv kubectl /usr/local/bin/ | |
| pip install awscli --upgrade | |
| - name: Log in to ECR | |
| run: | | |
| aws ecr get-login-password --region eu-north-1 | \ | |
| docker login --username AWS --password-stdin ${{ secrets.IMAGE_REGISTRY }} | |
| - name: Build and tag Docker image | |
| run: | | |
| LATEST_SHA=$(git rev-parse --short HEAD) | |
| echo "LATEST_SHA=$LATEST_SHA" >> $GITHUB_ENV | |
| docker build -t ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA . | |
| docker tag ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:latest | |
| - name: Push Docker image to ECR | |
| run: | | |
| docker push ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA | |
| docker push ${{ secrets.IMAGE_REGISTRY }}:latest | |
| - name: Update kubeconfig | |
| run: | | |
| aws eks update-kubeconfig --name eks-code2cloud-test --region eu-north-1 | |
| - name: Deploy to EKS | |
| run: | | |
| DEPLOYMENT_EXISTS=$(kubectl get deployment ${{ secrets.REPOSITORY }} --namespace=default --ignore-not-found) | |
| if [[ -z "$DEPLOYMENT_EXISTS" ]]; then | |
| echo "Creating new deployment" | |
| kubectl create deployment ${{ secrets.REPOSITORY }} \ | |
| --image=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA --namespace=default | |
| else | |
| echo "Updating existing deployment" | |
| kubectl set image deployment/${{ secrets.REPOSITORY }} \ | |
| ${{ secrets.REPOSITORY }}=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA --namespace=default | |
| fi |