Get access to the deployed Azure Static Webb App URL within your GitHub Workflow
Tim Deschryver
timdeschryver.dev
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:
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.
npminitplaywright
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.
.github/workflows/playwright.yml
name:Playwright Tests
on:
push:
branches:[main,master]
pull_request:
branches:[main,master]
jobs:
test:
timeout-minutes:60
runs-on:ubuntu-latest
steps:
-uses:actions/checkout@v2
-uses:actions/setup-node@v2
with:
node-version:'14.x'
-name:Install dependencies
run:npm ci
-name:Install Playwright Browsers
run:npx playwright install --with-deps
-name:Run Playwright tests
run:npx playwright test
-uses:actions/upload-artifact@v2
if:always()
with:
name:playwright-report
path:playwright-report/
retention-days:30
The workflow installs Playwright and executes the test suite when a push is made against the main branch, or when a Pull Request is opened.
To run these tests within my Azure workflow, I simply copy-pasted its content into the SWA workflow file, which gives the following result:
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.