Search code examples
gitgithubgithub-actions

How can I modify files in a commit in a github action?


Hello I am building a github action in order to update the docker tag, in order to do this I have the following code:

git fetch --tags
serviceVersions=''
for serviceToRelease in ${SERVICES_RELEASE[@]};
  do
        latestRev=$(git tag --list --sort=-committerdate "*$serviceToRelease*" | head -n 1)
        version=$(echo $latestRev | sed -nre 's/^[^0-9]*(([0-9]+\.)+[0-9]+).*/\1/p')
        newVersion=$(echo "${version%.*}.$((${version##*.}+1))")
        
        git tag $serviceToRelease-$newVersion
        git push origin --tags

        serviceLowerCase=$(echo $serviceToRelease | sed -e 's/\(.*\)/\L\1/')

        docker build -t $serviceLowerCase -f ./components/$serviceToRelease/Dockerfile ./components/$serviceToRelease
        docker tag $serviceLowerCase tag/repo:$serviceLowerCase-$newVersion
        docker push tag/repo:$serviceLowerCase-$newVersion
        
        serviceVersions="${serviceToRelease}-${newVersion} ${serviceVersions}"
  done

This only creates a tag but I need to modify files and add them to the commit with

grep -rli $version * | xargs -i@ sed -i 's/$version/$newVersion/g'

In order to update the references to the tag inside the commit, I dont want make another commit because that trigger the action again, so I need modify the files and include in the same commit that trigger the github action.

How can I do this?


Solution

  • You can't. It is literally impossible to change anything about any existing commit.

    You can, of course, make a new and different commit that's exactly like the original commit except for whatever you would like changed. That's easy enough to do. But it's a new and different commit and will have a different hash ID. All repositories involved with these commits will need to connect to your repository, obtain the new commit, and switch to using the new commit.

    I am deploying kubeflow operators, and their have the version setted in a file, and I want to set the version in the file with a git push.

    You can't do that either: it's too late. The git push command pushes existing commits, but you need to make a new commit with this version-file in place. Then, once the version file is in place, you can git push the resulting commit. That's the way to go.

    In other words, replace this:

    git tag <name>
    git push <repo> <refspec>
    

    with this:

    <update version file>
    git add <version file>
    git commit -m 'automatic update of version file'
    git tag <name>
    git push <repo> <refspec>
    

    (with appropriate error checking and handling, of course).