Improved Stability of Pushing

- Updated code from the template I used
This commit is contained in:
2020-02-17 22:42:00 +01:00
parent c4e0bcd525
commit b8682e0294

View File

@@ -3,41 +3,42 @@
set -eux set -eux
# Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh) # Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh)
git_setup ( ) { _git_setup ( ) {
cat <<- EOF > $HOME/.netrc
machine github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
machine api.github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
EOF
chmod 600 $HOME/.netrc
git config --global user.email "actions@github.com" git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions" git config --global user.name "Prettier Action"
} }
# Calling method to configure the git environemnt _git_changed() {
git_setup [[ -n "$(git status -s)" ]]
}
_git_push() {
if [ -z "$INPUT_BRANCH" ]
then
git push origin
else
git push --set-upstream origin "HEAD:$INPUT_BRANCH"
fi
}
echo "Installing prettier..." echo "Installing prettier..."
npm install --silent --global prettier npm install --silent --global prettier
echo "Prettifing files..." echo "Prettifing files..."
prettier $INPUT_PRETTIER_OPTIONS || echo "Problem while prettifying your files!" prettier $INPUT_PRETTIER_OPTIONS || echo "Problem while prettifying your files!"
if ! git diff --quiet if _git_changed;
then then
# Calling method to configure the git environemnt
_git_setup
echo "Commiting and pushing changes..." echo "Commiting and pushing changes..."
# The remote changes should already be fetched, but as there is #4 this could resolve the problem
git fetch
# Switch to the actual branch # Switch to the actual branch
git checkout $INPUT_BRANCH git checkout $INPUT_BRANCH
# Add changes to git # Add changes to git
git add "${INPUT_FILE_PATTERN}" git add "${INPUT_FILE_PATTERN}"
# Commit and push changes back # Commit and push changes back
git commit -m "$INPUT_COMMIT_MESSAGE" --author="$GITHUB_ACTOR <$GITHUB_ACTOR@users.noreply.github.com>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"} git commit -m "$INPUT_COMMIT_MESSAGE" --author="$GITHUB_ACTOR <$GITHUB_ACTOR@users.noreply.github.com>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"}
git push --set-upstream origin "HEAD:$INPUT_BRANCH" _git_push
echo "Changes pushed successfully." echo "Changes pushed successfully."
else else
echo "Nothing to commit. Exiting." echo "Nothing to commit. Exiting."