|
| 1 | +# BEGIN |
| 2 | +# Workflow Name: Automated Build and Deployment of Container Apps to Azure Web App |
| 3 | +name: Build and deploy container app to Azure Web App |
| 4 | +# Workflow triggers |
| 5 | +on: |
| 6 | + # Trigger when a push occurs on the "api" branch or when any tag is created |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - api |
| 10 | + tags: |
| 11 | + - '*' # Matches any tag |
| 12 | + # Allows manual triggering of the workflow via the GitHub Actions interface |
| 13 | + workflow_dispatch: |
| 14 | + # Trigger when a release is published |
| 15 | + release: |
| 16 | + types: [published] |
| 17 | +# Define the jobs in the workflow |
| 18 | +jobs: |
| 19 | + # Job 1: Build process |
| 20 | + build: |
| 21 | + # Runs on the latest Ubuntu host |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + # 1. Check out the repository code into the workflow |
| 25 | + - uses: actions/checkout@v2 |
| 26 | + # 2. Set up Docker Buildx, a tool for advanced Docker image building |
| 27 | + - name: Set up Docker Buildx |
| 28 | + uses: docker/setup-buildx-action@v2 |
| 29 | + # 3. Log in to the Azure Container Registry (ACR) |
| 30 | + # Requires credentials stored in GitHub Secrets |
| 31 | + - name: Log in to registry |
| 32 | + uses: docker/login-action@v1 |
| 33 | + with: |
| 34 | + registry: ${{ secrets.DOCKER_REGISTRY }} |
| 35 | + username: PushToken |
| 36 | + password: ${{ secrets.DOCKER_REGISTRY_SERVER_PASSWORD }} |
| 37 | + # 4. Convert the repository name to lowercase |
| 38 | + # Docker image names must be lowercase |
| 39 | + - name: Convert repo name to lowercase |
| 40 | + id: lowercase_repo_name |
| 41 | + run: echo "repo_name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 42 | + # 5. Build the Docker image and push it to the Azure Container Registry |
| 43 | + - name: Build and push container without tag |
| 44 | + uses: docker/build-push-action@v4 |
| 45 | + if: ${{ github.sha }} != "" |
| 46 | + with: |
| 47 | + push: true |
| 48 | + tags: ${{ secrets.DOCKER_REPO }}/${{ env.repo_name }}:${{ github.sha }} # Use Git commit SHA as the tag |
| 49 | + context: ./ # The build context is the root of the repository |
| 50 | + |
| 51 | + - name: Build and push container with tag |
| 52 | + uses: docker/build-push-action@v2 |
| 53 | + if: startsWith(github.ref, 'refs/tags/v') |
| 54 | + with: |
| 55 | + push: true |
| 56 | + tags: ${{ secrets.DOCKER_REPO }}/${{ env.repo_name }}:${{ github.ref_name }} |
| 57 | + file: ./ |
| 58 | + # Job 2: Deploy to the development environment |
| 59 | + deploy-dev: |
| 60 | + # Runs on the latest Ubuntu host |
| 61 | + runs-on: ubuntu-latest |
| 62 | + # This job depends on the "build" job and will run after it completes successfully |
| 63 | + needs: build |
| 64 | + # Define the deployment environment as "development" |
| 65 | + environment: |
| 66 | + name: 'development' |
| 67 | + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} |
| 68 | + steps: |
| 69 | + # 1. Convert the repository name to lowercase (reuse logic for consistent naming) |
| 70 | + - name: Convert repo name to lowercase |
| 71 | + id: lowercase_repo_name |
| 72 | + run: echo "repo_name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 73 | + # 2. Deploy the container image to the Azure Web App (Development Slot) |
| 74 | + - name: Deploy to Azure Web App |
| 75 | + id: deploy-to-webapp |
| 76 | + uses: azure/webapps-deploy@v2 |
| 77 | + with: |
| 78 | + app-name: ${{ env.repo_name }} # Use the lowercase repository name as the app name |
| 79 | + slot-name: 'dev' # Specify the development slot |
| 80 | + publish-profile: ${{ secrets.PUBLISH_PROFILE_DEV }} # Publish profile for the development environment |
| 81 | + images: ${{ secrets.DOCKER_REPO }}/${{ env.repo_name }}:${{ github.sha }} # Use the image from the ACR with the Git commit SHA |
| 82 | + - name: Deploy to Azure Web App (tag) |
| 83 | + uses: azure/webapps-deploy@v2 |
| 84 | + if: startsWith(github.ref, 'refs/tags/v') |
| 85 | + with: |
| 86 | + app-name: ${{ env.repo_name }} |
| 87 | + slot-name: 'dev' |
| 88 | + publish-profile: ${{ secrets.PUBLISH_PROFILE_DEV }} |
| 89 | + images: ${{ secrets.DOCKER_REPO }}/${{ env.repo_name }}:${{ github.ref_name }} |
| 90 | + # Job 3: Deploy to the production environment |
| 91 | + deploy-prod: |
| 92 | + # Runs on the latest Ubuntu host |
| 93 | + runs-on: ubuntu-latest |
| 94 | + if: startsWith(github.ref, 'refs/tags/v') # Only run this job for tags that start with 'v' |
| 95 | + # This job depends on the "deploy-dev" job and will run after it completes successfully |
| 96 | + needs: deploy-dev |
| 97 | + # Define the deployment environment as "production" |
| 98 | + environment: |
| 99 | + name: 'production' |
| 100 | + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} |
| 101 | + steps: |
| 102 | + # 1. Convert the repository name to lowercase (reuse logic for consistent naming) |
| 103 | + - name: Convert repo name to lowercase |
| 104 | + id: lowercase_repo_name |
| 105 | + run: echo "repo_name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 106 | + # 2. Deploy the container image to the Azure Web App (Production Slot) |
| 107 | + - name: Deploy to Azure Web App |
| 108 | + id: deploy-to-webapp |
| 109 | + uses: azure/webapps-deploy@v2 |
| 110 | + with: |
| 111 | + app-name: ${{ env.repo_name }} # Use the lowercase repository name as the app name |
| 112 | + slot-name: 'production' # Specify the production slot |
| 113 | + publish-profile: ${{ secrets.PUBLISH_PROFILE }} # Publish profile for the production environment |
| 114 | + images: ${{ secrets.DOCKER_REPO }}/${{ env.repo_name }}:${{ github.sha }} # Use the image from the ACR with the Git commit SHA |
| 115 | +# END |
0 commit comments