Home > Mobile >  Bash - Find changed files in folder compared to latest commit
Bash - Find changed files in folder compared to latest commit

Time:01-12

I'm trying to write a bash script that work into a gitlab pipeline that detect the changed files. Once, I retrieve the list of folder in the root project, I check if there is a file in the latest commit that are in this folder. If true I upload them into a s3 bucket.

This is my pipeline code:

stages:
  - deploy

file-distributions:
  image: python:latest
  stage: deploy
  before_script:
    - pip install awscli
  script:
    - >
      FILES_IN_FOLDER=$(ls -ad */)
      TO_BE_UPLOADED=()
      CHANGED_FILES=()

      for file in $(git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT $CI_COMMIT_SHA); do
        CHANGED_FILES =("$file")
      done

      for folder in $FILES_IN_FOLDER; do
        for file in $CHANGED_FILES; do
          if [[ "$file" == *"$folder"* ]]; then 
            TO_BE_UPLOADED =("$file")
          fi
        done
      done

      echo $TO_BE_UPLOADED
      if [ ${#TO_BE_UPLOADED[@]} -eq 0 ]; then
        echo "No files to upload"
        exit 0
      else
        aws s3 sync ./ s3://$S3_BUCKET/ --recursive --exclude "*" --include '"$(echo ${TO_BE_UPLOADED// /,})"'
      fi

When the script run into the pipeline I got this error:

/bin/bash: eval: line 133: syntax error near unexpected token `then'

Where I wrong?

CodePudding user response:

There must be a space between if and [, like this: This

if [[ "$file" == *"$folder"* ]]; then

must be

if [ "$file" == *"$folder"* ]; then 

CodePudding user response:

I dont know if this is an option, but GitLab also offers the built-in feature of Artifacts to automatically upload changed/created files during a pipeline.

Artifacts can also be configured to be uploaded to S3. While this is an instance wide configuration, ment to be used to store them in a safe place, you could also use it to retrieve them from there.

  •  Tags:  
  • Related