-
Notifications
You must be signed in to change notification settings - Fork 1
Add secure Terraform release workflow #1997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gunzip
wants to merge
3
commits into
feats/expose-terraform-release-matrix
Choose a base branch
from
feats/secure-terraform-release-workflow
base: feats/expose-terraform-release-matrix
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
.github/workflows/release-terraform-environment-v1.yaml
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| name: Terraform Environment Release v1 | ||
| run-name: Terraform release ${{ inputs.project }} | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| project: | ||
| description: "Nx Terraform environment project to release" | ||
| required: true | ||
| type: string | ||
| project-root: | ||
| description: "Repository path containing the Terraform environment.json manifest" | ||
| required: true | ||
| type: string | ||
| source-ref: | ||
| description: "Git commit containing the released infrastructure configuration" | ||
| required: true | ||
| type: string | ||
|
|
||
| concurrency: | ||
| group: infra-terraform-${{ inputs.project }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| validate: | ||
| name: Validate Deployment Environment | ||
| runs-on: ubuntu-24.04 | ||
| permissions: | ||
| contents: read | ||
| outputs: | ||
| apply-environment: ${{ fromJSON(steps.release-metadata.outputs.result).applyEnvironment }} | ||
| plan-environment: ${{ fromJSON(steps.release-metadata.outputs.result).planEnvironment }} | ||
| runner-label: ${{ fromJSON(steps.release-metadata.outputs.result).runnerLabel }} | ||
| source-ref: ${{ fromJSON(steps.release-metadata.outputs.result).sourceRef }} | ||
| steps: | ||
| - name: Validate release source and deployment metadata | ||
| id: release-metadata | ||
| uses: pagopa/dx/actions/run-dx-task@main | ||
| env: | ||
| GITHUB_TOKEN: ${{ github.token }} | ||
| with: | ||
| task: validateTerraformEnvironmentRelease | ||
| payload: >- | ||
| { | ||
| "owner": ${{ toJSON(github.repository_owner) }}, | ||
| "project": ${{ toJSON(inputs.project) }}, | ||
| "projectRoot": ${{ toJSON(inputs.project-root) }}, | ||
| "repo": ${{ toJSON(github.event.repository.name) }}, | ||
| "sourceRef": ${{ toJSON(inputs.source-ref) }} | ||
| } | ||
|
|
||
| plan: | ||
| name: Terraform Plan | ||
| needs: validate | ||
| runs-on: ["self-hosted", "${{ needs.validate.outputs.runner-label }}"] | ||
| environment: ${{ needs.validate.outputs.plan-environment }} | ||
| permissions: | ||
| actions: read | ||
| contents: read | ||
| id-token: write | ||
| env: | ||
| ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }} | ||
| ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }} | ||
| ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }} | ||
| ARM_USE_OIDC: true | ||
| ARM_USE_AZUREAD: true | ||
| ARM_STORAGE_USE_AZUREAD: true | ||
| ROLE_ARN: ${{ secrets.ROLE_ARN }} | ||
| AWS_REGION: ${{ secrets.AWS_REGION }} | ||
| steps: | ||
| - name: Check out released configuration | ||
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| with: | ||
| fetch-depth: 0 | ||
| filter: tree:0 | ||
| ref: ${{ needs.validate.outputs.source-ref }} | ||
|
|
||
| - name: Cloud Login | ||
| uses: pagopa/dx/actions/csp-login@main | ||
|
|
||
| - name: Setup Node.js | ||
| id: node-setup | ||
| uses: pagopa/dx/.github/actions/node-setup@main | ||
|
|
||
| - name: Setup Terraform | ||
| uses: pagopa/dx/.github/actions/terraform-setup@main | ||
|
|
||
| - name: Install dependencies (pnpm) | ||
| if: steps.node-setup.outputs.package-manager == 'pnpm' | ||
| run: pnpm install --frozen-lockfile | ||
|
|
||
| - name: Install dependencies (yarn) | ||
| if: steps.node-setup.outputs.package-manager == 'yarn' | ||
| run: yarn install --immutable | ||
|
|
||
| - name: Install dependencies (npm) | ||
| if: steps.node-setup.outputs.package-manager == 'npm' | ||
| run: npm ci | ||
|
|
||
| - name: Run Terraform plan-upload | ||
| env: | ||
| NX_PROJECT: ${{ inputs.project }} | ||
| run: npx nx run "$NX_PROJECT:tf-plan-upload" -c ci | ||
|
|
||
| apply: | ||
| name: Terraform Apply | ||
| needs: [plan, validate] | ||
| runs-on: ["self-hosted", "${{ needs.validate.outputs.runner-label }}"] | ||
| environment: ${{ needs.validate.outputs.apply-environment }} | ||
| permissions: | ||
| actions: read | ||
| contents: read | ||
| id-token: write | ||
| env: | ||
| ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }} | ||
| ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }} | ||
| ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }} | ||
| ARM_USE_OIDC: true | ||
| ARM_USE_AZUREAD: true | ||
| ARM_STORAGE_USE_AZUREAD: true | ||
| ROLE_ARN: ${{ secrets.ROLE_ARN }} | ||
| AWS_REGION: ${{ secrets.AWS_REGION }} | ||
| steps: | ||
| - name: Check out released configuration | ||
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| with: | ||
| fetch-depth: 0 | ||
| filter: tree:0 | ||
| ref: ${{ needs.validate.outputs.source-ref }} | ||
|
|
||
| - name: Cloud Login | ||
| uses: pagopa/dx/actions/csp-login@main | ||
|
|
||
| - name: Setup Node.js | ||
| id: node-setup | ||
| uses: pagopa/dx/.github/actions/node-setup@main | ||
|
|
||
| - name: Setup Terraform | ||
| uses: pagopa/dx/.github/actions/terraform-setup@main | ||
|
|
||
| - name: Install dependencies (pnpm) | ||
| if: steps.node-setup.outputs.package-manager == 'pnpm' | ||
| run: pnpm install --frozen-lockfile | ||
|
|
||
| - name: Install dependencies (yarn) | ||
| if: steps.node-setup.outputs.package-manager == 'yarn' | ||
| run: yarn install --immutable | ||
|
|
||
| - name: Install dependencies (npm) | ||
| if: steps.node-setup.outputs.package-manager == 'npm' | ||
| run: npm ci | ||
|
|
||
| - name: Apply reviewed Terraform plan | ||
| env: | ||
| NX_PROJECT: ${{ inputs.project }} | ||
| run: npx nx release publish --projects="$NX_PROJECT" |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@pagopa/dx-tasks': minor | ||
| --- | ||
|
|
||
| Validate trusted Terraform environment release metadata through dx-tasks. |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| run-dx-task: minor | ||
| --- | ||
|
|
||
| Expose JSON task results and an explicit GitHub token input from the run-dx-task | ||
| action. |
This file contains hidden or 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
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import{createRequire as e}from"node:module";var t=Object.create,n=Object.defineProperty,r=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,a=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,s=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(e){throw n=[e],e}},c=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t),e=null),t.exports),l=(e,t)=>{let r={};for(var i in e)n(r,i,{get:e[i],enumerable:!0});return t||n(r,Symbol.toStringTag,{value:`Module`}),r},u=(e,t,a,s)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var c=i(t),l=0,u=c.length,d;l<u;l++)d=c[l],!o.call(e,d)&&d!==a&&n(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(s=r(t,d))||s.enumerable});return e},d=(e,r,i)=>(i=e==null?{}:t(a(e)),u(r||!e||!e.__esModule?n(i,`default`,{value:e,enumerable:!0}):i,e)),f=e=>o.call(e,`module.exports`)?e[`module.exports`]:u(n({},`__esModule`,{value:!0}),e),p=e(import.meta.url);export{f as a,p as i,s as n,d as o,l as r,c as t}; |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** Verifies GitHub token selection for the run-dx-task action. */ | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { resolveGitHubToken } from "../github-token.js"; | ||
|
|
||
| describe("resolveGitHubToken", () => { | ||
| it("prefers the explicit action input", () => { | ||
| expect(resolveGitHubToken("input-token", "environment-token")).toBe( | ||
| "input-token", | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to GITHUB_TOKEN when the input is empty", () => { | ||
| expect(resolveGitHubToken("", "environment-token")).toBe( | ||
| "environment-token", | ||
| ); | ||
| }); | ||
|
|
||
| it("returns undefined when neither source provides a token", () => { | ||
| expect(resolveGitHubToken("", undefined)).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** Resolves the GitHub token while preserving the action's legacy environment contract. */ | ||
|
|
||
| export const resolveGitHubToken = ( | ||
| inputToken: string, | ||
| environmentToken: string | undefined, | ||
| ): string | undefined => inputToken || environmentToken; |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** Configures focused unit tests for the run-dx-task action. */ | ||
|
|
||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| environment: "node", | ||
| include: ["src/**/*.test.ts"], | ||
| watch: false, | ||
| }, | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.