Deploy in production #46
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: Deploy to development | |
| on: | |
| issue_comment: | |
| types: [created] | |
| env: | |
| AWS_REGION: us-east-1 | |
| ECR_REPOSITORY: app/${{ github.event.repository.name }} | |
| EKS_CLUSTER_NAME: EKS-lanchonete-cluster | |
| GH_TOKEN: ${{ github.token }} | |
| SERVICE_NAME: ${{ github.event.repository.name }} | |
| permissions: | |
| contents: read | |
| jobs: | |
| build-deploy-dev: | |
| if: contains(github.event.comment.body, '/deploy') | |
| runs-on: ubuntu-latest | |
| outputs: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - 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-session-token: ${{ secrets.AWS_SESSION_TOKEN }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Login to ECR registry | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v1 | |
| with: | |
| registry-type: private | |
| mask-password: true | |
| - name: Build container image for development | |
| id: build-image-dev | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| run: | | |
| # Build image container using commit SHA with dev suffix | |
| VERSION=${{ github.sha }}-dev | |
| echo "Building image using commit SHA: $VERSION" | |
| docker build \ | |
| --cache-from=type=registry,ref=$ECR_REGISTRY/$ECR_REPOSITORY:latest \ | |
| --cache-to=type=inline \ | |
| -t $ECR_REGISTRY/$ECR_REPOSITORY:$VERSION . | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$VERSION | |
| echo "image-version=$VERSION" >> $GITHUB_ENV | |
| - name: Setup Kubeconfig | |
| run: aws eks --region $AWS_REGION update-kubeconfig --name $EKS_CLUSTER_NAME | |
| - name: Install Kustomize if not present | |
| run: | | |
| if ! command -v kustomize &> /dev/null; then | |
| echo "Kustomize not found. Installing..." | |
| curl -sSLo kustomize.tar.gz https://github.com/kubernetes-sigs/kustomize/releases/latest/download/kustomize_linux_amd64.tar.gz | |
| tar -xvzf kustomize.tar.gz | |
| chmod +x kustomize | |
| sudo mv kustomize /usr/local/bin/ | |
| fi | |
| - name: Deploy to EKS cluster in development namespace | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| run: | | |
| source $GITHUB_ENV | |
| ENVIRONMENT=development | |
| KUSTOMIZE_DIR=$(pwd)/infra/kubernetes/$ENVIRONMENT | |
| cd $KUSTOMIZE_DIR | |
| # Check if namespace exists, if not create it | |
| kubectl get namespace $ENVIRONMENT || kubectl create namespace $ENVIRONMENT | |
| echo "Current directory: $(pwd)" && echo "List base files:" && ls -la ../base | |
| kustomize edit set image app/tc-auth-service=$ECR_REGISTRY/$ECR_REPOSITORY:${{ env.image-version }} | |
| echo "Deploying $SERVICE_NAME in $ENVIRONMENT namespace." | |
| echo "Using image version: ${{ env.image-version }}" | |
| kubectl apply -k $KUSTOMIZE_DIR -n $ENVIRONMENT || { | |
| echo "Failed to deploy $SERVICE_NAME, reverting to previous version... Please, check the logs"; | |
| kubectl rollout undo deployment/${{ env.SERVICE_NAME }} -n $ENVIRONMENT; | |
| exit 1; | |
| } | |
| echo "$SERVICE_NAME deployed successfully!" |