Skip to content

Commit b939e8c

Browse files
Merge pull request #20 from devops-infra/feature/git-push-force
Enable `git push --force`
2 parents 3fa15a7 + c505c35 commit b939e8c

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Features:
1010
* Can add a timestamp to a branch name, when `target_branch` is set and `add_timestamp` is `true`. Will create a branch named `${branch_name}/${add_timestamp}`. Great for cron-based updates.
1111
* As a commit message will use `commit_message` if set, or `commit_prefix` and add changed files or just list changed files.
1212
* Good to combine with my other action [devops-infra/action-pull-request](https://github.com/devops-infra/action-pull-request).
13+
* Can use `git push --force` for fast-forward changes.
1314

1415

1516
## Badge swag
@@ -41,22 +42,24 @@ Features:
4142
add_timestamp: true
4243
commit_prefix: "[AUTO]"
4344
commit_message: "Automatic commit"
45+
force: false
4446
target_branch: update/version
4547
```
4648
4749
48-
Input Variable | Required | Default |Description
49-
:--- | :---: | :---: | :---
50-
github_token | Yes | `""` | Personal Access Token for GitHub for pushing the code.
51-
add_timestamp | No | `false` | Whether to add the timestamp to a new branch name. Used when `target_branch` is set. Uses format `%Y-%m-%dT%H-%M-%SZ`.
52-
commit_prefix | No | `[AUTO-COMMIT]` | Prefix added to commit message.
53-
commit_message | No | `""` | Full commit message to set.
54-
target_branch | No | *current branch* | Name of a new branch to push the code into. Creates branch if not existing.
50+
| Input Variable | Required | Default | Description |
51+
| -------------- | -------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------- |
52+
| github_token | Yes | `""` | Personal Access Token for GitHub for pushing the code. |
53+
| add_timestamp | No | `false` | Whether to add the timestamp to a new branch name. Used when `target_branch` is set. Uses format `%Y-%m-%dT%H-%M-%SZ`. |
54+
| commit_prefix | No | `[AUTO-COMMIT]` | Prefix added to commit message. |
55+
| commit_message | No | `""` | Full commit message to set. |
56+
| force | No | `false` | Whether to use force push for fast-forward changes. Use only if necessary. |
57+
| target_branch | No | *current branch* | Name of a new branch to push the code into. Creates branch if not existing. |
5558

56-
Outputs | Description
57-
:--- | :---
58-
files_changed | List of changed files. As returned by `git diff --staged --name-status`.
59-
branch_name | Name of the branch code was pushed into.
59+
| Outputs | Description |
60+
| ------------- | ------------------------------------------------------------------------ |
61+
| files_changed | List of changed files. As returned by `git diff --staged --name-status`. |
62+
| branch_name | Name of the branch code was pushed into. |
6063

6164

6265
## Examples

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ inputs:
1818
description: Full commit message to set
1919
required: false
2020
default: ""
21+
force:
22+
description: Whether to force push
23+
required: false
24+
default: "false"
2125
target_branch:
2226
description: Name of a new branch to push the code into
2327
required: false

entrypoint.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ echo " commit_prefix: ${INPUT_COMMIT_PREFIX}"
1010
echo " commit_message: ${INPUT_COMMIT_MESSAGE}"
1111
echo " target_branch: ${INPUT_TARGET_BRANCH}"
1212
echo " add_timestamp: ${INPUT_ADD_TIMESTAMP}"
13+
echo " force: ${INPUT_FORCE}"
1314

1415
# Require github_token
1516
if [[ -z "${GITHUB_TOKEN}" ]]; then
@@ -45,12 +46,14 @@ if [[ (-n "${INPUT_TARGET_BRANCH}" || "${INPUT_ADD_TIMESTAMP}" == "true") && -n
4546
git checkout -b "${BRANCH}"
4647
fi
4748

49+
# Set git credentials
50+
git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}"
51+
git config --global user.name "${GITHUB_ACTOR}"
52+
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
53+
4854
# Create an auto commit
4955
if [[ -n ${FILES_CHANGED} ]]; then
50-
echo "[INFO] Committing and pushing changes."
51-
git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}"
52-
git config --global user.name "${GITHUB_ACTOR}"
53-
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
56+
echo "[INFO] Committing changes."
5457
git add -A
5558
if [[ -n "${INPUT_COMMIT_MESSAGE}" ]]; then
5659
git commit -am "${INPUT_COMMIT_MESSAGE}" --allow-empty
@@ -59,6 +62,19 @@ if [[ -n ${FILES_CHANGED} ]]; then
5962
else
6063
git commit -am "Files changed:" -m "${FILES_CHANGED}" --allow-empty
6164
fi
65+
if [[ "${INPUT_FORCE}" == "true" ]]; then
66+
git push origin "${BRANCH}" --force
67+
else
68+
git push origin "${BRANCH}"
69+
fi
70+
fi
71+
72+
# Push
73+
if [[ "${INPUT_FORCE}" == "true" ]]; then
74+
echo "[INFO] Pushing changes."
75+
git push origin "${BRANCH}" --force
76+
elif [[ -n ${FILES_CHANGED} ]]; then
77+
echo "[INFO] Pushing changes."
6278
git push origin "${BRANCH}"
6379
fi
6480

0 commit comments

Comments
 (0)