How to access artifacts that are only available locally on the Bamboo Agents

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

This KB article proposes an alternative solution for customers who have builds with an Agent-local artifact handler and want to download such artifacts without directly accessing the Agents.

Environment

Diagnosis

In the case of using an  Agent-local artifact handler, artifacts aren't downloadable from the build result pages unless an additional Remote, S3, or SFTP handler is also enabled. This is intentional.

To download the artifacts via the UI, it is advised to configure an additional artifact handler to expose them to the web interface.

Use case

In certain scenarios, such as a secure Bamboo Agents domain where transferring artifacts back to the Bamboo instance is not desired due to security, privacy, and disk space constraints, it becomes challenging for developers to access such artifacts for additional review. They are required to conduct a series of manual steps to:

  1. Identify the Agent that executed the build
  2. Request access to that specific server
  3. Locate and copy the generated artifact

Solution

Download Artifacts from an Agents Plan

The proposed solution involves a Bamboo Plan that runs exclusively on each Agent, locates the artifacts, and shares them as new artifacts after a customised build. This Plan executes a script based on a customised variable to find the artifacts generated by the specified build, copies these artifacts to the current Plan's working directory, publishes the artifacts using an artifact handler that supports remote sharing and sends files to the Bamboo server, and finally cleans up the working directory to save space on the Agent.

Here's the step-by-step process:

  1. Start by creating an "Artifact downloader" Plan with an empty variable, for example, buildResultArtifactToFetch, for each Agent that will be running a build with a configured Agent-local artifact handler. Feel free to organise the Plan in a dedicated Project if you see fit.
  2. Add a single job to that Plan containing a requirement that is unique to each Agent, for instance, agent_name = agent123
  3. Add that same requirement as a custom capability to your Agent. Ensure it's exclusive so that the build always runs on the same agent
  4. Add an artifact configuration to that Job that will scan the artifacts folder and match **/* (everything)
  5. Add a Script task with the following content. Remember to review the script and adjust the BAMBOO_AUTH variable as needed

    The script below will work on UNIX/Linux Agents. If your Agent runs on Windows you must create an equivalent process using PowerShell or other supported scripting language

    #!/bin/bash
    
    # The Build to fetch artifacts from
    BAMBOO_BUILD_ARTIFACT_TO_FETCH=${bamboo_buildResultArtifactToFetch}
    
    # Exits if ${bamboo_buildResultArtifactToFetch} is empty
    if [ -z ${BAMBOO_BUILD_ARTIFACT_TO_FETCH} ] ; then
      echo "buildResultArtifactToFetch was not declared. This variable must be declared in a custom build. Exiting"
      exit 0
    fi
    
    # Extract the build number
    BAMBOO_BUILD_NUM=$(printf "build-%05d" "${BAMBOO_BUILD_ARTIFACT_TO_FETCH##*-}")
    
    # Credentials (can be a Personal Access Token) - https://confluence.atlassian.com/bamboo/personal-access-tokens-976779873.html
    #BAMBOO_AUTH="-H 'Authorization: Bearer AAAAAbbbbbbCCCCCCddddd1111112222233334444'"
    BAMBOO_AUTH="-u bamboo_user:password"
    
    # Locate the folder that will store the Artifacts from the declred build
    BAMBOO_URL="$(echo ${bamboo_buildResultsUrl} | grep -oP 'https?://[^/]+')"
    BAMBOO_PLANDIRECTORY_URL="${BAMBOO_URL}/rest/api/latest/planDirectoryInfo/${BAMBOO_BUILD_ARTIFACT_TO_FETCH}"
    BAMBOO_ARTIFACTS_FOLDER="$(curl -s ${BAMBOO_AUTH} ${BAMBOO_PLANDIRECTORY_URL} | jq -r '.results[0].storageTag')"
    
    # If you have a custom storage location in Agent-local artifact handler configuration, change it below
    # If a custom Agent-local artifact handler storage location is not set, it defaults to ${WRAPPER_BIN_DIR}
    # find that on Bamboo Administrator -> Artifact Handlers -> Agent-local artifact handler (storage location)
    : ${BAMBOO_LOCAL_ARTIFACTS_CUSTOM_FOLDER:=${WRAPPER_BIN_DIR}}
    
    # Builds the location to find the artifacts. Do not change this
    BAMBOO_LOCAL_ARTIFACTS_FOLDER="${BAMBOO_LOCAL_ARTIFACTS_CUSTOM_FOLDER}/${BAMBOO_ARTIFACTS_FOLDER}"
    
    # Scan the BAMBOO_LOCAL_ARTIFACTS_FOLDER and build a list that matches the specified build number
    BAMBOO_ARTIFACTS_LIST=$(find ${BAMBOO_LOCAL_ARTIFACTS_FOLDER} -type d -name ${BAMBOO_BUILD_NUM})
    
    # If something is found go ahead
    if [ -n "${BAMBOO_ARTIFACTS_LIST}" ] ; then
      # Copy the artifacts BAMBOO_LOCAL_ARTIFACTS_FOLDER
      FETCH_ARTIFACTS_FOLDER="${bamboo_build_working_directory}/artifacts/${BAMBOO_BUILD_ARTIFACT_TO_FETCH}"
      rm -rf ${FETCH_ARTIFACTS_FOLDER}
      mkdir -p ${FETCH_ARTIFACTS_FOLDER}
      for artifact in ${BAMBOO_ARTIFACTS_LIST}; do
        # Extract the job name from the path
        job_name=$(basename $(dirname ${artifact}))
    
        # Create a directory for the job if it doesn't exist
        job_directory=${FETCH_ARTIFACTS_FOLDER}/${job_name}
        mkdir -p ${job_directory}
    
        # Copy the artifact to the job directory with a prefix
        cp -a ${artifact}/* ${job_directory}
        echo "Artifact for ${artifact} copied to ${job_directory}"
      done
    else
      echo "No artifacts found for ${BAMBOO_BUILD_ARTIFACT_TO_FETCH}"
    fi
  6. In the Job configuration, click on "Other" and select "Clean working directory after each build"
  7. Switch to the Plan's configuration "Other" tab, ensure the "Use custom artifact handler settings" is selected and has an artifact handler that will publish them back to the Bamboo Server, in case the default artifact handler is different.
  8. Run a customised manual build of that Plan by assigning it with a custom variable value for the buildResultArtifactToFetch variable with the Build number that contains the artifacts you want to fetch. Once the build is complete, the artifacts will be available in the UI and will be linked to the new Plan's build.



Last modified on Jan 3, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.