diff --git a/.github/workflows/deploy.reusable.yml b/.github/workflows/deploy.reusable.yml index 0170c62..9754c6d 100644 --- a/.github/workflows/deploy.reusable.yml +++ b/.github/workflows/deploy.reusable.yml @@ -21,16 +21,12 @@ concurrency: jobs: deploy: - # windows-latest, not ubuntu-latest: `pac auth create`'s SP profile - # (secret included) persists via Windows Credential Manager (DPAPI) here, - # which just works. On Linux runners it falls back to an unprotected - # plaintext cache (no keyring daemon available even with libsecret - # installed) that a later step's `pac code push` process fails to reload - # correctly -- surfaces as AADSTS7000215 "Invalid client secret provided" - # even though the same secret authenticates fine elsewhere in the same - # job (e.g. the import-solution action, which doesn't round-trip through - # this cache). Found live 2026-07-02; libsecret-1-0 alone didn't fix it - # (confirmed: still no working keyring service headlessly). + # windows-latest was chosen on 2026-07-02 because `pac auth create`'s + # service-principal profile could not be reloaded on Linux runners by a + # later `pac code push`. The app is now published with the npm-based + # Power Apps CLI and no `pac auth` profile (see scripts/deploy/deploy-app.ps1), + # so that reason is gone; the runner is kept because every other step is + # verified on it and a Linux runner has not been tried with the new CLI. runs-on: windows-latest environment: ${{ inputs.environment }} permissions: @@ -120,19 +116,13 @@ jobs: PP_ENVIRONMENT_URL: ${{ vars.PP_ENVIRONMENT_URL }} run: ./scripts/deploy/configure-reminder-flow.ps1 - # --- App (US2, FR-005): pac code push and pac data import (below) use - # the pac CLI directly, so authenticate immediately before them here - # rather than earlier — a profile created before the actions above ran - # is not reliably present afterward. - - name: Authenticate to Power Platform - shell: pwsh - env: - PP_CLIENT_ID: ${{ secrets.PP_CLIENT_ID }} - PP_CLIENT_SECRET: ${{ secrets.PP_CLIENT_SECRET }} - PP_TENANT_ID: ${{ secrets.PP_TENANT_ID }} - PP_ENVIRONMENT_URL: ${{ vars.PP_ENVIRONMENT_URL }} - run: ./scripts/deploy/auth.ps1 - + # --- App (US2, FR-005): published with the npm-based Power Apps CLI + # (`pa app push --non-interactive`), signed in as the service principal + # through environment variables -- Microsoft's supported path, per the + # resolution of microsoft/PowerAppsCodeApps#394. No `pac auth` profile + # is involved. One-time prerequisite, done by the app's owner: the app + # is shared with the service principal with edit access (see + # scripts/deploy/deploy-app.ps1). - name: Render power.config.json shell: pwsh env: @@ -142,6 +132,10 @@ jobs: - name: Deploy Code App shell: pwsh + env: + PP_CLIENT_ID: ${{ secrets.PP_CLIENT_ID }} + PP_CLIENT_SECRET: ${{ secrets.PP_CLIENT_SECRET }} + PP_TENANT_ID: ${{ secrets.PP_TENANT_ID }} run: ./scripts/deploy/deploy-app.ps1 # --- Data (US3, FR-016): off by default, opt-in only, no record-content logging --- diff --git a/.github/workflows/promote-prod.yml b/.github/workflows/promote-prod.yml index b401146..04c8b53 100644 --- a/.github/workflows/promote-prod.yml +++ b/.github/workflows/promote-prod.yml @@ -110,18 +110,14 @@ jobs: deploy-production: needs: sync-dev-and-export - # windows-latest, not ubuntu-latest: `pac auth create`'s SP profile - # (secret included) persists via Windows Credential Manager (DPAPI) here, - # which just works. On Linux runners it falls back to an unprotected - # plaintext cache (no keyring daemon available even with libsecret - # installed) that a later step's `pac code push` process fails to reload - # correctly -- surfaces as AADSTS7000215 "Invalid client secret provided" - # even though the same secret authenticates fine elsewhere in the same - # job (e.g. the import-solution action, which doesn't round-trip through - # this cache). Found live 2026-07-02; libsecret-1-0 alone didn't fix it - # (confirmed: still no working keyring service headlessly). The - # sync-dev-and-export job above stays on ubuntu-latest -- it only uses - # official actions with their own direct auth, never a persisted profile. + # windows-latest was chosen on 2026-07-02 because `pac auth create`'s + # service-principal profile could not be reloaded on Linux runners by a + # later `pac code push`. The app is now published with the npm-based + # Power Apps CLI and no `pac auth` profile (see scripts/deploy/deploy-app.ps1), + # so that reason is gone; the runner is kept because every other step is + # verified on it and a Linux runner has not been tried with the new CLI. + # The sync-dev-and-export job above stays on ubuntu-latest -- it only + # uses official actions with their own direct auth. runs-on: windows-latest environment: production permissions: @@ -197,19 +193,13 @@ jobs: PP_ENVIRONMENT_URL: ${{ vars.PP_ENVIRONMENT_URL }} run: ./scripts/deploy/configure-reminder-flow.ps1 - # --- App (US2, FR-005): pac code push and pac data import (below) use - # the pac CLI directly, so authenticate immediately before them here - # rather than earlier — a profile created before the actions above ran - # is not reliably present afterward. - - name: Authenticate to Power Platform - shell: pwsh - env: - PP_CLIENT_ID: ${{ secrets.PP_CLIENT_ID }} - PP_CLIENT_SECRET: ${{ secrets.PP_CLIENT_SECRET }} - PP_TENANT_ID: ${{ secrets.PP_TENANT_ID }} - PP_ENVIRONMENT_URL: ${{ vars.PP_ENVIRONMENT_URL }} - run: ./scripts/deploy/auth.ps1 - + # --- App (US2, FR-005): published with the npm-based Power Apps CLI + # (`pa app push --non-interactive`), signed in as the service principal + # through environment variables -- Microsoft's supported path, per the + # resolution of microsoft/PowerAppsCodeApps#394. No `pac auth` profile + # is involved. One-time prerequisite, done by the app's owner: the app + # is shared with the service principal with edit access (see + # scripts/deploy/deploy-app.ps1). - name: Render power.config.json shell: pwsh env: @@ -219,6 +209,10 @@ jobs: - name: Deploy Code App shell: pwsh + env: + PP_CLIENT_ID: ${{ secrets.PP_CLIENT_ID }} + PP_CLIENT_SECRET: ${{ secrets.PP_CLIENT_SECRET }} + PP_TENANT_ID: ${{ secrets.PP_TENANT_ID }} run: ./scripts/deploy/deploy-app.ps1 # --- Data (US3, FR-016): off by default, opt-in only, no record-content logging --- diff --git a/README.md b/README.md index d2af2fe..56bbf5b 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,15 @@ Manual promotion No code reaches production without a human sign-off. ``` +The app is published by the deployment service principal with the Power Apps CLI (`pa app push --non-interactive`), the path Microsoft documents for service principals. One-time setup per environment: the app's owner shares the app with that environment's service principal, with edit access. From a folder whose `power.config.json` points at that environment's app: + +```bash +npx pa auth login +npx pa app share --principal --access edit +``` + +Use the object ID shown under **Enterprise applications** in Microsoft Entra, not the one under App registrations. + ### Security posture - **[Gitleaks](https://github.com/gitleaks/gitleaks)** runs on every PR — any committed secret blocks the merge diff --git a/package-lock.json b/package-lock.json index 15dbd0e..c6fbc05 100644 --- a/package-lock.json +++ b/package-lock.json @@ -41,6 +41,7 @@ "devDependencies": { "@eslint/js": "^9.36.0", "@microsoft/power-apps": "^1.1.3", + "@microsoft/power-apps-cli": "1.2.0", "@microsoft/power-apps-vite": "^1.0.2", "@testing-library/jest-dom": "^6.9.1", "@testing-library/react": "^16.3.2", @@ -1552,25 +1553,17 @@ } }, "node_modules/@microsoft/power-apps-cli": { - "version": "0.11.0", - "resolved": "https://registry.npmjs.org/@microsoft/power-apps-cli/-/power-apps-cli-0.11.0.tgz", - "integrity": "sha512-REaBFiEkJSWKbPujOM/Qrjxw5XbBzUXWPRB5h9frItwqqsndoM6n08eKO16IOB69b0zxo2UOENmbPWQ3rMswMA==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@microsoft/power-apps-cli/-/power-apps-cli-1.2.0.tgz", + "integrity": "sha512-0cbkf5eL530G6bm1zcJazbnVsiFXga5jIiDba6PLVLQyhZyhZdsRK29LUiQBCsM3CrebgMkqghYvuIM0UxRFRQ==", "dev": true, "license": "See license in LICENSE file", "dependencies": { - "@azure/msal-node": "3.6.3", "@azure/msal-node-extensions": "1.5.17", - "@clack/prompts": "0.6.3", - "@microsoft/1ds-core-js": "4.3.11", - "@microsoft/1ds-post-js": "4.3.11", - "@microsoft/power-apps-actions": "1.5.0", - "@microsoft/power-apps-common": "1.1.1", - "chalk": "4.1.2", - "commander": "10.0.1", "open": "8.4.0" }, "bin": { - "power-apps": "dist/Bin.js" + "pa": "dist/Bin.js" }, "engines": { "node": ">=22" @@ -1616,6 +1609,31 @@ } } }, + "node_modules/@microsoft/power-apps/node_modules/@microsoft/power-apps-cli": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/@microsoft/power-apps-cli/-/power-apps-cli-0.11.0.tgz", + "integrity": "sha512-REaBFiEkJSWKbPujOM/Qrjxw5XbBzUXWPRB5h9frItwqqsndoM6n08eKO16IOB69b0zxo2UOENmbPWQ3rMswMA==", + "dev": true, + "license": "See license in LICENSE file", + "dependencies": { + "@azure/msal-node": "3.6.3", + "@azure/msal-node-extensions": "1.5.17", + "@clack/prompts": "0.6.3", + "@microsoft/1ds-core-js": "4.3.11", + "@microsoft/1ds-post-js": "4.3.11", + "@microsoft/power-apps-actions": "1.5.0", + "@microsoft/power-apps-common": "1.1.1", + "chalk": "4.1.2", + "commander": "10.0.1", + "open": "8.4.0" + }, + "bin": { + "power-apps": "dist/Bin.js" + }, + "engines": { + "node": ">=22" + } + }, "node_modules/@mswjs/interceptors": { "version": "0.41.9", "resolved": "https://registry.npmjs.org/@mswjs/interceptors/-/interceptors-0.41.9.tgz", @@ -1642,19 +1660,29 @@ "license": "MIT" }, "node_modules/@nevware21/ts-async": { - "version": "0.5.5", - "resolved": "https://registry.npmjs.org/@nevware21/ts-async/-/ts-async-0.5.5.tgz", - "integrity": "sha512-vwqaL05iJPjLeh5igPi8MeeAu10i+Aq7xko1fbo9F5Si6MnVN5505qaV7AhSdk5MCBJVT/UYMk3kgInNjDb4Ig==", + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/@nevware21/ts-async/-/ts-async-0.6.1.tgz", + "integrity": "sha512-W2kFiT5oPuxTrB3NrxUId/U+1AuAhIaiDQkLC4HcxkjNc+85GfELYdPQXnsDWDG8yji24F5qk6QpBDxZX3/0+g==", "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/nevware21" + }, + { + "type": "buymeacoffee", + "url": "https://www.buymeacoffee.com/nevware21" + } + ], "license": "MIT", "dependencies": { - "@nevware21/ts-utils": ">= 0.12.2 < 2.x" + "@nevware21/ts-utils": ">= 0.15.0 < 2.x" } }, "node_modules/@nevware21/ts-utils": { - "version": "0.14.0", - "resolved": "https://registry.npmjs.org/@nevware21/ts-utils/-/ts-utils-0.14.0.tgz", - "integrity": "sha512-WoeqTIXQ8WPhl+lD2NbMHoAQ4sJl0n7EoRoDmVJui//Usg512enl9q1fdbVobuZt3omnxnmVsDrNIvPBvFgddQ==", + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/@nevware21/ts-utils/-/ts-utils-0.16.0.tgz", + "integrity": "sha512-DDln15VUBSw5JjJlNno1UCSzgAGzHHklm7u695jXDCzwSn7W2F7B2VYHwLLcT1cOWQQBQAJK6e2/Y8oTG+W0Ig==", "dev": true, "funding": [ { @@ -3728,26 +3756,26 @@ } }, "node_modules/@ts-morph/common/node_modules/brace-expansion": { - "version": "5.0.6", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.6.tgz", - "integrity": "sha512-kLpxurY4Z4r9sgMsyG0Z9uzsBlgiU/EFKhj/h91/8yHu0edo7XuixOIH3VcJ8kkxs6/jPzoI6U9Vj3WqbMQ94g==", + "version": "5.0.12", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.12.tgz", + "integrity": "sha512-YovQ3rzhaLMIrDjNDMkNS01tea93qhEhG5xy8f6+R0l+dw3Ki+5sCoIoI942iuLZTHWogWktgwVDhU09iNEimQ==", "dev": true, "license": "MIT", "dependencies": { "balanced-match": "^4.0.2" }, "engines": { - "node": "18 || 20 || >=22" + "node": "20 || >=22" } }, "node_modules/@ts-morph/common/node_modules/minimatch": { - "version": "10.2.5", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.5.tgz", - "integrity": "sha512-MULkVLfKGYDFYejP07QOurDLLQpcjk7Fw+7jXS2R2czRQzR56yHRveU5NDJEOviH+hETZKSkIk5c+T23GjFUMg==", + "version": "10.2.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.6.tgz", + "integrity": "sha512-vpLQEs+VLCr1nU0BXS07maYoFwlDAH0gngQuuttxIwutDFEMHq2blX+8vpgxDdK3J1PwjCJiep77OitTZ4Ll1A==", "dev": true, "license": "BlueOak-1.0.0", "dependencies": { - "brace-expansion": "^5.0.5" + "brace-expansion": "^5.0.8" }, "engines": { "node": "18 || 20 || >=22" @@ -6207,9 +6235,9 @@ } }, "node_modules/jsonwebtoken/node_modules/semver": { - "version": "7.8.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.1.tgz", - "integrity": "sha512-rkVq3IXh+4FDGch+KwzX3aV9W3kO54GyEgpvBzSyctDA6Xtd7RJQV1xmXbeQp5v7+VzLOfVqiutSE6GICgPFvg==", + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", "dev": true, "license": "ISC", "bin": { diff --git a/package.json b/package.json index 9a0f9d8..beed9cd 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "devDependencies": { "@eslint/js": "^9.36.0", "@microsoft/power-apps": "^1.1.3", + "@microsoft/power-apps-cli": "1.2.0", "@microsoft/power-apps-vite": "^1.0.2", "@testing-library/jest-dom": "^6.9.1", "@testing-library/react": "^16.3.2", diff --git a/scripts/ci/render-power-config.ps1 b/scripts/ci/render-power-config.ps1 index 35cb7bb..6ab5bce 100644 --- a/scripts/ci/render-power-config.ps1 +++ b/scripts/ci/render-power-config.ps1 @@ -3,7 +3,7 @@ Renders power.config.json from power.config.template.json by substituting the __ENVIRONMENT_ID__ / __APP_ID__ placeholders with values from the PP_ENVIRONMENT_ID / PP_APP_ID environment variables. Run this immediately -before `pac code push` in a deploy job. +before the Code App is published in a deploy job (scripts/deploy/deploy-app.ps1). #> $ErrorActionPreference = 'Stop' diff --git a/scripts/deploy/auth.ps1 b/scripts/deploy/auth.ps1 deleted file mode 100644 index f51ec11..0000000 --- a/scripts/deploy/auth.ps1 +++ /dev/null @@ -1,54 +0,0 @@ -#Requires -Version 7.0 -<# -Non-interactive Power Platform authentication via a service principal. -Fails fast with a clear message if required secrets/variables are missing or -auth fails — never falls back to interactive login (FR-006). - -Required environment variables: - PP_CLIENT_ID Service principal application (client) ID - PP_CLIENT_SECRET Service principal client secret - PP_TENANT_ID Entra tenant ID - PP_ENVIRONMENT_URL Target Power Platform environment URL -#> - -$ErrorActionPreference = 'Stop' - -$required = @('PP_CLIENT_ID', 'PP_CLIENT_SECRET', 'PP_TENANT_ID', 'PP_ENVIRONMENT_URL') -$missing = $required | Where-Object { [string]::IsNullOrWhiteSpace((Get-Item -Path "env:$_" -ErrorAction SilentlyContinue).Value) } - -if ($missing.Count -gt 0) { - throw "Missing required Power Platform auth environment variable(s): $($missing -join ', '). Configure them as secrets on the target GitHub Environment." -} - -Write-Host "Authenticating to $($env:PP_ENVIRONMENT_URL) via service principal $($env:PP_CLIENT_ID)..." - -# --accept-cleartext-caching: GitHub-hosted runners have no OS keyring -# available, so pac auth create needs this to cache the token for reuse by -# later steps/processes in the same (ephemeral, single-use) runner. -pac auth create ` - --applicationId $env:PP_CLIENT_ID ` - --clientSecret $env:PP_CLIENT_SECRET ` - --tenant $env:PP_TENANT_ID ` - --environment $env:PP_ENVIRONMENT_URL ` - --accept-cleartext-caching - -if ($LASTEXITCODE -ne 0) { - throw "pac auth create failed (exit code $LASTEXITCODE). Verify the service principal is registered as an application user in the target environment and the secret has not expired." -} - -Write-Host "Authenticated successfully." - -# pac auth create's persisted profile does not retain a usable client secret -# by design (threat-model choice, not a bug) -- when a later `pac` process -# (e.g. `pac code push`) needs to refresh an access token via MSAL's -# ConfidentialClient, it has no refresh token and no secret, and fails with -# AADSTS7000215 "Invalid client secret provided". This reproduces identically -# regardless of OS/keyring (confirmed live 2026-07-02 on both ubuntu-latest -# and windows-latest runners). The documented fix, from a Microsoft -# powerplatform-vscode maintainer, is to expose the secret via a well-known -# env var that pac's internal refresh logic looks for: -# https://github.com/microsoft/powerplatform-vscode/issues/297#issuecomment-1663066345 -# https://github.com/microsoft/powerplatform-vscode/issues/456 -if ($env:GITHUB_ENV) { - "PAC_CLI_SPN_SECRET=$($env:PP_CLIENT_SECRET)" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8 -} diff --git a/scripts/deploy/deploy-app.ps1 b/scripts/deploy/deploy-app.ps1 index d1c4a61..29cb316 100644 --- a/scripts/deploy/deploy-app.ps1 +++ b/scripts/deploy/deploy-app.ps1 @@ -1,46 +1,84 @@ #Requires -Version 7.0 <# -Pushes the built Code App using the pac CLI. Assumes: - - `pac auth create` has already run (scripts/deploy/auth.ps1) - - power.config.json has already been rendered (scripts/ci/render-power-config.ps1) +Publishes the built Code App with the npm-based Power Apps CLI (`pa`), signed +in as the deployment service principal. Never prompts and never falls back to +an interactive sign-in (FR-006). + +This is Microsoft's supported path for a service principal: +https://learn.microsoft.com/power-apps/developer/code-apps/how-to/use-service-principal +It was given as the resolution of microsoft/PowerAppsCodeApps#394 and replaces +`pac code push --solutionName`, the workaround this pipeline used for the +HTTP 500 the PAC CLI returns under a service principal. Microsoft's guidance +is that the PAC CLI must not be used to publish a code app as a service +principal. + +One-time prerequisite per environment, done by the app's owner and never by +this pipeline: share the app with the service principal with edit access. +From a folder whose power.config.json points at that environment's app: + + npx pa auth login + npx pa app share --principal --access edit + +Environment-level roles alone do not let a service principal update an +existing app, and a service principal cannot grant itself access. + +Assumes: + - `npm ci` has installed the pinned @microsoft/power-apps-cli + - power.config.json has been rendered (scripts/ci/render-power-config.ps1) - dist/ contains the production build (npm run build:ci) ---log-to-console surfaces pac diagnostics directly in the (ephemeral) runner's -job log, per Microsoft's guidance for GitHub-hosted runners. +Required environment variables: + PP_CLIENT_ID Service principal application (client) ID + PP_CLIENT_SECRET Service principal client secret + PP_TENANT_ID Entra tenant ID #> $ErrorActionPreference = 'Stop' +$required = @('PP_CLIENT_ID', 'PP_CLIENT_SECRET', 'PP_TENANT_ID') +$missing = $required | Where-Object { [string]::IsNullOrWhiteSpace((Get-Item -Path "env:$_" -ErrorAction SilentlyContinue).Value) } +if ($missing.Count -gt 0) { + throw "Missing required Power Platform auth environment variable(s): $($missing -join ', '). Configure them as secrets on the target GitHub Environment." +} + $repoRoot = Resolve-Path (Join-Path $PSScriptRoot '../..') $configPath = Join-Path $repoRoot 'power.config.json' if (-not (Test-Path $configPath)) { throw "power.config.json not found at $configPath. Run scripts/ci/render-power-config.ps1 first." } +$environmentId = (Get-Content -Path $configPath -Raw | ConvertFrom-Json).environmentId +if ([string]::IsNullOrWhiteSpace($environmentId)) { + throw "power.config.json has no environmentId." +} + +# These variables make the CLI sign in as the service principal instead of +# using a cached interactive account. They live only in this process. +$env:PA_CLI_USE_SP_AUTH = 'true' +$env:PA_CLI_SP_CLIENT_ID = $env:PP_CLIENT_ID +$env:PA_CLI_SP_CLIENT_SECRET = $env:PP_CLIENT_SECRET +$env:PA_CLI_SP_TENANT_ID = $env:PP_TENANT_ID + +Write-Host "Publishing Code App using $configPath as service principal $($env:PP_CLIENT_ID)..." + +Push-Location $repoRoot +try { + # --no: use the version npm ci installed; never download another one. + # The earlier CLI could print an HTTP error and still exit 0 (found live + # 2026-07-02), so the output is checked as well as the exit code. + npx --no -- pa app push --non-interactive --no-color --environment-id $environmentId 2>&1 | + Tee-Object -Variable capturedOutput | ForEach-Object { Write-Host $_ } + $exitCode = $LASTEXITCODE +} finally { + Pop-Location +} -Write-Host "Pushing Code App using $configPath..." - -# --solutionName: `pac code push` under a service-principal identity returns a -# generic HTTP 500 when the Code App isn't associated with an explicit -# solution (confirmed live 2026-07-02, filed as -# https://github.com/microsoft/PowerAppsCodeApps/issues/394). A maintainer -# workaround (see issue #394 comments) is to bundle/associate the app with a -# real solution and pass it explicitly -- reusing the same schema solution -# (MedTrackSolution) already used for tables/etc. in this repo. -$solutionName = 'MedTrackSolution' - -# `pac code push` can print an HTTP error (e.g. the app's appId belonging to a -# different environment) and still exit 0 -- found live 2026-07-02 when a -# reused appId hit "InvalidEnvironmentName" but the step still reported -# success. Capture output and fail on either a non-zero exit code or a -# surfaced HTTP error, rather than trusting the exit code alone. -pac --log-to-console code push --solutionName $solutionName 2>&1 | Tee-Object -Variable capturedOutput | ForEach-Object { Write-Host $_ } - -if ($LASTEXITCODE -ne 0) { - throw "pac code push failed (exit code $LASTEXITCODE)." +$sharingHint = "If the service principal has never published this app, the app's owner must first share it: pa app share --principal --access edit (see this script's header)." +if ($exitCode -ne 0) { + throw "pa app push failed (exit code $exitCode). $sharingHint" } if ($capturedOutput -match 'HTTP error status') { - throw "pac code push reported an HTTP error but exited 0 -- treating as a failure. See the output above for details." + throw "pa app push reported an HTTP error but exited 0 -- treating as a failure. See the output above for details. $sharingHint" } -Write-Host "Code App pushed successfully." +Write-Host "Code App published successfully." diff --git a/scripts/deploy/migrate-data.ps1 b/scripts/deploy/migrate-data.ps1 index 8b8e75f..329b4c3 100644 --- a/scripts/deploy/migrate-data.ps1 +++ b/scripts/deploy/migrate-data.ps1 @@ -11,8 +11,8 @@ against v2.7.4 — the only related tool is `pac tool CMT`, a Windows GUI executable that cannot run headlessly on a GitHub-hosted Linux runner). This script replaces that non-existent step with a small, self-contained Web API upsert. It authenticates independently via OAuth client-credentials -rather than depending on `pac auth create`'s CLI profile, since that profile -is not reliably present across steps (see auth.ps1's ordering note). +rather than depending on a `pac auth create` CLI profile, which was found not +to be reliably present across steps. Medications upsert by the `ppa_name` alternate key. Intake logs upsert by the composite `ppa_scheduledfor` + medication-lookup alternate key; because diff --git a/specs/003-github-actions-cicd/contracts/deploy.contract.md b/specs/003-github-actions-cicd/contracts/deploy.contract.md index 14a8e20..2c8d645 100644 --- a/specs/003-github-actions-cicd/contracts/deploy.contract.md +++ b/specs/003-github-actions-cicd/contracts/deploy.contract.md @@ -18,11 +18,11 @@ Deploys the Code App + Dataverse schema (+ opt-in data) to a target Power Platfo |---|---| | Checkout | source at release commit. | | Install `pac` | via `microsoft/powerplatform-actions/actions/install` or dotnet tool. | -| Authenticate | `pac auth create` with SP secrets → target `PP_ENVIRONMENT_URL`. Fail fast with clear message if secrets missing/invalid; never prompt (FR-006, edge case "Secrets missing"). | +| Authenticate | The solution actions take the SP secrets as inputs; the app step signs the Power Apps CLI in as the SP through `PA_CLI_*` environment variables (set by `deploy-app.ps1` from `PP_CLIENT_ID`/`PP_CLIENT_SECRET`/`PP_TENANT_ID`). No `pac auth` profile. Fail fast with clear message if secrets missing/invalid; never prompt (FR-006, edge case "Secrets missing"). | | Build | `npm ci` + `npm run build:ci` (or download CI artifact). | | Render config | `scripts/ci/render-power-config.*` → `power.config.json` from template + `PP_ENVIRONMENT_ID`/`PP_APP_ID` (FR-020). | | **Schema** | `pac solution pack` (`solution/src`) → `import-solution` (SP auth) → `publish-solution`. Runs **before** app push (FR-007). | -| **App** | `pac code push` (`scripts/deploy/deploy-app.*`, `--log-to-console`) (FR-005). | +| **App** | `pa app push --non-interactive` from the pinned `@microsoft/power-apps-cli` (`scripts/deploy/deploy-app.ps1`) (FR-005). One-time prerequisite per environment: the app's owner shares the app with the SP with edit access (`pa app share --principal --access edit`). Replaces `pac code push --solutionName` (changed 2026-10-05, microsoft/PowerAppsCodeApps#394). | | **Data (conditional)** | `if: inputs.migrate_data` → `scripts/deploy/migrate-data.ps1`, a Web API upsert against `data/ppa_medication.json`/`data/ppa_intakelog.json` keyed by alternate key (idempotent upsert). Skipped entirely when false (FR-016). `pac data import`/`export` do not exist in the PAC CLI; the only related tool, `pac tool CMT`, is a Windows GUI executable and cannot run on a GitHub-hosted Linux runner — found live 2026-07-01 during T039. | | Record | Emit deployment summary (commit, environment, actor, outcome, data flag) (FR-009). | diff --git a/specs/003-github-actions-cicd/tasks.md b/specs/003-github-actions-cicd/tasks.md index 8e58df8..e5158d9 100644 --- a/specs/003-github-actions-cicd/tasks.md +++ b/specs/003-github-actions-cicd/tasks.md @@ -303,3 +303,26 @@ Task: "Add alternate key to ppa_intakelog in solution/src/" ### Sequencing Note Unlike a typical feature where same-priority stories (US2, US4) are fully parallel, here US4 is deliberately sequenced **before** US2: US2's deploy workflow reads Power Platform identifiers via the externalized-config mechanism (`power.config.template.json` + `render-power-config.ps1`) that US4 introduces, so building US2 first would mean writing it against a config pattern that gets replaced immediately after. + +--- + +## Follow-up (2026-10-05): the app is published with the Power Apps CLI, as Microsoft now documents + +microsoft/PowerAppsCodeApps#394 was closed on 2026-08-19 with official guidance +([Publish Power Apps code apps with a service principal](https://learn.microsoft.com/power-apps/developer/code-apps/how-to/use-service-principal)): + +- a service principal needs **edit access to the app itself**; environment-level roles are not enough; +- the app's owner grants that once, with `pa app share --principal --access edit`; +- updates are then published with `pa app push --non-interactive`, signed in through the `PA_CLI_USE_SP_AUTH`, `PA_CLI_SP_CLIENT_ID`, `PA_CLI_SP_CLIENT_SECRET` and `PA_CLI_SP_TENANT_ID` environment variables; +- the npm-based CLI must be used for this, **not the PAC CLI**. + +What changed here: + +- `scripts/deploy/deploy-app.ps1` runs `pa app push --non-interactive` from `@microsoft/power-apps-cli`, pinned at 1.2.0 as a dev dependency so `npm ci` installs it with a locked integrity hash. It replaces `pac --log-to-console code push --solutionName MedTrackSolution`, the workaround from the section above. +- `deploy.reusable.yml` and `promote-prod.yml`: the "Authenticate to Power Platform" step (`pac auth create`) is removed, and the three service principal secrets are passed to "Deploy Code App" instead. +- `scripts/deploy/auth.ps1` is deleted: nothing else used the `pac auth` profile. The `PAC_CLI_SPN_SECRET` workaround it carried is no longer needed. +- The local `build` script (`pac code push` under the developer's own sign-in) is unchanged. + +**Prerequisite before the next deploy** (checked 2026-10-05: neither service principal has a permission on its environment's app yet, only the owner does): the owner shares the dev app with the dev service principal and the production app with the production one, each with edit access. + +**Not yet verified**: a pipeline run with the new step. The runner stays `windows-latest`; the reason it was chosen (the `pac auth` profile on Linux) no longer applies, but a Linux runner has not been tried with the new CLI.