Containerise API application #74
Workflow file for this run
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: "Instance - deploy test" | |
on: | |
pull_request: | |
branches: [main] | |
jobs: | |
build-api: | |
uses: benchiverton/OnlineStore/.github/workflows/workflow-build-test-publish-dotnet.yml@containerise-apps | |
with: | |
solution_file: "src/Api/Company.Api.sln" | |
project_file: "src/Api/Company.Api/Company.Api.csproj" | |
dotnet_version: 8.0.203 | |
image_name: "api" | |
build-website: | |
uses: benchiverton/OnlineStore/.github/workflows/workflow-build-test-publish-dotnet.yml@main | |
with: | |
solution_file: "src/Website/Company.Website.sln" | |
project_file: "src/Website/Company.Website/Company.Website.csproj" | |
dotnet_version: 8.0.203 | |
artifact_name: "website" | |
self_contained: true | |
deploy-terraform-test: | |
needs: [build-api, build-website] | |
environment: test | |
env: | |
ARM_CLIENT_ID: ${{ secrets.TF_VAR_AGENT_CLIENT_ID }} | |
ARM_CLIENT_SECRET: ${{ secrets.TF_VAR_AGENT_CLIENT_SECRET }} | |
ARM_SUBSCRIPTION_ID: ${{ secrets.TF_VAR_SUBSCRIPTION_ID }} | |
ARM_TENANT_ID: ${{ secrets.TF_VAR_TENANT_ID }} | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: "./terraform" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v2 | |
with: | |
terraform_wrapper: false | |
- name: Terraform Format | |
id: fmt | |
run: terraform -chdir=instance fmt | |
- name: Terraform Init | |
id: init | |
run: terraform -chdir=instance init -backend-config="container_name=onlinestoretfstate-test" -backend-config="key=terraform.tfstate.${{ github.head_ref }}" | |
- name: Terraform Validate | |
id: validate | |
run: terraform -chdir=instance validate -no-color | |
- name: Terraform Plan | |
id: plan | |
run: terraform -chdir=instance plan -no-color | |
env: | |
TF_VAR_location: "East US" | |
TF_VAR_environment: ${{ github.head_ref }} | |
TF_VAR_acr_username: ${{ secrets.ACR_USERNAME }} | |
TF_VAR_acr_password: ${{ secrets.ACR_TOKEN }} | |
TF_VAR_monitoring_enabled: true # enabled for testing | |
- name: Terraform Apply | |
id: apply | |
run: terraform -chdir=instance apply -auto-approve | |
env: | |
TF_VAR_location: "East US" | |
TF_VAR_environment: ${{ github.head_ref }} | |
TF_VAR_acr_username: ${{ secrets.ACR_USERNAME }} | |
TF_VAR_acr_password: ${{ secrets.ACR_TOKEN }} | |
TF_VAR_monitoring_enabled: true # enabled for testing | |
- name: Save terraform outputs | |
shell: bash | |
run: | | |
mkdir terraform-outputs | |
terraform -chdir=instance output -raw resource_group_name > terraform-outputs/resource_group_name.txt | |
terraform -chdir=instance output -raw web_app_api_name > terraform-outputs/web_app_api_name.txt | |
terraform -chdir=instance output -raw web_app_api_hostname > terraform-outputs/web_app_api_hostname.txt | |
terraform -chdir=instance output -raw web_app_website_name > terraform-outputs/web_app_website_name.txt | |
terraform -chdir=instance output -raw web_app_website_hostname > terraform-outputs/web_app_website_hostname.txt | |
terraform -chdir=instance output -raw container_instance_monitoring_fqdn > terraform-outputs/container_instance_monitoring_fqdn.txt | |
# -raw cannot handle null values - https://github.com/hashicorp/terraform/issues/32384 | |
terraform -chdir=instance show -json | jq -r '.values.outputs.container_instance_monitoring_fqdn.value // ""' > container_instance_monitoring_fqdn.txt | |
- name: Upload terraform outputs for deploy job | |
uses: actions/upload-artifact@v3 | |
with: | |
name: terraform-outputs | |
path: terraform/terraform-outputs/ | |
deploy-app-test: | |
needs: [deploy-terraform-test] | |
environment: test | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Download terraform outputs | |
uses: actions/download-artifact@v3 | |
with: | |
name: terraform-outputs | |
- name: Set terraform outputs as env variables | |
id: vars | |
shell: bash | |
run: | | |
echo "resource_group_name=$(cat resource_group_name.txt)" >> $GITHUB_ENV | |
echo "web_app_api_name=$(cat web_app_api_name.txt)" >> $GITHUB_ENV | |
echo "web_app_api_hostname=$(cat web_app_api_hostname.txt)" >> $GITHUB_ENV | |
echo "web_app_website_name=$(cat web_app_website_name.txt)" >> $GITHUB_ENV | |
echo "web_app_website_hostname=$(cat web_app_website_hostname.txt)" >> $GITHUB_ENV | |
echo "container_app_api_fqdn=$(cat container_app_api_fqdn.txt)" >> $GITHUB_ENV | |
echo "container_instance_monitoring_fqdn=$(cat container_instance_monitoring_fqdn.txt)" >> $GITHUB_ENV | |
- name: Download website package | |
uses: actions/download-artifact@v3 | |
with: | |
name: website | |
path: ${{ github.workspace }}/website | |
- name: Update website config | |
run: echo "`jq '.Api.BasePath="https://${{ env.container_app_api_fqdn }}"' ${{ github.workspace }}/website/wwwroot/appsettings.json`" > ${{ github.workspace }}/website/wwwroot/appsettings.json | |
- name: Login via Azure CLI | |
uses: azure/login@v1 | |
with: | |
creds: ${{ secrets.AZURE_CREDENTIALS }} | |
- name: 'Deploy to Azure Container Instances' | |
uses: 'azure/aci-deploy@v1' | |
with: | |
resource-group: ${{ env.resource_group_name }} | |
dns-name-label: ${{ env.resource_group_name }}${{ github.run_number }} | |
image: onlinestorecontainerregistry.azurecr.io/api:${{ github.sha }} | |
registry-login-server: ${{ secrets.REGISTRY_LOGIN_SERVER }} | |
registry-username: ${{ secrets.ACR_USERNAME }} | |
registry-password: ${{ secrets.ACR_TOKEN }} | |
name: api | |
location: 'East US' | |
- name: Run Azure webapp deploy for website | |
uses: azure/webapps-deploy@v2 | |
id: websitedeploy | |
with: | |
app-name: ${{ env.web_app_website_name }} | |
package: ${{ github.workspace }}/website | |
- name: Post test environment information on PR | |
uses: actions/[email protected] | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const output = `### Test environment information | |
#### 🔗[Visit website](https://${{ env.web_app_website_hostname }}/) | |
#### 🔗[Visit API](https://${{ env.web_app_api_hostname }}/swagger/) | |
#### 🔗[Visit monitoring UI](http://${{ env.container_instance_monitoring_fqdn }}:16686) | |
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`; | |
github.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) |