Search code examples
antscpbamboobitbucket-server

Bamboo SCP Task without maintaining directory structure


I seem to be having trouble figuring out how to move the contents of my build directory to the server using SCP without copying the actual build folder. My current process is this:

  1. Bamboo build checks out source code from Stash
  2. Ant will create a build directory and copy all necessary files to it
  3. A shared artifact is then created. (the build directory itself)

So my artifact structure looks something like this

build/
- img/
- css/
- index.html

When I copy to the server, I always get the build folder itself on the server. So my root folder ends up looking like: /root/build/rest_of_files

What I'm trying to achieve is copying all of the files from the build directory and placing them at the root level. So my final root folder would look like:

root/
- img/
- css/
- index.html

I've tried different local paths, using and not using ant patterns. I've tried different patterns for the artifact itself instead, but I just can't seem to figure out what I need to do.

I hope this is clear.


Solution

  • Ok I figured out a way to complete what I was looking to do. Basically, I ended up creating a few more targets in my ant build file that (re)moved files to achieve the structure that I wanted.

    <?xml version="1.0" encoding="utf-8"?>
    <project name="my_project_name" basedir="." default="build">
        <!-- dev directory -->
        <property name="dev.dir" value="dev"></property>
        <!-- public directory -->
        <property name="public.dir" value="${dev.dir}/public/"></property>
        <!-- output directory -->
        <property name="build.dir" value="build"/>
        <!-- public_html directory -->
        <property name="web.dir" value="${build.dir}/public_html/"></property>
    
        <!-- create the build directory -->
        <target name="migrate" description="Make required directories">
            <echo message="Creating directories."/>
            <!-- make the build directory if it doesn't exist -->
            <mkdir dir="${build.dir}/"/>
            <!-- make the public_html directory -->
            <mkdir dir="${web.dir}/"/>
            <!-- call our target that copies the files to the build folder -->
            <antcall target="copyToBuild"/>
        </target>
    
        <!-- list of includes -->
        <target name="copyToBuild" description="Copy files to build folder">
            <echo message="Building."/>
            <!-- copy to the build directory -->
            <copy todir="${build.dir}">
                <fileset dir="${dev.dir}">
                    <!-- includes -->
                    <include name="**"></include>
                    <!-- excludes -->
                    <exclude name="build.xml"></exclude>
                    <exclude name="public/**"></exclude>
                </fileset>
            </copy>
            <!-- copy files to the public_html directory -->
            <copy includeEmptyDirs="true" todir="${web.dir}">
                <fileset dir="${public.dir}">
                    <include name="**"></include>
                    <!-- exclude the less directory -->
                    <exclude name="**/less/**"></exclude>
                </fileset>
            </copy>
    
            <!-- call the clean target -->
            <antcall target="clean"/>
        </target>
    
        <!-- Clean the root folder -->
        <target name="clean" description="Clean the root directory">
            <echo message="Cleaning."></echo>
            <!-- delete all except the build directory -->
            <delete includeemptydirs="true">
                <fileset dir="${basedir}" includes="**/*">
                    <include name="**/*"></include>
                    <exclude name="build/**"></exclude>
                </fileset>
            </delete>
            <!-- delete the dev folder -->
            <delete dir="${dev.dir}"></delete>
            <!-- copy files to the root directory -->
            <copy todir="${basedir}">
                <fileset dir="${build.dir}">
                    <include name="**/*"></include>
                </fileset>
            </copy>
            <!-- delete the build directory -->
            <delete dir="${build.dir}"></delete>
            <!-- delete the public directory -->
            <delete dir="${public.dir}"></delete>
        </target>
    </project>