diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..41fc2cb7 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,54 @@ +name: CI + +on: + pull_request: + branches: [master] + push: + branches: [master] + +permissions: + contents: read + +jobs: + validate: + name: Validate + runs-on: ubuntu-22.04 + timeout-minutes: 25 + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Setup .NET + uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + with: + dotnet-version: 6.0.x + + - name: Setup Node.js + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 + with: + node-version: 22.x + cache: npm + cache-dependency-path: src/Payments.Mvc/ClientApp/package-lock.json + + - name: Restore frontend dependencies + working-directory: src/Payments.Mvc/ClientApp + run: npm ci --legacy-peer-deps + + - name: Build frontend + working-directory: src/Payments.Mvc/ClientApp + run: npm run build + + - name: Test frontend + working-directory: src/Payments.Mvc/ClientApp + run: npm run test:run + + - name: Restore .NET projects + run: dotnet restore Payments.sln + + - name: Build .NET projects + run: dotnet build Payments.sln --configuration Release --no-restore + + - name: Test .NET projects + run: dotnet test Payments.sln --configuration Release --no-build diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..1db3c7db --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,192 @@ +name: Deploy + +on: + workflow_dispatch: + inputs: + environment: + description: GitHub Environment to deploy. + required: true + type: environment + default: test + +permissions: + contents: read + id-token: write + +jobs: + deploy: + name: Deploy ${{ inputs.environment }} + # Only test is enabled during this migration. Production stays in Azure Pipelines. + if: github.ref == 'refs/heads/master' && inputs.environment == 'test' + runs-on: ubuntu-22.04 + timeout-minutes: 30 + environment: ${{ inputs.environment }} + concurrency: + group: azure-${{ inputs.environment }} + cancel-in-progress: false + env: + APP_HEALTH_TIMEOUT_SECONDS: ${{ vars.APP_HEALTH_TIMEOUT_SECONDS || '60' }} + AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }} + AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }} + AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }} + RESOURCE_GROUP: ${{ vars.RESOURCE_GROUP }} + WEB_APP_NAME: ${{ vars.WEB_APP_NAME }} + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Validate deployment configuration + shell: bash + run: | + set -euo pipefail + for name in AZURE_CLIENT_ID AZURE_TENANT_ID AZURE_SUBSCRIPTION_ID RESOURCE_GROUP WEB_APP_NAME; do + if [[ -z "${!name}" ]]; then + echo "Missing required GitHub Environment variable: $name" + exit 1 + fi + done + if [[ ! "$APP_HEALTH_TIMEOUT_SECONDS" =~ ^[1-9][0-9]*$ ]]; then + echo "APP_HEALTH_TIMEOUT_SECONDS must be a positive integer." + exit 1 + fi + + - name: Setup .NET + uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + with: + dotnet-version: 6.0.x + + - name: Setup Node.js + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 + with: + node-version: 22.x + cache: npm + cache-dependency-path: src/Payments.Mvc/ClientApp/package-lock.json + + - name: Restore frontend dependencies + working-directory: src/Payments.Mvc/ClientApp + run: npm ci --legacy-peer-deps + + - name: Build frontend + working-directory: src/Payments.Mvc/ClientApp + run: npm run build + + - name: Test frontend + working-directory: src/Payments.Mvc/ClientApp + run: npm run test:run + + - name: Restore .NET projects + run: dotnet restore Payments.sln + + - name: Build .NET projects + run: dotnet build Payments.sln --configuration Release --no-restore + + - name: Test .NET projects + run: dotnet test Payments.sln --configuration Release --no-build + + - name: Publish app + run: dotnet publish src/Payments.Mvc/Payments.Mvc.csproj --configuration Release -p:FrontendDependenciesRestored=true --output "$PWD/publish/web" + + - name: Publish WebJobs + run: | + dotnet publish src/Payments.Jobs.MoneyMovement/Payments.Jobs.MoneyMovement.csproj --configuration Release --output "$PWD/publish/web/app_data/jobs/triggered/Payments.Jobs.MoneyMovement" + dotnet publish src/Payments.Jobs.AutoApprove/Payments.Jobs.AutoApprove.csproj --configuration Release --output "$PWD/publish/web/app_data/jobs/triggered/Payments.Jobs.AutoApprove" + + - name: Package app + shell: bash + run: | + set -euo pipefail + mkdir -p publish + cd publish/web + zip -qr ../webapp.zip . + + - name: Azure login + uses: azure/login@7ddb5af1ef8758cf1353cf3b42f940aee27ba21c # v3.0.2 + with: + client-id: ${{ env.AZURE_CLIENT_ID }} + tenant-id: ${{ env.AZURE_TENANT_ID }} + subscription-id: ${{ env.AZURE_SUBSCRIPTION_ID }} + + - name: Verify existing App Service + run: az webapp show --resource-group "$RESOURCE_GROUP" --name "$WEB_APP_NAME" --output none + + - name: Check SCM readiness + shell: bash + run: | + set -euo pipefail + + scm_url="https://${WEB_APP_NAME}.scm.azurewebsites.net/api/settings" + ready_count=0 + + for attempt in {1..12}; do + scm_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' --max-time 10 "$scm_url" || true)" + + if [[ "$scm_status" == "200" || "$scm_status" == "401" ]]; then + ((ready_count += 1)) + if (( ready_count == 2 )); then + echo "SCM is ready for package deployment." + exit 0 + fi + else + ready_count=0 + fi + + if (( attempt < 12 )); then + sleep 5 + fi + done + + echo "SCM is not ready for package deployment." + exit 1 + + - name: Deploy app package + uses: azure/webapps-deploy@02a81bead70021f5284939794bcec79c271ab383 # v3 + with: + app-name: ${{ env.WEB_APP_NAME }} + package: publish/webapp.zip + + - name: Verify application health + shell: bash + run: | + set -euo pipefail + + app_url="https://${WEB_APP_NAME}.azurewebsites.net/health" + start_time=$SECONDS + deadline=$((start_time + APP_HEALTH_TIMEOUT_SECONDS)) + ready_count=0 + attempt=0 + + while (( SECONDS < deadline )); do + ((attempt += 1)) + remaining=$((deadline - SECONDS)) + if (( remaining <= 0 )); then + break + fi + + request_timeout=$((remaining < 10 ? remaining : 10)) + app_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' --max-time "$request_timeout" "$app_url" || true)" + elapsed=$((SECONDS - start_time)) + + if [[ "$app_status" == "200" ]]; then + ((ready_count += 1)) + if (( ready_count == 2 )); then + echo "The deployed application is healthy after ${elapsed} seconds." + exit 0 + fi + else + ready_count=0 + echo "Health check attempt $attempt returned HTTP ${app_status:-000} after ${elapsed} seconds." + fi + + remaining=$((deadline - SECONDS)) + if (( remaining <= 0 )); then + break + fi + + sleep_seconds=$((remaining < 5 ? remaining : 5)) + sleep "$sleep_seconds" + done + + echo "The package was deployed, but the application did not become healthy within ${APP_HEALTH_TIMEOUT_SECONDS} seconds." + exit 1 diff --git a/README.md b/README.md index c7932a05..b018190a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ -# Payments -[![Node Version](https://img.shields.io/badge/dynamic/json?label=Node%20Version&query=%24.engines.node&url=https%3A%2F%2Fraw.githubusercontent.com%2Fucdavis%2Fpayments%2Fmaster%2Fsrc%2FPayments.Mvc%2FClientApp%2Fpackage.json)](https://img.shields.io/badge/dynamic/json?label=Node%20Version&query=%24.engines.node&url=https%3A%2F%2Fraw.githubusercontent.com%2Fucdavis%2Fpayments%2Fmaster%2Fsrc%2FPayments.Mvc%2FClientApp%2Fpackage.json) -[![Build Status](https://dev.azure.com/ucdavis/payments/_apis/build/status%2Fucdavis.payments?branchName=master)](https://dev.azure.com/ucdavis/payments/_build/latest?definitionId=7&branchName=master) -[![CodeQL](https://github.com/ucdavis/payments/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/ucdavis/payments/actions/workflows/codeql-analysis.yml) - -[![forthebadge](https://forthebadge.com/images/badges/uses-html.svg)](https://forthebadge.com) - -Accept and process credit card payments for CA&ES +# Payments + +[![Node Version](https://img.shields.io/badge/dynamic/json?label=Node%20Version&query=%24.engines.node&url=https%3A%2F%2Fraw.githubusercontent.com%2Fucdavis%2Fpayments%2Fmaster%2Fsrc%2FPayments.Mvc%2FClientApp%2Fpackage.json)](https://img.shields.io/badge/dynamic/json?label=Node%20Version&query=%24.engines.node&url=https%3A%2F%2Fraw.githubusercontent.com%2Fucdavis%2Fpayments%2Fmaster%2Fsrc%2FPayments.Mvc%2FClientApp%2Fpackage.json) +[![Build Status](https://dev.azure.com/ucdavis/payments/_apis/build/status%2Fucdavis.payments?branchName=master)](https://dev.azure.com/ucdavis/payments/_build/latest?definitionId=7&branchName=master) +[![CodeQL](https://github.com/ucdavis/payments/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/ucdavis/payments/actions/workflows/codeql-analysis.yml) + +[![forthebadge](https://forthebadge.com/images/badges/uses-html.svg)](https://forthebadge.com) + +Accept and process credit card payments for CA&ES + +## Deployment + +See [the test deployment runbook](docs/test-deployment.md) for the GitHub Actions pilot, handover checks, and rollback. Production continues through Azure Pipelines. diff --git a/docs/test-deployment.md b/docs/test-deployment.md new file mode 100644 index 00000000..2fb27eee --- /dev/null +++ b/docs/test-deployment.md @@ -0,0 +1,92 @@ +# GitHub Actions deployment + +The manual `Deploy` workflow follows the [web-app-template deployment workflow](https://github.com/ucdavis/web-app-template/blob/main/.github/workflows/deploy-azure-appservice.yml). +Choose a GitHub environment when running it from `master`. Only `test` is enabled +for this migration. Production continues through the existing Azure pipeline. + +## Build and publish + +CI builds the frontend and .NET projects and runs their tests using .NET 6 and +Node 22. Deployment repeats those steps, then publishes directly to `publish/web`. +Payments' existing publish target includes the frontend in the web output. + +The deploy workflow also publishes both WebJobs into the web directory before +zipping it to `publish/webapp.zip`: + +- `app_data/jobs/triggered/Payments.Jobs.MoneyMovement`, hourly. +- `app_data/jobs/triggered/Payments.Jobs.AutoApprove`, daily at 06:00 UTC. + +Their existing `run.cmd`, `settings.job`, dependencies and settings are preserved. +The same job logs into Azure with OIDC, checks SCM readiness, deploys the ZIP using +`azure/webapps-deploy`, and checks `/health` for two consecutive HTTP 200 responses. +The health endpoint performs a read-only database connection check and returns +`Healthy` or `Unhealthy`, without connection details. + +## GitHub environment + +The `test` environment and deployment identity already exist. The environment +restricts deployment to `master` and supplies these variables: + +| Variable | Purpose | +| ---------------------------- | --------------------------------------------- | +| `AZURE_CLIENT_ID` | Deployment managed identity client ID | +| `AZURE_TENANT_ID` | Azure tenant ID | +| `AZURE_SUBSCRIPTION_ID` | Azure subscription ID | +| `RESOURCE_GROUP` | Existing App Service resource group | +| `WEB_APP_NAME` | Existing App Service name | +| `APP_HEALTH_TIMEOUT_SECONDS` | Optional health-check timeout, defaults to 60 | + +The test identity is `id-payments-test-deploy`, with Website Contributor scoped to +`payments-test` only. Its federated subject is +`repo:ucdavis/payments:environment:test`. No production identity or permissions are +part of this change. + +Application secrets and connection strings remain in Azure. Preserve Windows +hosting, 32-bit workers, Always On, schedules, domains, certificates, and +`ASPNETCORE_ENVIRONMENT=Production`. Do not change database migration behavior or +enable run-from-package during this migration. + +The earlier draft's `TEST_DEPLOYMENT_READY` variable is no longer used. Deployment +requires an explicit manual run; do not dispatch it until the handover below. + +## Test handover + +1. Review and merge the PR after CI passes. Coordinate other application changes so + the first deployment does not also introduce schema or financial logic changes. +2. Recheck the live test release and retain its artifact for rollback. The initial + baseline was Release-218 / build 15073 / commit + `e09c70b5d33f15f1cc3840757b3c263c2cb5ba6f`. Inspect both WebJobs and their histories. +3. In Azure classic release definition 1, change only Test's automatic trigger to + manual. Preserve the build pipeline, release artifact trigger, and Production + stage. Confirm no test release is running or queued, including older releases. +4. Check neither scheduled job is running. Between hourly runs and outside the + daily AutoApprove window, run `Deploy` on `master` with environment `test`. +5. Confirm deployment and health checks pass. Inspect deployed files and both + WebJobs, especially their schedules and Windows entry points. The old MSDeploy + task removed extra files and renamed locked files; inspect the first ZIP deploy + for obsolete files or locking issues without deleting runtime data. + +## Read-only acceptance + +For this first pass, inspect login, existing invoice pages, frontend assets, and +safe authenticated reads where access is available. Observe the next scheduled +MoneyMovement and AutoApprove runs and inspect application errors and job results. +Do not manually run jobs or initiate payments, recharge, email, or webhooks. + +A green build or deployment does not prove authenticated workflows or scheduled +job success. Record any checks that still need credentials or the next scheduled +run. The AutoApprove executable does not check `Finance:RechargeAutoApprove`, so +that setting is not a job-disable switch. + +Automatic GitHub deployment and production migration need a separate decision. + +## Recovery + +Stop further manual GitHub runs and let any active deployment finish. Redeploy +the retained known-good artifact through the existing Azure release's Test stage +only. Check login, deployed files, and both jobs. The older artifact does not have +`/health`, so use its existing application routes for verification. + +If abandoning the handover, restore Test's previous automatic trigger after +confirming no GitHub deployment is running or queued. Package rollback does not +undo database changes or job side effects. diff --git a/src/Payments.Mvc/Controllers/HealthController.cs b/src/Payments.Mvc/Controllers/HealthController.cs new file mode 100644 index 00000000..56a833c6 --- /dev/null +++ b/src/Payments.Mvc/Controllers/HealthController.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Payments.Core.Data; + +namespace Payments.Mvc.Controllers +{ + [AllowAnonymous] + [ApiExplorerSettings(IgnoreApi = true)] + public class HealthController : ControllerBase + { + private readonly ApplicationDbContext _context; + + public HealthController(ApplicationDbContext context) => _context = context; + + [HttpGet("/health")] + [ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)] + public async Task Get(CancellationToken cancellationToken) + { + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + timeout.CancelAfter(TimeSpan.FromSeconds(5)); + var healthy = false; + try + { + healthy = await _context.Database.CanConnectAsync(timeout.Token); + } + catch (OperationCanceledException) + { + // A timed-out dependency is unavailable; never expose connection details. + } + + return new ContentResult + { + StatusCode = healthy ? StatusCodes.Status200OK : StatusCodes.Status503ServiceUnavailable, + ContentType = "text/plain", + Content = healthy ? "Healthy" : "Unhealthy", + }; + } + } +} diff --git a/src/Payments.Mvc/Payments.Mvc.csproj b/src/Payments.Mvc/Payments.Mvc.csproj index da866740..96ee990b 100644 --- a/src/Payments.Mvc/Payments.Mvc.csproj +++ b/src/Payments.Mvc/Payments.Mvc.csproj @@ -93,7 +93,7 @@ - + diff --git a/tests/Payments.Tests/ControllerTests/HealthControllerTests.cs b/tests/Payments.Tests/ControllerTests/HealthControllerTests.cs new file mode 100644 index 00000000..aa65853a --- /dev/null +++ b/tests/Payments.Tests/ControllerTests/HealthControllerTests.cs @@ -0,0 +1,49 @@ +using System; +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.TestHost; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Payments.Core.Data; +using Payments.Mvc.Controllers; +using Xunit; + +namespace payments.Tests.ControllerTests +{ + public class HealthControllerTests + { + [Theory] + [InlineData("Data Source=:memory:", HttpStatusCode.OK, "Healthy")] + [InlineData("Data Source=/missing-payments-health-test/database.db;Mode=ReadOnly", HttpStatusCode.ServiceUnavailable, "Unhealthy")] + public async Task AnonymousRequestReportsDatabaseAvailabilityWithoutDetails( + string connectionString, HttpStatusCode expectedStatus, string expectedBody) + { + using var server = new TestServer(new WebHostBuilder() + .ConfigureServices(services => + { + services.AddDbContext(options => options.UseSqlite(connectionString)); + services.AddControllers().AddApplicationPart(typeof(HealthController).Assembly); + services.AddAuthentication("test").AddCookie("test"); + services.AddAuthorization(options => options.FallbackPolicy = new AuthorizationPolicyBuilder() + .RequireAuthenticatedUser().Build()); + }) + .Configure(app => + { + app.UseStatusCodePagesWithReExecute("/Error/{0}"); + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => endpoints.MapControllers()); + })); + using var client = server.CreateClient(); + using var response = await client.GetAsync("/health"); + Assert.Equal(expectedStatus, response.StatusCode); + Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); + Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType); + Assert.True(response.Headers.CacheControl.NoStore); + } + } +} diff --git a/tests/Payments.Tests/payments.Tests.csproj b/tests/Payments.Tests/payments.Tests.csproj index 9f8ac95b..2313f582 100644 --- a/tests/Payments.Tests/payments.Tests.csproj +++ b/tests/Payments.Tests/payments.Tests.csproj @@ -6,6 +6,7 @@ +