gitbitbucketbitbucket-pipelines

How to make zip file from git for each folder


I have this pipeline which will generate zip file only with diff files and send it to my FTP store.

pipelines:
  branches:
    master:
      - step:
          name: Update package
          image: python:latest #3.13.5, 3.13, 3, latest:
          script:
            - apt-get update
            - apt-get -qq install zip curl sudo
            - export VERSION_LABEL=$(TZ=":Europe/Berlin" date +%Y-%m-%d_%H:%M:%S)
            - echo "Repo name is $BITBUCKET_REPO_SLUG & version is $VERSION_LABEL"
            - echo "Dir build $BITBUCKET_CLONE_DIR"
            - export APP_VERSION=$(git show HEAD:libs/Core/Const.php | grep 'public const string VERSION = ' | sed -e 's/.*= //;s/;//' | sed 's/"//g' | sed "s/'//g" | tr '.' '_')
            - echo "App version is $APP_VERSION"
            # Update ZIP package
            - mkdir $BITBUCKET_REPO_SLUG
            - export ZIP_FILE=update_$BITBUCKET_REPO_SLUG--$APP_VERSION--$VERSION_LABEL.zip
            - export FILES=$(git diff-tree --no-commit-id --name-only --diff-filter=ACMR -r HEAD^^..HEAD -- ":(exclude)plugin")
            - echo $FILES
            - if [ -n "$FILES" ]; then
                cp -R --parents $FILES $BITBUCKET_REPO_SLUG/ 2>/dev/null &&
                rm -f $BITBUCKET_REPO_SLUG/bitbucket-pipelines.yml &&
                rm -f $BITBUCKET_REPO_SLUG/.gitignore &&
                rm -rf $BITBUCKET_REPO_SLUG/_sources &&
                rm -rf $BITBUCKET_REPO_SLUG/_test &&
                rm -rf $BITBUCKET_REPO_SLUG/plugin &&
                find $BITBUCKET_REPO_SLUG/ -empty -type d -delete &&
                zip -r $ZIP_FILE $BITBUCKET_REPO_SLUG/ || true &&
                curl -T $ZIP_FILE ftp://update.example.com/ --user $FTP_USERNAME:$FTP_PASSWORD &&
                rm -f $ZIP_FILE || true &&
                rm -rf $BITBUCKET_REPO_SLUG &&
                echo "Zip file $ZIP_FILE";
              else
                echo "No any files to zip.";
              fi

Now, I want do it the same with folder plugin/ which contains separated folders with each plugin code but I need separated zip files for each plugin folder. I dont know how many plugins are there before. Is it possible to do it with some loop which will make same diff-tree check and make the same as process with main code?

Plugin folders are like:

plugin/aaa/*.*
plugin/first/*.*
plugin/other/*.*
...

How to make zip files like this and send them to store too?

update_plugin_aaa--VERSION--DATE.zip
update_plugin_first--VERSION--DATE.zip
update_plugin_other--VERSION--DATE.zip
...

Solution

  • I'm not familiar with Bitbucket pipelines, but if you're asking how to create in Git (like your title suggests) a patch file with the diffs for each subfolder in the plugin directory, you could iterate through each subdirectory with a for loop, something like for dir in plugin/*/, save the name of the folder currently iterated, and then write the result of git diff in a patch file named after the subdirectory via the redirection operator.

    for dir in plugin/*/
    do
      folder=$(basename "$dir")
      git diff-tree --no-commit-id --name-only --diff-filter=ACMR -r HEAD^^..HEAD "$dir" > "diff_${folder}.patch"
    done