famous-river-94971
01/04/2023, 8:50 PMpants
for a monorepo of AWS Cloud Development Kit (CDK), an infrastructure as code tool, projects. I've got a decent prototype set up, and it looks like the --changed-since
flag is working great. It's identifying "targets" that should be redeployed when they change.
I'm struggling to figure out the best way to run a shell command on targets that have changed. For instance, this command is properly identifying code that has changed:
./pants --changed-since=HEAD --changed-dependees=transitive --filter-tag-regex='^cdk_app$' list
aws/projects/project_1:project_1
aws/projects/project_1/app.py
aws/projects/project_1/stack.py
Now I'd like to run a single command in project_1
(e.g., cd aws/projects/project_1 && ./my-script.sh
), but I'm not sure how to best do this. In this case my-script
would call out to the CDK binary to do the actual deployment 🧵famous-river-94971
01/04/2023, 8:50 PMfamous-river-94971
01/04/2023, 8:51 PMfamous-river-94971
01/04/2023, 8:51 PMfamous-river-94971
01/04/2023, 8:54 PMlist
is giving me a list of files, but I really only care about "targets" (e.g., just the project_1
path)curved-television-6568
01/04/2023, 9:06 PMcurved-television-6568
01/04/2023, 9:06 PM./pants peek
can be instructive here, revealing all the details.. 🙂famous-river-94971
01/04/2023, 9:06 PMfamous-river-94971
01/04/2023, 9:07 PMpython_source
files are changed, I know I need to run the CDK command in the top-level project folderfamous-river-94971
01/04/2023, 9:07 PMfamous-river-94971
01/04/2023, 9:08 PMaws/projects/project_1/**/*.py
file changes, I need to run a single command in aws/projects/project_1
curved-television-6568
01/04/2023, 9:08 PMexperimental_shell_command
per project that depend on the code, and then list those targets for the changed-since, should turn them up when their deps change.curved-television-6568
01/04/2023, 9:08 PMfamous-river-94971
01/04/2023, 9:09 PMcurved-television-6568
01/04/2023, 9:09 PMfamous-river-94971
01/04/2023, 9:10 PMcurved-television-6568
01/04/2023, 9:10 PMexperimental_run_shell_command
you want.. which you invoke explicitly with ./pants run …
famous-river-94971
01/04/2023, 9:11 PMrun
some custom code.curved-television-6568
01/04/2023, 9:11 PM./pants run src/python/main.py
…famous-river-94971
01/04/2023, 9:11 PMyarn lerna run --since '' --concurrency "$LERNA_CONCURRENCY" cdk -- deploy --progress events --require-approval never '**'
This is what I do for TypeScript projects using lernafamous-river-94971
01/04/2023, 9:12 PMcdk
executable installed on the CI runner. Then that binary reads the Python files where the infrastructure is defined.famous-river-94971
01/04/2023, 9:13 PMcurved-television-6568
01/04/2023, 9:13 PMfamous-river-94971
01/04/2023, 9:13 PMpolite-garden-50641
01/04/2023, 9:24 PMchanged_targets=`./pants --changed-since=<< pipeline.git.base_revision >> --changed-dependees=transitive --filter-target-type=pex_binary --filter-address-regex="src/python/toolchain/service.*" list
this is part of the script, the list of pex targets that need to be deployed is assigned into changed_targets
variable in the bash script (in CI)famous-river-94971
01/04/2023, 9:36 PMcommit 29ff052b413bbf497192973f28f776c6344cb512 (HEAD -> pants)
Author: Ben Limmer <ben@benlimmer.com>
Date: Wed Jan 4 14:24:30 2023 -0700
try adding a script
diff --git a/aws/projects/project_1/BUILD b/aws/projects/project_1/BUILD
index a15e303..1dcab2b 100644
--- a/aws/projects/project_1/BUILD
+++ b/aws/projects/project_1/BUILD
@@ -1,3 +1,9 @@
-python_sources(
- tags=["cdk_app"]
+python_sources()
+
+experimental_run_shell_command(
+ name="cdk",
+ tags=["cdk"],
+ command="../../scripts/cdk-deploy.sh",
+ dependencies=["aws/projects/project_1:project_1"],
+ workdir="aws/projects/project_1",
)
diff --git a/aws/scripts/BUILD b/aws/scripts/BUILD
new file mode 100644
index 0000000..6c95f66
--- /dev/null
+++ b/aws/scripts/BUILD
@@ -0,0 +1 @@
+shell_sources()
diff --git a/aws/scripts/cdk-deploy.sh b/aws/scripts/cdk-deploy.sh
new file mode 100755
index 0000000..e7a3bbc
--- /dev/null
+++ b/aws/scripts/cdk-deploy.sh
@@ -0,0 +1,4 @@
+#! /bin/bash
+set -e
+
+echo "hello from $(basename "$(pwd)")"
diff --git a/pants.toml b/pants.toml
index b0ce38c..2f954eb 100644
--- a/pants.toml
+++ b/pants.toml
@@ -3,6 +3,7 @@ pants_version = "2.14.0"
backend_packages = [
"pants.backend.python",
"pants.backend.python.lint.black",
+ "pants.backend.shell",
]
[source]
famous-river-94971
01/04/2023, 9:36 PMproject_1
source files (either directly or transitively) I get this:
./pants --changed-since=HEAD --changed-dependees=transitive --filter-tag-regex='^cdk$' run
hello from project_1
which is what I expect 🎉famous-river-94971
01/04/2023, 9:45 PM ./pants --changed-since=HEAD --changed-dependees=transitive --filter-tag-regex='^cdk$' list
aws/projects/project_1:cdk
aws/projects/project_2:cdk
It looks like I could use xargs like this to execute run
on each:
./pants --changed-since=HEAD --changed-dependees=transitive --filter-tag-regex='^cdk$' list | xargs -L1 ./pants run
hello from project_1
hello from project_2
famous-river-94971
01/04/2023, 9:46 PMfamous-river-94971
01/04/2023, 10:15 PMfamous-river-94971
01/04/2023, 10:32 PMpants export
and source the virtualenv. Then export PYTHONPATH
to get module resolution working.chilly-holiday-77415
01/05/2023, 9:46 AM