@@ -338,56 +338,78 @@ jobs:
338338 if : always()
339339 run : docker stop solar-system-app && docker rm solar-system-app
340340
341- # Stage 11: Deploy to Azure VM
342- # deploy-azure:
343- # name: Deploy to Azure VM
344- # runs-on: ubuntu-latest
345- # needs: [container-scan, dast-zap]
346- # if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
347- # permissions:
348- # contents: read
349- # packages: read
350- # environment:
351- # name: production
352- # url: http ://${{ secrets.AZURE_VM_IP }}:3000
341+ # Stage 11: Deploy Infrastructure and Application to Azure
342+ deploy-azure :
343+ name : Deploy to Azure Web App
344+ runs-on : ubuntu-latest
345+ needs : [container-scan, dast-zap]
346+ if : github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
347+ permissions :
348+ contents : read
349+ packages : read
350+ environment :
351+ name : production
352+ url : https ://${{ secrets.AZURE_WEBAPP_NAME }}.azurewebsites.net
353353
354- # steps:
355- # - name: Deploy on Azure VM
356- # uses: appleboy/ssh-action@master
357- # with:
358- # host: ${{ secrets.AZURE_VM_IP }}
359- # username: ${{ secrets.AZURE_VM_USERNAME }}
360- # key: ${{ secrets.AZURE_VM_SSH_KEY }}
361- # script: |
362- # # Log in to GitHub Container Registry
363- # echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
364-
365- # # Pull the latest image
366- # docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}-${{ github.sha }}
367-
368- # # Stop and remove old container if exists
369- # docker stop solar-system || true
370- # docker rm solar-system || true
371-
372- # # Run new container
373- # docker run -d --name solar-system \
374- # -p 3000:3000 \
375- # --restart unless-stopped \
376- # -e MONGO_URI="${{ secrets.MONGO_URI }}" \
377- # ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}-${{ github.sha }}
378-
379- # # Clean up old images (keep last 3)
380- # docker image prune -af --filter "until=72h"
381-
382- # # Verify deployment
383- # sleep 5
384- # curl -f http://localhost:3000/ready || exit 1
385-
386- # echo "Deployment successful!"
387- # docker ps | grep solar-system
388-
389- # - name: Health check
390- # run: |
391- # sleep 10
392- # curl -f http://${{ secrets.AZURE_VM_IP }}:3000/ready || exit 1
393- # echo "Application is healthy and running!"
354+ steps :
355+ - name : Checkout code
356+ uses : actions/checkout@v4
357+
358+ - name : Log in to Azure
359+ uses : azure/login@v1
360+ with :
361+ creds : ${{ secrets.AZURE_CREDENTIALS }}
362+
363+ - name : Setup Terraform
364+ uses : hashicorp/setup-terraform@v3
365+ with :
366+ terraform_version : 1.6.0
367+
368+ - name : Terraform Init
369+ run : terraform init
370+ working-directory : ./terraform
371+
372+ - name : Terraform Plan
373+ run : |
374+ terraform plan \
375+ -var="app_name=${{ secrets.AZURE_WEBAPP_NAME }}" \
376+ -var="github_username=${{ github.repository_owner }}" \
377+ -var="github_token=${{ secrets.GITHUB_TOKEN }}" \
378+ -var="mongo_uri=${{ secrets.MONGO_URI }}" \
379+ -var="docker_image=${{ github.repository_owner }}/solar-system:${{ github.ref_name }}" \
380+ -out=tfplan
381+ working-directory : ./terraform
382+ env :
383+ ARM_CLIENT_ID : ${{ secrets.ARM_CLIENT_ID }}
384+ ARM_CLIENT_SECRET : ${{ secrets.ARM_CLIENT_SECRET }}
385+ ARM_SUBSCRIPTION_ID : ${{ secrets.ARM_SUBSCRIPTION_ID }}
386+ ARM_TENANT_ID : ${{ secrets.ARM_TENANT_ID }}
387+
388+ - name : Terraform Apply
389+ run : terraform apply -auto-approve tfplan
390+ working-directory : ./terraform
391+ env :
392+ ARM_CLIENT_ID : ${{ secrets.ARM_CLIENT_ID }}
393+ ARM_CLIENT_SECRET : ${{ secrets.ARM_CLIENT_SECRET }}
394+ ARM_SUBSCRIPTION_ID : ${{ secrets.ARM_SUBSCRIPTION_ID }}
395+ ARM_TENANT_ID : ${{ secrets.ARM_TENANT_ID }}
396+
397+ - name : Get Web App URL
398+ id : webapp
399+ run : |
400+ WEBAPP_URL=$(terraform output -raw webapp_url)
401+ echo "url=$WEBAPP_URL" >> $GITHUB_OUTPUT
402+ working-directory : ./terraform
403+
404+ - name : Restart Web App (force pull latest image)
405+ run : |
406+ az webapp restart \
407+ --name ${{ secrets.AZURE_WEBAPP_NAME }} \
408+ --resource-group $(terraform output -raw resource_group_name)
409+ working-directory : ./terraform
410+
411+ - name : Health check
412+ run : |
413+ sleep 60
414+ curl -f ${{ steps.webapp.outputs.url }}/ready || exit 1
415+ echo "Application is healthy and running on Azure Web App!"
0 commit comments