From ef7ff81f5564f55007433eb2e9b78229ad60c39a Mon Sep 17 00:00:00 2001 From: Christopher Rybicki Date: Sun, 2 Jan 2022 18:34:14 -0800 Subject: [PATCH] fix: package-js task not checked by mergify (#1426) Co-authored-by: github-actions --- .gitattributes | 1 - .github/workflows/build.yml | 26 +++++ .github/workflows/integ.yml | 36 ------- .gitignore | 1 - .mergify.yml | 3 +- .projen/files.json | 1 - .projen/tasks.json | 3 +- .projenrc.js | 44 ++------- API.md | 74 ++++++++++++++ src/build/build-workflow.ts | 119 ++++++++++++++++++++++- test/gitlab/gitlab-configuration.test.ts | 4 +- 11 files changed, 228 insertions(+), 84 deletions(-) delete mode 100644 .github/workflows/integ.yml diff --git a/.gitattributes b/.gitattributes index 138c2d6e3af..0d6d40beb32 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,7 +7,6 @@ /.github/pull_request_template.md linguist-generated /.github/workflows/auto-approve.yml linguist-generated /.github/workflows/build.yml linguist-generated -/.github/workflows/integ.yml linguist-generated /.github/workflows/pull-request-lint.yml linguist-generated /.github/workflows/release.yml linguist-generated /.github/workflows/stale.yml linguist-generated diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 349d0a1892b..30642f1a92c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -186,3 +186,29 @@ jobs: run: cd .repo && npx projen package:go - name: Collect go Artifact run: mv .repo/dist dist + post-build-integ: + needs: build + runs-on: ubuntu-latest + permissions: + contents: read + if: "! needs.build.outputs.self_mutation_happened" + steps: + - uses: actions/setup-python@v2 + with: + python-version: 3.x + - uses: actions/setup-go@v2 + with: + go-version: 1.16.x + - name: Download build artifacts + uses: actions/download-artifact@v2 + with: + name: build-artifact + path: dist + - name: Checkout + uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + - name: Install dependencies + run: yarn install --check-files --frozen-lockfile + - run: /bin/bash ./projen.bash integ diff --git a/.github/workflows/integ.yml b/.github/workflows/integ.yml deleted file mode 100644 index 1fe8d467a76..00000000000 --- a/.github/workflows/integ.yml +++ /dev/null @@ -1,36 +0,0 @@ -# ~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen". - -name: integ -on: - pull_request: {} - workflow_dispatch: {} -jobs: - integ: - runs-on: ubuntu-latest - permissions: - contents: read - env: - CI: "true" - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Set git identity - run: |- - git config user.name "github-actions" - git config user.email "github-actions@github.com" - - name: Setup Node.js - uses: actions/setup-node@v2.2.0 - with: - node-version: 12.22.0 - - name: Install dependencies - run: yarn install --check-files --frozen-lockfile - - name: Set up Python 3.x - uses: actions/setup-python@v2 - with: - python-version: 3.x - - name: Set up Go 1.16 - uses: actions/setup-go@v2 - with: - go-version: ^1.16.0 - - name: integ - run: /bin/bash ./projen.bash integ diff --git a/.gitignore b/.gitignore index a0064634477..b0ff156b7f5 100644 --- a/.gitignore +++ b/.gitignore @@ -59,5 +59,4 @@ tsconfig.json !templates/** !/.markdownlint.json !/.vscode/launch.json -!/.github/workflows/integ.yml !/.github/workflows/upgrade-main.yml diff --git a/.mergify.yml b/.mergify.yml index ea4301ee3dc..cee30863e81 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -16,10 +16,11 @@ pull_request_rules: - "#approved-reviews-by>=1" - -label~=(do-not-merge) - status-success=build - - status-success=integ + - status-success=package-js - status-success=package-java - status-success=package-python - status-success=package-go + - status-success=post-build-integ - name: Label core contributions actions: label: diff --git a/.projen/files.json b/.projen/files.json index a9539295aea..c0f450af860 100644 --- a/.projen/files.json +++ b/.projen/files.json @@ -6,7 +6,6 @@ ".github/pull_request_template.md", ".github/workflows/auto-approve.yml", ".github/workflows/build.yml", - ".github/workflows/integ.yml", ".github/workflows/pull-request-lint.yml", ".github/workflows/release.yml", ".github/workflows/stale.yml", diff --git a/.projen/tasks.json b/.projen/tasks.json index 02f303b08ec..699b246a75a 100644 --- a/.projen/tasks.json +++ b/.projen/tasks.json @@ -138,9 +138,10 @@ }, "integ": { "name": "integ", + "description": "Run integration tests", "steps": [ { - "spawn": "build" + "spawn": "compile" }, { "spawn": "package:python" diff --git a/.projenrc.js b/.projenrc.js index 227e6379de4..eef2edcb743 100644 --- a/.projenrc.js +++ b/.projenrc.js @@ -178,47 +178,15 @@ const pythonCompatTask = project.addTask("integ:python-compat", { description: "Checks that projen's submodule structure does not cause import failures for python. Expects python to be installed and projen to be fully built.", }); -const integTask = project.addTask("integ"); -integTask.spawn(project.buildTask); +const integTask = project.addTask("integ", { + description: "Run integration tests", +}); +integTask.spawn(project.compileTask); integTask.spawn(project.tasks.tryFind("package:python")); integTask.spawn(pythonCompatTask); -project - .tryFindObjectFile(".mergify.yml") - .addOverride("pull_request_rules.0.conditions.3", "status-success=integ"); - -new github.TaskWorkflow(project.github, { - name: "integ", - jobId: "integ", - triggers: { - pullRequest: {}, - workflowDispatch: {}, - }, - env: { - CI: "true", - }, - permissions: { - contents: workflows.JobPermission.READ, - }, - - preBuildSteps: [ - ...project.installWorkflowSteps, // install dependencies for projen - { - name: "Set up Python 3.x", - uses: "actions/setup-python@v2", - with: { - "python-version": "3.x", - }, - }, - { - name: "Set up Go 1.16", - uses: "actions/setup-go@v2", - with: { - "go-version": "^1.16.0", - }, - }, - ], - task: integTask, +project.buildWorkflow.addPostBuildJobTask(integTask, { + tools: { python: { version: "3.x" }, go: { version: "1.16.x" } }, }); project.synth(); diff --git a/API.md b/API.md index a83dbfcac6a..b8cafdc798b 100644 --- a/API.md +++ b/API.md @@ -183,6 +183,8 @@ Name|Description [awscdk.IntegrationTestOptions](#projen-awscdk-integrationtestoptions)|Options for `IntegrationTest`. [awscdk.LambdaFunctionCommonOptions](#projen-awscdk-lambdafunctioncommonoptions)|Common options for `LambdaFunction`. [awscdk.LambdaFunctionOptions](#projen-awscdk-lambdafunctionoptions)|Options for `Function`. +[build.AddPostBuildJobCommandsOptions](#projen-build-addpostbuildjobcommandsoptions)|Options for `BuildWorkflow.addPostBuildJobCommands`. +[build.AddPostBuildJobTaskOptions](#projen-build-addpostbuildjobtaskoptions)|Options for `BuildWorkflow.addPostBuildJobTask`. [build.BuildWorkflowOptions](#projen-build-buildworkflowoptions)|*No description* [cdk.Catalog](#projen-cdk-catalog)|*No description* [cdk.ConstructLibraryOptions](#projen-cdk-constructlibraryoptions)|*No description* @@ -4056,6 +4058,50 @@ addPostBuildJob(id: string, job: Job): void +#### addPostBuildJobCommands(id, commands, options?)🔹 + +Run a sequence of commands as a job within the build workflow which is executed after the build job succeeded. + +Jobs are executed _only_ if the build did NOT self mutate. If the build +self-mutate, the branch will either be updated or the build will fail (in +forks), so there is no point in executing the post-build job. + +```ts +addPostBuildJobCommands(id: string, commands: Array, options?: AddPostBuildJobCommandsOptions): void +``` + +* **id** (string) *No description* +* **commands** (Array) *No description* +* **options** ([build.AddPostBuildJobCommandsOptions](#projen-build-addpostbuildjobcommandsoptions)) Specify tools and other options. + * **checkoutRepo** (boolean) Check out the repository at the pull request branch before commands are run. __*Default*__: false + * **installDeps** (boolean) Install project dependencies before running commands. `checkoutRepo` must also be set to true. __*Default*__: false + * **tools** ([github.workflows.Tools](#projen-github-workflows-tools)) Tools that should be installed before the commands are run. __*Optional*__ + + + + +#### addPostBuildJobTask(task, options)🔹 + +Run a task as a job within the build workflow which is executed after the build job succeeded. + +The job will have access to build artifacts and will install project +dependencies in order to be able to run any commands used in the tasks. + +Jobs are executed _only_ if the build did NOT self mutate. If the build +self-mutate, the branch will either be updated or the build will fail (in +forks), so there is no point in executing the post-build job. + +```ts +addPostBuildJobTask(task: Task, options: AddPostBuildJobTaskOptions): void +``` + +* **task** ([Task](#projen-task)) *No description* +* **options** ([build.AddPostBuildJobTaskOptions](#projen-build-addpostbuildjobtaskoptions)) Specify tools and other options. + * **tools** ([github.workflows.Tools](#projen-github-workflows-tools)) Tools that should be installed before the task is run. __*Optional*__ + + + + #### addPostBuildSteps(...steps)🔹 Adds steps that are executed after the build. @@ -11656,6 +11702,34 @@ Name | Type | Description +## struct AddPostBuildJobCommandsOptions 🔹 + + +Options for `BuildWorkflow.addPostBuildJobCommands`. + + + +Name | Type | Description +-----|------|------------- +**checkoutRepo**?🔹 | boolean | Check out the repository at the pull request branch before commands are run.
__*Default*__: false +**installDeps**?🔹 | boolean | Install project dependencies before running commands. `checkoutRepo` must also be set to true.
__*Default*__: false +**tools**?🔹 | [github.workflows.Tools](#projen-github-workflows-tools) | Tools that should be installed before the commands are run.
__*Optional*__ + + + +## struct AddPostBuildJobTaskOptions 🔹 + + +Options for `BuildWorkflow.addPostBuildJobTask`. + + + +Name | Type | Description +-----|------|------------- +**tools**?🔹 | [github.workflows.Tools](#projen-github-workflows-tools) | Tools that should be installed before the task is run.
__*Optional*__ + + + ## struct BuildWorkflowOptions 🔹 diff --git a/src/build/build-workflow.ts b/src/build/build-workflow.ts index 711e486e978..e383b51aaf2 100644 --- a/src/build/build-workflow.ts +++ b/src/build/build-workflow.ts @@ -6,7 +6,8 @@ import { DEFAULT_GITHUB_ACTIONS_USER, } from "../github/constants"; import { WorkflowActions } from "../github/workflow-actions"; -import { Job, JobPermission, JobStep } from "../github/workflows-model"; +import { Job, JobPermission, JobStep, Tools } from "../github/workflows-model"; +import { NodeProject } from "../javascript"; import { Project } from "../project"; const PULL_REQUEST_REF = "${{ github.event.pull_request.head.ref }}"; @@ -84,7 +85,7 @@ export class BuildWorkflow extends Component { private readonly buildTask: Task; private readonly github: GitHub; private readonly workflow: GithubWorkflow; - private readonly artifactsDirectory?: string; + private readonly artifactsDirectory: string; private readonly _postBuildJobs: string[] = []; @@ -196,6 +197,82 @@ export class BuildWorkflow extends Component { this._postBuildJobs.push(id); } + /** + * Run a task as a job within the build workflow which is executed after the + * build job succeeded. + * + * The job will have access to build artifacts and will install project + * dependencies in order to be able to run any commands used in the tasks. + * + * Jobs are executed _only_ if the build did NOT self mutate. If the build + * self-mutate, the branch will either be updated or the build will fail (in + * forks), so there is no point in executing the post-build job. + * + * @param options Specify tools and other options + */ + public addPostBuildJobTask(task: Task, options: AddPostBuildJobTaskOptions) { + this.addPostBuildJobCommands( + `post-build-${task.name}`, + [`${this.project.projenCommand} ${task.name}`], + { + checkoutRepo: true, + installDeps: true, + tools: options.tools, + } + ); + } + + /** + * Run a sequence of commands as a job within the build workflow which is + * executed after the build job succeeded. + * + * Jobs are executed _only_ if the build did NOT self mutate. If the build + * self-mutate, the branch will either be updated or the build will fail (in + * forks), so there is no point in executing the post-build job. + * + * @param options Specify tools and other options + */ + public addPostBuildJobCommands( + id: string, + commands: string[], + options?: AddPostBuildJobCommandsOptions + ) { + const steps = []; + + if (options?.checkoutRepo) { + steps.push({ + name: "Checkout", + uses: "actions/checkout@v2", + with: { + ref: PULL_REQUEST_REF, + repository: PULL_REQUEST_REPOSITORY, + }, + }); + } + + if ( + options?.checkoutRepo && + options?.installDeps && + this.project instanceof NodeProject + ) { + steps.push({ + name: "Install dependencies", + run: `${this.project.package.installCommand}`, + }); + } + + steps.push({ run: commands.join("\n") }); + + this.addPostBuildJob(id, { + permissions: { + contents: JobPermission.READ, + }, + tools: options?.tools, + runsOn: ["ubuntu-latest"], + steps, + }); + } + private addSelfMutationJob() { this.workflow.addJob("self-mutation", { runsOn: ["ubuntu-latest"], @@ -271,3 +348,41 @@ export class BuildWorkflow extends Component { ]; } } + +/** + * Options for `BuildWorkflow.addPostBuildJobTask` + */ +export interface AddPostBuildJobTaskOptions { + /** + * Tools that should be installed before the task is run. + */ + readonly tools?: Tools; +} + +/** + * Options for `BuildWorkflow.addPostBuildJobCommands` + */ +export interface AddPostBuildJobCommandsOptions { + /** + * Tools that should be installed before the commands are run. + */ + readonly tools?: Tools; + + /** + * Check out the repository at the pull request branch before commands are + * run. + * + * @default false + */ + readonly checkoutRepo?: boolean; + + /** + * Install project dependencies before running commands. `checkoutRepo` must + * also be set to true. + * + * Currently only supported for `NodeProject`. + * + * @default false + */ + readonly installDeps?: boolean; +} diff --git a/test/gitlab/gitlab-configuration.test.ts b/test/gitlab/gitlab-configuration.test.ts index 54f1a37246e..0d5f59c782d 100644 --- a/test/gitlab/gitlab-configuration.test.ts +++ b/test/gitlab/gitlab-configuration.test.ts @@ -9,9 +9,7 @@ test("throws when adding an adding a job to a non-existant nested template", () const c = new GitlabConfiguration(p); c.createNestedTemplates({ foo: {} }); // THEN - expect(() => c.nestedTemplates.bar.addStages("baz")).toThrow( - /Cannot read property '\w+' of undefined/ - ); + expect(() => c.nestedTemplates.bar.addStages("baz")).toThrowError; }); test("does not throw when adding an services with an existing nested template", () => {