iosxcodegithubgithub-actionsxcode-cloud

Automatic merge based on commit message


I am using Xcode Cloud with my iOS project. There are a few workflows based on branches and tags. Commonly one of them is finishing with app publication on the Test Flight.

My goal is to make an automation that will be run on develop branch every time when I will make some push. This automation will arise when script will find #promote tag inside commit message.

My first approach was to use post build script in Xcode Cloud as shown below:

#!/bin/sh

#  ci_post_xcodebuild.sh
#  BeforeDaily
#
#  Created by Piotrek on 28/08/2022.
#  

cd "$CI_WORKSPACE"
MESSAGE=$(git log -1 --oneline --format=%s | sed 's/^.*: //')

if [[ "$MESSAGE" == *"#promote"* ]]; then
    echo "Merging develop into stage"
    
    git fetch --all
    git checkout --track -b stage origin/stage
    git merge develop
    git push origin stage
    git checkout develop
    
    echo "Done"
else
    echo "There is nothing to do here..."
fi

This approach was promising but Xcode Cloud does not have write permissions on the Github project and it is not possible to change it. The result of the script is here:

2022-08-28T21:37:40.713643324Z  Merging develop into stage
2022-08-28T21:37:42.747978838Z  From http://github.com/<username>/<project_name>
2022-08-28T21:37:42.748494378Z   * [new branch]      develop    -> origin/develop
2022-08-28T21:37:42.749001513Z   * [new branch]      main       -> origin/main
2022-08-28T21:37:42.749224260Z   * [new branch]      stage      -> origin/stage
2022-08-28T21:37:42.749514956Z  Switched to a new branch 'stage'
2022-08-28T21:37:42.749752757Z  branch 'stage' set up to track 'origin/stage'.
2022-08-28T21:37:42.750006530Z  fatal: refusing to merge unrelated histories
2022-08-28T21:37:42.852059197Z  remote: Write access to repository not granted.
2022-08-28T21:37:42.852480361Z  fatal: unable to access 'http://github.com/piotrekjeremicz/beforedaily-swiftui-app.git/': The requested URL returned error: 403
2022-08-28T21:37:42.852854256Z  Switched to branch 'develop'
2022-08-28T21:37:42.853020173Z  Done

So the question is: Is there any method that could provide me automatic merge based on commit message content?


Solution

  • I could not find any other solution than make a clean clone and work on fresh git repository. Everything is done in Xcode Cloud Workflow Here is a final ci_post_xcbuild.sh that makes a merge to stage if some message on develop contains #promote string.

    #!/bin/sh
    
    cd "$CI_WORKSPACE"
    MESSAGE=$(git log -1 --oneline --format=%s | sed 's/^.*: //')
    
    if [[ "$CI_BRANCH" == develop && "$MESSAGE" == *"#promote"* ]]; then
        echo "Automerge develop branch into stage"
        
        mkdir tempclone
        cd tempclone
        
        git clone https://x-access-token:$GITHUB_TOKEN@github.com/github_account/github_repo_name.git
        cd beforedaily-swiftui-app
        
        git config --global user.email "xcodecloud@jeremicz.com"
        git config --global user.name "Xcode Cloud"
    
        git checkout --track -b stage origin/stage
        git merge origin/develop
        git push origin stage
        
        cd ../../
        rm -r tempclone
        
    else
        echo "There is nothing to do here..."
    fi
    

    This script works with Github Token as an environment value that could be setup in Workflow settings.