eShopOnWeb CI/CD #12
This file contains 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: eShopOnWeb CI/CD | |
# Triggers | |
on: | |
pull_request: | |
push: | |
branches: | |
- main | |
paths: | |
- "src/Web/**" | |
workflow_dispatch: | |
# Environment variables | |
env: | |
RESOURCE-GROUP: ivey-az400-github | |
LOCATION: uksouth | |
TEMPLATE-FILE: .azure/bicep/webapp.bicep | |
SUBSCRIPTION-ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
WEBAPP-NAME: ivey-az400-webapp | |
jobs: | |
# Build and test | |
buildandtest: | |
runs-on: ubuntu-latest | |
outputs: | |
test_results: ${{ steps.unittestoutput.outputs.test_results }} | |
test_outcome: ${{ steps.unittestoutput.outputs.test_outcome }} | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v1 | |
with: | |
dotnet-version: '6.0.x' | |
include-prerelease: true | |
- name: Build with dotnet | |
run: dotnet build ./eShopOnWeb.sln --configuration Release | |
- name: Test with dotnet | |
id: unittest | |
run: | | |
dotnet test ./eShopOnWeb.sln --configuration Release --no-build --filter "FullyQualifiedName~UnitTest" |& tee test_results.txt | |
# break if 'dotnet test' failed | |
test ${PIPESTATUS[0]} -eq 0 | |
continue-on-error: true | |
# Create string output | |
- name: Create output from tests | |
run: | | |
RESULT=$(cat test_results.txt) | |
echo "## Unit Tests Output" >> result.string | |
echo "<details><summary>Click to view unit tests output</summary>" >> result.string | |
echo "" >> result.string | |
echo '```' >> result.string | |
echo "$RESULT" >> result.string | |
echo '```' >> result.string | |
echo "</details>" >> result.string | |
SUMMARY=$(cat result.string) | |
SUMMARY="${SUMMARY//'%'/'%25'}" | |
SUMMARY="${SUMMARY//$'\n'/'%0A'}" | |
SUMMARY="${SUMMARY//$'\r'/'%0D'}" | |
echo "{summary}=$SUMMARY" >> $GITHUB_OUTPUT | |
# Publish to task summary | |
- name: Publish to Task Summary | |
run: | | |
cat result.string >> $GITHUB_STEP_SUMMARY | |
- name: dotnet publish | |
run: dotnet publish ./src/Web/Web.csproj -c Release -o ${{env.DOTNET_ROOT}}/myapp | |
- name: Upload artifact for deployment job | |
uses: actions/upload-artifact@v3 | |
with: | |
name: .net-app | |
path: ${{env.DOTNET_ROOT}}/myapp | |
- name: Upload artifact for deployment job | |
uses: actions/upload-artifact@v3 | |
with: | |
name: bicep-template | |
path: ${{ env.TEMPLATE-FILE }} | |
# Use Bicep to deploy infrastructure | |
deploy: | |
runs-on: ubuntu-latest | |
needs: buildandtest | |
if: ${{ github.event_name != 'pull_request' }} | |
environment: | |
name: 'Development' | |
url: "${{ steps.deploy-to-webapp.outputs.webapp-url }}" | |
steps: | |
# Download the publish files created in previous job | |
- name: Download artifact from build job | |
uses: actions/download-artifact@v3 | |
with: | |
name: .net-app | |
path: .net-app | |
# Download the bicep templates from previous job | |
- name: Download artifact from build job | |
uses: actions/download-artifact@v3 | |
with: | |
name: bicep-template | |
path: bicep-template | |
- name: Azure Login | |
uses: azure/login@v1 | |
with: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
# Create resource group | |
- name: Create resource group | |
uses: azure/cli@v2 | |
with: | |
azcliversion: 2.30.0 | |
inlineScript: az group create -n ${{ env.RESOURCE-GROUP }} -l ${{ env.LOCATION }} --tags course=AZ-400 | |
# Deploy Azure WebApp using Bicep file | |
- name: deploy | |
uses: azure/arm-deploy@v1 | |
with: | |
subscriptionId: ${{ env.SUBSCRIPTION-ID }} | |
resourceGroupName: ${{ env.RESOURCE-GROUP }} | |
template: bicep-template/webapp.bicep | |
parameters: 'webAppName=${{ env.WEBAPP-NAME }} location=${{ env.LOCATION }}' | |
failOnStdErr: false | |
# Publish website to Azure App Service (WebApp) | |
- name: Publish Website to WebApp | |
uses: Azure/webapps-deploy@v2 | |
id: deploy-to-webapp | |
with: | |
app-name: ${{ env.WEBAPP-NAME }} | |
package: .net-app | |