https://pantsbuild.org/ logo
#general
Title
# general
k

kind-angle-20894

02/02/2023, 1:50 PM
Follow-up question: Where does the command look for my script file?
Copy code
experimental_shell_command(
    command="./script.sh",
    tools=["echo", "bash"],
    dependencies=[":docker"],
)
f

fresh-cat-90827

02/02/2023, 2:51 PM
you want
experimental_run_shell_command
if you want to run the script directly in your workspace
k

kind-angle-20894

02/02/2023, 3:07 PM
I want to run it directly after I build a docker image. Is that possible?
So that when I run
./pants publish <my-image>
the images will build, then my script will run
f

fresh-cat-90827

02/02/2023, 3:14 PM
just to clarify, you would use the
experimental_shell_command
like this:
Copy code
python_tests(name="root-run-py", dependencies=[":root-script"], sources=["test_run.py"])

experimental_shell_command(
    name="root-script",
    command="./script.sh",
    tools=["echo", "bash"],
    dependencies=[":shell-scripts"],
    outputs=["my-script.log"],
    log_output=True,
)

shell_sources(name="shell-scripts", sources=["script.sh"])
Copy code
# test_run.py
def test_example():
    with open("my-script.log") as fh:
        assert fh.read().strip() == "hello"
script.sh
Copy code
#!/usr/bin/env bash
echo "hello" > my-script.log
now
Copy code
❯ ./pants test test_run.py
15:11:05.83 [INFO] Completed: Run Pytest - //test_run.py:root-run-py succeeded.

✓ //test_run.py:root-run-py succeeded in 0.23s.
so here we ask Pants to run the
script.sh
before running the test (it creates a file in the sandbox where test is run). And we declare the dependency via
dependencies=[":root-script"]
in the
python_tests
target
So that when I run ./pants publish <my-image> the images will build, then my script will run
I don't think it is possible OOTB. Without writing a plugin, I think it would be just easier to do a separate
./pants run
. Perhaps all the information you need can be extracted from the targets via the
peek
goal?
k

kind-angle-20894

02/02/2023, 3:20 PM
So it's simple to run the script before the python program using dependencies, but it's hard to run it afterwards?
Hmm, I think I actually got it to do what I wanted, kind of.
🙌 1
Copy code
experimental_run_shell_command(
    name="hello",
    command="docker images",
    dependencies=[":docker"],
)
./pants run packages/backend:hello
This triggers a docker build and then runs the command
Is it possible to pass arguments to the docker target in this way?
f

fresh-cat-90827

02/02/2023, 3:23 PM
Is it possible to pass arguments to the docker target in this way?
sorry, I am not sure I follow
k

kind-angle-20894

02/02/2023, 3:25 PM
If I want to pass a
--tag
argument to the docker target as though I was calling
/pants --tag=blabla publish packages/backend:docker
right now I guess my shell command just kind of calls
package
with no arguments on my docker target
f

fresh-cat-90827

02/02/2023, 3:27 PM
I haven't dealt with Docker images for a while now, but I think you may want https://www.pantsbuild.org/docs/tagging-docker-images#using-env-vars-to-include-dynamic-data-in-tags
k

kind-angle-20894

02/02/2023, 3:30 PM
I'll read up on this. Thanks!
c

curved-television-6568

02/02/2023, 4:07 PM
when you invoke pants
./pants …
you can pass additional options for any/all subsystems as needed along with it, so something like
./pants run --docker-build-args="…" packages/backend:hello
would allow you to provide options for docker on the command line when running your shell script target.
there’s no way currently to have that setup from your shell script target, though..
7 Views