diff --git a/README.md b/README.md index d81af82..6a9f926 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/action.yml b/action.yml index 0e5798f..f6b60d5 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/entrypoint.sh b/entrypoint.sh index bd4f4b4..2f59285 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 @@ -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