Using Step or Anchor with After-Script
Asked by Bitwise DEVS:

Is it possible to call a step from an anchor with **after-script**? I cannot see any related article that is doing it as of the moment.

![Screenshot 2024-02-28 at 8.00.58?AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/309059iB5A9EEC7AF13DF15/image-size/large?v=v2&px=999 "Screenshot 2024-02-28 at 8.00.58?AM.png")

What we are trying to accomplish is to create an anchor that also calls another anchor whenever that build fails. Another challenge is when the step that we are trying to assess uses different image from what the **after-script** is trying to do.  

Thanks


Answers:
author: Theodora Boudale
body: Hi [@Bitwise DEVS](/t5/user/viewprofilepage/user-id/3162676),

It is not possible to call a step from the after-script. Just to give you some context, for each step in your yml file, a Docker container starts based on the Docker image you have specified in your yml file. The repo is cloned in that container and then the commands of that step's script are executed. The commands of the after-script are executed in the same Docker container, and then that container gets destroyed. So, the after-script can include one or multiple commands.

If certain tools you need are not available, you can install them during the step by adding the necessary commands in your yml file. Another option would be to build your own custom image with the tools you need, push it to Docker Hub or another registry, and then use this custom image as a build container. You can find some resources about this here:

* <https://support.atlassian.com/bitbucket-cloud/docs/use-docker-images-as-build-environments/#Creating-a-custom-build-environment>

Kind regards,  
Theodora

comments: author: Bitwise DEVS
body: If we have \`image\` in **step level** and **top level** which one is being used?  
Can we use \`pipe\` and \`image\` at the same time? Specifically use \`pipe\` in \`after-script\`?


author: Theodora Boudale
body: Hi [@Bitwise DEVS](/t5/user/viewprofilepage/user-id/3162676),
> If we have \`image\` in step level and top level which one is being used?

The image on the step level is going to be used. The top one will be used for steps that don't have an image on the step level (if any).
> Can we use \`pipe\` and \`image\` at the same time? Specifically use \`pipe\` in \`after-script\`?

Yes, the two options are not related to each other.

The image specifies which Docker image is going to be used for the build container where the step runs.

A pipe can be used either in the script or in the after-script of a step. The pipe will start a separate Docker container based on the pipe's Docker image.

Please feel free to let me know if you have any other questions.

Kind regards,  
Theodora


author: Bitwise DEVS
body: Is there a sample how to use pipe in after-script? Trying to write it this way but haven't actually run it.

```
- step: &create-keys # YAML anchor for reusable step https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/
    name: Create Keystore file
    script:
      - mkdir keys # Create new directory
      - echo $DEBUG_KEYSTORE_FILE_BASE64 | base64 -d > keys/$DEBUG_KEYSTORE_FILE # Decode Base64 then use DEBUG_KEYSTORE_FILE as file name defined in repository variable
    artifacts: # Keep these artifacts for subsequent pipeline steps
      - keys/** # Artifacts are files that are produced by a step https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/
    after-script:
      - if [[ $BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else echo "Step failed, generating build status badge"; fi
      - pipe: atlassian/bitbucket-upload-file:0.1.2 # Deploy your artifacts using the bitbucket-upload-file pipe approach https://support.atlassian.com/bitbucket-cloud/docs/deploy-build-artifacts-to-bitbucket-downloads/ or https://bitbucket.org/atlassian/bitbucket-upload-file/src/master/
          variables: # Bitbucket app passwords allow you to deploy an artifact that has been produced by your pipeline to Bitbucket Downloads
            BITBUCKET_USERNAME: $BITBUCKET_USERNAME
            BITBUCKET_APP_PASSWORD: $BUILD_STATUS_BADGE_PASSWORD
            FILENAME: 'status.svg' # The file to be uploaded on Bitbucket Downloads
```

<br />

Currently the JetBrain's IDE is marking it error **Invalid child element in a block mapping** not sure if it is false positive.

![Screenshot 2024-03-07 at 6.35.01?AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/310653i77DD487E6E31B0C1/image-size/large?v=v2&px=999 "Screenshot 2024-03-07 at 6.35.01?AM.png")


author: Theodora Boudale
body: Hi [@Bitwise DEVS](/t5/user/viewprofilepage/user-id/3162676),

The pipe is used in the after-script the same way it is used in the script.

Please ensure that

* **variables:** has 2 spaces indentation from the previous level, **- pipe: ...**
* **BITBUCKET_USERNAME** , **BITBUCKET_APP_PASSWORD** , and **FILENAME** have 2 spaces indentation from the previous level, **variables:**

Your IDE may still show an error, but the yml should work with this indentation.

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud