Hey you.
If you're here it probably means four things:
You're using an Azure Static Web App (SWA)
You're using GitHub Actions
You're deploying the SWA with the Azure/static-web-apps-deploy GitHub Action
You're looking into running your test suite (maybe with ๐ญ Playwright) against a preview environment while a Pull Request is open
If this is you, you don't have to look any further because I got you covered!
When you create an Azure Static Web App within the Azure Portal and configure it to use GitHub with GitHub Actions, it automatically generates a GitHub workflow for you. The workflow file is also added to your GitHub repository, and it defines some pre-configured steps to build and deploy your SWA.
When I migrated my blog to be a SWA, the workflow that was generated looked like this:
name: Azure Static Web Apps CI/CD
on:push:branches:- main
pull_request:types:[opened, synchronize, reopened, closed]branches:- main
jobs:build_and_deploy_job:if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
name: Build and Deploy Job
steps:-uses: actions/checkout@v2
with:submodules:true-name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_SALMON_ROCK_0FB035B03 }}repo_token: ${{ secrets.GITHUB_TOKEN }}# Used for Github integrations (i.e. PR comments)action:"upload"###### Repository/Build Configurations - These values can be configured to match your app requirements. ####### For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfigapp_location:"/"# App source code pathapi_location:""# Api source code path - optionaloutput_location:"build"# Built app content directory - optional###### End of Repository/Build Configurations ######close_pull_request_job:if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
steps:-name: Close Pull Request
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
with:azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_SALMON_ROCK_0FB035B03 }}action:"close"
The workflow uses Azure/static-web-apps-deploy GitHub Action to build the application, and deploy it to your Azure resource.
The workflow is triggered when a pull request is opened, or when a push is made against the main branch.
What I was missing here is a way to make sure I don't break my blog when I make changes.
Believe me when I say that the timing is always bad when something is broken.
This happened to me numerous times in the past, especially because my blog is written with SvelteKit, which is undergoing changes regularly toward a stable v1 release.
I don't want to manually verify my blog I make changes because this can be time-consuming, and I always forgot something.
That's why I wanted to add a safety check within the workflow.
Automated tests to check if everything is working as expected.
Of course, I immediately grabbed to Playwright for this task.
Just like the GitHub workflow that was generated, Playwright is also able to create a workflow for you.
Just install Playwright with the init script, and work your way through the step wizard.
npm init playwright
When you've completed the wizard, you'll end up with a new GitHub workflow file.
In my case, this looked like the following file.
name: Azure Static Web Apps CI/CD
on:push:branches:- main
pull_request:types:[opened, synchronize, reopened, closed]branches:- main
jobs:build_and_deploy_job:if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
name: Build and Deploy Job
steps:-uses: actions/checkout@v2
with:submodules:true-name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_SALMON_ROCK_0FB035B03 }}repo_token: ${{ secrets.GITHUB_TOKEN }}# Used for Github integrations (i.e. PR comments)action:"upload"###### Repository/Build Configurations - These values can be configured to match your app requirements. ####### For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfigapp_location:"/"# App source code pathapi_location:""# Api source code path - optionaloutput_location:"build"# Built app content directory - optional###### End of Repository/Build Configurations ######smoke_test_job:name: โ๏ธ Smoke test ${{ github.event.deployment_status.target_url }}runs-on: ubuntu-lateststeps:-name: ๐๏ธ Checkoutuses: actions/checkout@v3-name: ๐ข Use Node.js 16uses: actions/setup-node@v3with:node-version:16-name: ๐ Install dependenciesrun: npm install-name: ๐ญ Install Playwrightrun: npx playwright install --with-deps-name: ๐งช Run Playwright Testsrun: npm run test-name: ๐ฆ Upload Test Results Artifactuses: actions/upload-artifact@v3if: always()with:name: playwright-test-resultspath: playwright-reportclose_pull_request_job:if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
steps:-name: Close Pull Request
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
with:azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_SALMON_ROCK_0FB035B03 }}action:"close"
To get the most value out of these tests at this stage, you have to run them against the deployed environment.
Therefore, we need to know the deployed URL.
The docs mention there's a convention to build the preview URL, which looks like https://<SUBDOMAIN-PULL_REQUEST_ID>.<AZURE_REGION>.azurestaticapps.net. But this also doesn't take into account that we also have our live environment when we push to main. Ideally, we also want to verify
that this continues to work.
But there has to be a better way.
Luckily, there's the output variable static_web_app_url that's defined on the Azure/static-web-apps-deploy GitHub Action.
With this knowledge, all we have to do is include the static_web_app_url variable to the output variables of the build job. This way, we can access the variable within the test job to configure Playwright.
To run the Playwright tests against the preview environment, assign the output variable to the PLAYWRIGHT_TEST_BASE_URL environment variable within the test job.