Shell script self update using git

前端 未结 1 1147
温柔的废话
温柔的废话 2021-01-05 20:49

I\'m writing a script to execute tasks before my end to end tests. One of the steps is to select the branch where those tests were written. Sometimes the script changes betw

相关标签:
1条回答
  • 2021-01-05 21:04

    This is the script I came up with:

    #!/bin/bash
    
    SCRIPT=$(readlink -f "$0")
    SCRIPTPATH=$(dirname "$SCRIPT")
    SCRIPTNAME="$0"
    ARGS="$@"
    BRANCH="Your_git_branch"
    
    self_update() {
        cd $SCRIPTPATH
        git fetch
    
        [ -n $(git diff --name-only origin/$BRANCH | grep $SCRIPTNAME) ] && {
            echo "Found a new version of me, updating myself..."
            git pull --force
            git checkout $BRANCH
            git pull --force
            echo "Running the new version..."
            exec "$SCRIPTNAME" "$@"
    
            # Now exit this old instance
            exit 1
        }
        echo "Already the latest version."
    }
    
    main() {
       echo "Running"
    }
    
    self_update
    main
    
    0 讨论(0)
提交回复
热议问题