<@ULVA73B9P> Can I expand a custom Block type in a...
# ask-marvin
j
@Marvin Can I expand a custom Block type in a custom .run_shell_script: step in a prefect.yaml file? If so, how?
Copy code
pull:
- prefect.deployments.steps.run_shell_script:
m
thought for 118.5 seconds
Based on the research and verification, here's what you need to know about using custom blocks with
run_shell_script
steps in a
prefect.yaml
file: 1. First, you'll need to install the shell integration:
Copy code
bash
pip install "prefect[shell]"
2. Yes, you can use custom blocks in a
run_shell_script
step in your
prefect.yaml
. There are two main approaches: ### Option 1: Using Block References
Copy code
yaml
pull:
  - prefect.deployments.steps.run_shell_script:
      script: |
        echo "Using block: {{ my_custom_block.some_field }}"
      env:
        SOME_VAR: "{{ my_custom_block.some_field }}"
### Option 2: Using Block Loading
Copy code
yaml
pull:
  - prefect.deployments.steps.run_shell_script:
      script: |
        echo "Using block: {{ prefect.blocks.my-block-type.block-name.some_field }}"
To make this work: 1. Your custom block must be registered with Prefect first:
Copy code
bash
prefect block register --module your_module.blocks
2. Create an instance of your block either through the UI or programmatically. 3. In your
prefect.yaml
, you can reference block values using the templating syntax
{{ }}
. The block values will be expanded when the deployment step runs. Important Notes: - Make sure your custom block is properly registered and available in the Prefect server - The block must be accessible in the environment where the deployment runs - Values are expanded at runtime, so make sure any sensitive information is properly handled - The exact fields you can reference depend on your custom block's structure If you need help with a specific block type or field, please share more details about your custom block's structure and I can provide more targeted guidance.
j
@Marvin the below step is failing:
Copy code
- prefect.deployments.steps.run_shell_script:
      id: vgt-install
      script: |
          export ARTIFACTORY_UW2_USER="{{ prefect.blocks.secret.artifactory-uw2-user }}"
          export ARTIFACTORY_UW2_API_TOKEN="{{ prefect.blocks.secret.artifactory-uw2-api-token }}"
          pip install --upgrade --pre -i "https://${ARTIFACTORY_UW2_USER}:${ARTIFACTORY_UW2_API_TOKEN}@artifactory-uw2.mycompany.com/artifactory/api/pypi/my-pypi-index" my_package
      stream_output: true
With an error like this:
Copy code
Invalid template. Only a single block placeholder is allowed in a string and no surrounding text is allowed.
What is the correct way to pass more than one block value to a single command?
m
thought for 46.2 seconds
Based on the error message and Prefect 3.x's templating rules, the issue is that you can't combine block references with other text in a single string. Here's how to fix your script using environment variables in the proper format:
Copy code
yaml
- prefect.deployments.steps.run_shell_script:
      id: vgt-install
      script: |
          pip install --upgrade --pre -i "https://$ARTIFACTORY_USER:$ARTIFACTORY_TOKEN@artifactory-uw2.mycompany.com/artifactory/api/pypi/my-pypi-index" my_package
      env:
        ARTIFACTORY_USER: "{{ prefect.blocks.secret.artifactory-uw2-user }}"
        ARTIFACTORY_TOKEN: "{{ prefect.blocks.secret.artifactory-uw2-api-token }}"
      stream_output: true
Key changes: 1. Moved the block references to the
env
section where each environment variable can have a single block reference 2. Referenced these environment variables in the script using
$ARTIFACTORY_USER
and
$ARTIFACTORY_TOKEN
3. Removed the
export
statements as they're not needed - the
env
section handles this for you This approach works because: - Each environment variable in the
env
section can contain exactly one block reference - The environment variables are then available to your script - The templating restriction of "single block placeholder with no surrounding text" is satisfied This is the recommended pattern when you need to use multiple block values in a single command or script.