Docker & .NET App CI/CD #3
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: Docker & .NET App CI/CD | |
| on: | |
| workflow_dispatch: | |
| env: | |
| EXPECTED_RESPONSE: "Hello World!" | |
| AZURE_WEBAPP_PACKAGE_PATH: ./publish | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v3 | |
| with: | |
| dotnet-version: 8.0.x | |
| - name: Restore dependencies | |
| run: dotnet restore | |
| - name: Build | |
| run: dotnet build --configuration Release --no-restore | |
| test: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v3 | |
| with: | |
| dotnet-version: 8.0.x | |
| - name: Restore dependencies | |
| run: dotnet restore | |
| - name: Run tests | |
| run: dotnet test | |
| build-docker: | |
| runs-on: ubuntu-latest | |
| needs: [build, test] | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| - name: Log in to registry | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: https://index.docker.io/v1/ | |
| username: ${{ secrets.AzureAppService_ContainerUsername_007dc16c00194a579ee0e182f3912414 }} | |
| password: ${{ secrets.AzureAppService_ContainerPassword_03b591a7c15d426aa4439cb5a79e30b8 }} | |
| - name: Build and push container image to registry | |
| uses: docker/build-push-action@v3 | |
| with: | |
| push: true | |
| tags: forsanta/webapi-starter-dotnet8:${{ github.sha }} | |
| file: ./Dockerfile | |
| build-binary: | |
| needs: [build, test] | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v3 | |
| with: | |
| dotnet-version: 8.0.x | |
| - name: Restore dependencies | |
| run: dotnet restore | |
| - name: Build | |
| run: dotnet build --configuration Release --no-restore | |
| - name: Publish app | |
| run: dotnet publish -c Release -o publish | |
| - name: Upload build artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: dotnet-publish | |
| path: publish/ | |
| deploy-binary: | |
| permissions: | |
| contents: none | |
| runs-on: ubuntu-latest | |
| needs: build-binary | |
| environment: | |
| name: 'Development' | |
| outputs: | |
| webapp-url: ${{ steps.set-url.outputs.webapp-url }} | |
| steps: | |
| - name: Download artifact from build job | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: dotnet-publish | |
| path: ./publish | |
| - name: Deploy to Azure Web App | |
| id: deploy-to-webapp | |
| uses: azure/webapps-deploy@v2 | |
| with: | |
| app-name: app-as-code | |
| publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} | |
| package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} | |
| - name: Set webapp-url output | |
| id: set-url | |
| run: echo "webapp-url=https://app-as-code.azurewebsites.net" >> $GITHUB_OUTPUT | |
| deploy-docker: | |
| runs-on: ubuntu-latest | |
| needs: [build-docker] | |
| environment: | |
| name: production | |
| outputs: | |
| webapp-url: ${{ steps.set-url.outputs.webapp-url }} | |
| steps: | |
| - name: Deploy to Azure Web App with Docker | |
| id: deploy-to-webapp | |
| uses: azure/webapps-deploy@v2 | |
| with: | |
| app-name: fefezfezf | |
| slot-name: production | |
| publish-profile: ${{ secrets.AzureAppService_PublishProfile_9b585dbfa0374924ab161fec5b2dea00 }} | |
| images: forsanta/webapi-starter-dotnet8:${{ github.sha }} | |
| - name: Set webapp-url output | |
| id: set-url | |
| run: echo "webapp-url=https://fefezfezf.azurewebsites.net" >> $GITHUB_OUTPUT | |
| check-url-docker: | |
| runs-on: ubuntu-latest | |
| needs: deploy-docker | |
| steps: | |
| - name: Vérifier que l’URL retourne la valeur attendue | |
| run: | | |
| url="${{ needs.deploy-docker.outputs.webapp-url }}" | |
| echo "Test de l'URL : $url" | |
| response=$(curl -s "$url") | |
| echo "Réponse : $response" | |
| if [ "$response" != "$EXPECTED_RESPONSE" ]; then | |
| echo "❌ L'URL ne retourne pas '$EXPECTED_RESPONSE'" | |
| exit 1 | |
| fi | |
| echo "✅ L'URL retourne bien '$EXPECTED_RESPONSE'" | |
| check-url-binary: | |
| runs-on: ubuntu-latest | |
| needs: deploy-binary | |
| steps: | |
| - name: Vérifier que l’URL retourne la valeur attendue | |
| run: | | |
| url="${{ needs.deploy-binary.outputs.webapp-url }}" | |
| echo "Test de l'URL : $url" | |
| response=$(curl -s "$url") | |
| echo "Réponse : $response" | |
| if [ "$response" != "$EXPECTED_RESPONSE" ]; then | |
| echo "❌ L'URL ne retourne pas '$EXPECTED_RESPONSE'" | |
| exit 1 | |
| fi | |
| echo "✅ L'URL retourne bien '$EXPECTED_RESPONSE'" |