Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ This action supports three tag levels for flexible versioning:
| `no_edit` | No | `false` | Whether to not edit commit message when using amend (`--no-edit`). |
| `organization_domain` | No | `github.com` | GitHub Enterprise domain name. |
| `target_branch` | No | *current branch* | Name of a new branch to push the code into. Creates branch if not existing unless there are no changes and `amend` is false. |
| `repository_path` | No | `.` | Relative path under `${{ github.workspace }}` where the repository is checked out. Set this when `actions/checkout` uses `path:`. |


### 📤 Output Parameters
Expand Down Expand Up @@ -186,6 +187,34 @@ jobs:
force_with_lease: true # Safer force push option
```

### 📁 Custom checkout path example
Commit and push when `actions/checkout` uses a custom path.

```yaml
name: Commit from custom checkout path
on:
push
jobs:
change-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout repository into custom path
uses: actions/checkout@v6
with:
path: work/repo

- name: Change something in checked out repository
run: |
echo "Updated" >> work/repo/README.md

- name: Commit and push changes
uses: devops-infra/action-commit-push@v1.2.2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
repository_path: work/repo
commit_message: "Update README"
```

## 📝 Amend Options
When using `amend: true`, you have several options for handling the commit message:

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ inputs:
description: Name of a new branch to push the code into (skipped when no changes and amend is false)
required: false
default: ""
repository_path:
description: Relative path under GITHUB_WORKSPACE to the checked-out repository (use when actions/checkout path is set)
required: false
default: "."
outputs:
files_changed:
description: List of changed files
Expand Down
40 changes: 37 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ echo " fail_on_rebase_conflict: ${INPUT_FAIL_ON_REBASE_CONFLICT}"
echo " no_edit: ${INPUT_NO_EDIT}"
echo " organization_domain: ${INPUT_ORGANIZATION_DOMAIN}"
echo " target_branch: ${INPUT_TARGET_BRANCH}"
echo " repository_path: ${INPUT_REPOSITORY_PATH}"

# Require github_token
if [[ -z "${GITHUB_TOKEN}" ]]; then
Expand All @@ -28,12 +29,45 @@ if [[ -z "${GITHUB_TOKEN}" ]]; then
exit 1
fi

REPOSITORY_PATH="${INPUT_REPOSITORY_PATH:-.}"
if [[ -z "${REPOSITORY_PATH}" ]]; then
REPOSITORY_PATH="."
fi
if [[ "${REPOSITORY_PATH}" == /* ]]; then
echo "[ERROR] Input 'repository_path' must be a relative path under GITHUB_WORKSPACE."
exit 1
fi

WORKSPACE_DIR="$(realpath -m "${GITHUB_WORKSPACE}")"
REPO_DIR="$(realpath -m "${GITHUB_WORKSPACE}/${REPOSITORY_PATH}")"
if [[ "${REPO_DIR}" != "${WORKSPACE_DIR}" && "${REPO_DIR}" != "${WORKSPACE_DIR}"/* ]]; then
echo "[ERROR] Input 'repository_path' resolves outside GITHUB_WORKSPACE."
exit 1
fi
if [[ ! -d "${REPO_DIR}" ]]; then
echo "[ERROR] Repository path does not exist: ${REPO_DIR}"
exit 1
fi
if ! git -C "${REPO_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "[ERROR] Path is not a git repository: ${REPO_DIR}"
exit 1
fi
echo "[INFO] Using repository path: ${REPO_DIR}"

# Keep all global git config isolated to a temp file
export GIT_CONFIG_GLOBAL
GIT_CONFIG_GLOBAL="$(mktemp /tmp/action-commit-push-git-config-XXXXXX)"
trap 'rm -f "${GIT_CONFIG_GLOBAL}"' EXIT

# Set git credentials
git config --global safe.directory "${GITHUB_WORKSPACE}"
git config --global safe.directory /github/workspace
git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGANIZATION_DOMAIN}/${GITHUB_REPOSITORY}"
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}"
git config --global safe.directory "${REPO_DIR}"
git -C "${REPO_DIR}" remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGANIZATION_DOMAIN}/${GITHUB_REPOSITORY}"
git -C "${REPO_DIR}" config user.name "${GITHUB_ACTOR}"
git -C "${REPO_DIR}" config user.email "${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}"

cd "${REPO_DIR}"

get_current_branch() {
local branch
Expand Down
Loading