Skip to content

Commit bf37438

Browse files
feat: add repository_path input for custom checkout paths in action
Relates to #96
1 parent aaaa95b commit bf37438

3 files changed

Lines changed: 70 additions & 3 deletions

File tree

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ This action supports three tag levels for flexible versioning:
8484
| `no_edit` | No | `false` | Whether to not edit commit message when using amend (`--no-edit`). |
8585
| `organization_domain` | No | `github.com` | GitHub Enterprise domain name. |
8686
| `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. |
87+
| `repository_path` | No | `.` | Relative path under `${{ github.workspace }}` where the repository is checked out. Set this when `actions/checkout` uses `path:`. |
8788

8889

8990
### 📤 Output Parameters
@@ -186,6 +187,34 @@ jobs:
186187
force_with_lease: true # Safer force push option
187188
```
188189

190+
### 📁 Custom checkout path example
191+
Commit and push when `actions/checkout` uses a custom path.
192+
193+
```yaml
194+
name: Commit from custom checkout path
195+
on:
196+
push
197+
jobs:
198+
change-and-push:
199+
runs-on: ubuntu-latest
200+
steps:
201+
- name: Checkout repository into custom path
202+
uses: actions/checkout@v6
203+
with:
204+
path: work/repo
205+
206+
- name: Change something in checked out repository
207+
run: |
208+
echo "Updated" >> work/repo/README.md
209+
210+
- name: Commit and push changes
211+
uses: devops-infra/action-commit-push@v1.2.2
212+
with:
213+
github_token: ${{ secrets.GITHUB_TOKEN }}
214+
repository_path: work/repo
215+
commit_message: "Update README"
216+
```
217+
189218
## 📝 Amend Options
190219
When using `amend: true`, you have several options for handling the commit message:
191220

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ inputs:
5858
description: Name of a new branch to push the code into (skipped when no changes and amend is false)
5959
required: false
6060
default: ""
61+
repository_path:
62+
description: Relative path under GITHUB_WORKSPACE to the checked-out repository (use when actions/checkout path is set)
63+
required: false
64+
default: "."
6165
outputs:
6266
files_changed:
6367
description: List of changed files

entrypoint.sh

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ echo " fail_on_rebase_conflict: ${INPUT_FAIL_ON_REBASE_CONFLICT}"
1919
echo " no_edit: ${INPUT_NO_EDIT}"
2020
echo " organization_domain: ${INPUT_ORGANIZATION_DOMAIN}"
2121
echo " target_branch: ${INPUT_TARGET_BRANCH}"
22+
echo " repository_path: ${INPUT_REPOSITORY_PATH}"
2223

2324
# Require github_token
2425
if [[ -z "${GITHUB_TOKEN}" ]]; then
@@ -28,12 +29,45 @@ if [[ -z "${GITHUB_TOKEN}" ]]; then
2829
exit 1
2930
fi
3031

32+
REPOSITORY_PATH="${INPUT_REPOSITORY_PATH:-.}"
33+
if [[ -z "${REPOSITORY_PATH}" ]]; then
34+
REPOSITORY_PATH="."
35+
fi
36+
if [[ "${REPOSITORY_PATH}" == /* ]]; then
37+
echo "[ERROR] Input 'repository_path' must be a relative path under GITHUB_WORKSPACE."
38+
exit 1
39+
fi
40+
41+
WORKSPACE_DIR="$(realpath -m "${GITHUB_WORKSPACE}")"
42+
REPO_DIR="$(realpath -m "${GITHUB_WORKSPACE}/${REPOSITORY_PATH}")"
43+
if [[ "${REPO_DIR}" != "${WORKSPACE_DIR}" && "${REPO_DIR}" != "${WORKSPACE_DIR}"/* ]]; then
44+
echo "[ERROR] Input 'repository_path' resolves outside GITHUB_WORKSPACE."
45+
exit 1
46+
fi
47+
if [[ ! -d "${REPO_DIR}" ]]; then
48+
echo "[ERROR] Repository path does not exist: ${REPO_DIR}"
49+
exit 1
50+
fi
51+
if ! git -C "${REPO_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
52+
echo "[ERROR] Path is not a git repository: ${REPO_DIR}"
53+
exit 1
54+
fi
55+
echo "[INFO] Using repository path: ${REPO_DIR}"
56+
57+
# Keep all global git config isolated to a temp file
58+
export GIT_CONFIG_GLOBAL
59+
GIT_CONFIG_GLOBAL="$(mktemp /tmp/action-commit-push-git-config-XXXXXX)"
60+
trap 'rm -f "${GIT_CONFIG_GLOBAL}"' EXIT
61+
3162
# Set git credentials
3263
git config --global safe.directory "${GITHUB_WORKSPACE}"
3364
git config --global safe.directory /github/workspace
34-
git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGANIZATION_DOMAIN}/${GITHUB_REPOSITORY}"
35-
git config --global user.name "${GITHUB_ACTOR}"
36-
git config --global user.email "${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}"
65+
git config --global safe.directory "${REPO_DIR}"
66+
git -C "${REPO_DIR}" remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGANIZATION_DOMAIN}/${GITHUB_REPOSITORY}"
67+
git -C "${REPO_DIR}" config user.name "${GITHUB_ACTOR}"
68+
git -C "${REPO_DIR}" config user.email "${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}"
69+
70+
cd "${REPO_DIR}"
3771

3872
get_current_branch() {
3973
local branch

0 commit comments

Comments
 (0)