diff --git a/.github/workflows/git-commit.yml b/.github/workflows/git-commit.yml new file mode 100644 index 0000000..45b0aec --- /dev/null +++ b/.github/workflows/git-commit.yml @@ -0,0 +1,56 @@ +name: Git commit + +on: [pull_request] + +env: + sha: $GITHUB_SHA + +jobs: + git-commit: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Mark test file + run: | + echo $GITHUB_SHA > test02.md + mkdir docs + echo $GITHUB_SHA > docs/test01.md + echo $GITHUB_SHA > docs/test02.md + mkdir docs-test-folder + echo $GITHUB_SHA > docs-test-folder/test01.md + echo $(pwd) + + - name: Push specific file + uses: ./git-commit + env: + API_TOKEN_GITHUB: ${{ secrets.GITHUB_TOKEN }} + with: + src: 'test02.md' + dst_branch: 'git-commit-file' + pr_create: 'true' + pr_base: 'master' + pr_title: "Push specific file: ${{ github.repository }}" + + - name: Push specific folder + uses: ./git-commit + env: + API_TOKEN_GITHUB: ${{ secrets.GITHUB_TOKEN }} + with: + src: 'docs-test-folder/' + dst: 'docs-test-folder/' + dst_branch: 'git-commit-folder' + pr_create: 'true' + pr_base: 'master' + + - name: Push multiple files + uses: ./git-commit + env: + API_TOKEN_GITHUB: ${{ secrets.GITHUB_TOKEN }} + with: + src: 'docs/*.md' + dst: 'docs/' + dst_branch: 'git-commit-multiple-files' + pr_create: 'true' + pr_base: 'master' diff --git a/git-commit/Dockerfile b/git-commit/Dockerfile new file mode 100644 index 0000000..3d9d851 --- /dev/null +++ b/git-commit/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine + +RUN apk update && \ + apk upgrade && \ + apk add git curl + +ADD ./entrypoint.sh /entrypoint.sh + +ENTRYPOINT [ "/entrypoint.sh" ] \ No newline at end of file diff --git a/git-commit/action.yml b/git-commit/action.yml new file mode 100644 index 0000000..5ed40c3 --- /dev/null +++ b/git-commit/action.yml @@ -0,0 +1,55 @@ +name: 'Git commit' +description: 'Commit changed file to destination repository' +inputs: + src: + description: 'Source file from the origin directory. Can be a file, glob (`*.json`) or folder' + required: true + dst: + description: 'Directory to push the source to. Defaults to same folder as the source input' + required: false + dst_branch: + description: 'Destination branch' + required: true + # See https://api.github.com/users/github-actions%5Bbot%5D + user_name: + description: 'GitHub username for the commit. Defaults to GITHUB_ACTOR' + required: false + default: 'github-actions[bot]' + user_email: + description: 'Email for the git commit. Defaults to commit author email' + required: false + default: '41898282+github-actions[bot]@users.noreply.github.com' + repository: + description: 'Destination repository, if not provided it will commit back to the same repo' + required: false + commit_message: + description: 'A custom message for the commit' + required: false + pr_create: + description: 'Create pull request automatically if changes are successfully commited' + required: false + default: "false" + pr_title: + description: 'Pull request title' + required: false + pr_description: + description: 'Pull request description' + required: false + pr_base: + description: 'Create pull request again specific base. Defaults to master branch' + required: false + default: 'master' +runs: + using: 'docker' + image: 'Dockerfile' + args: + - ${{ inputs.src }} + - ${{ inputs.dst }} + - ${{ inputs.user-email }} + - ${{ inputs.user-name }} + - ${{ inputs.repository }} + - ${{ inputs.branch }} + - ${{ inputs.commit-message }} +branding: + icon: 'git-commit' + color: 'green' \ No newline at end of file diff --git a/git-commit/entrypoint.sh b/git-commit/entrypoint.sh new file mode 100755 index 0000000..3702126 --- /dev/null +++ b/git-commit/entrypoint.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +set -e +set -x + +if [ -z "$INPUT_REPOSITORY" ] +then + INPUT_REPOSITORY="${GITHUB_REPOSITORY}" +fi + +if [ -z "$INPUT_COMMIT_MESSAGE" ] +then + INPUT_COMMIT_MESSAGE="Generated from https://github.com/${GITHUB_REPOSITORY}/commit/${GITHUB_SHA}" +fi + +if [ -z "$INPUT_PR_TITLE" ] +then + INPUT_PR_TITLE="Automatic update from: ${GITHUB_REPOSITORY}" +fi + +if [ -z "$INPUT_PR_DESCRIPTION" ] +then + INPUT_PR_DESCRIPTION=${INPUT_COMMIT_MESSAGE} +fi + +if [ -z "$INPUT_DST_BRANCH" ] +then + INPUT_DST_BRANCH=${GITHUB_HEAD_REF} +fi + +CLONE_DIR=$(mktemp -d) + +echo "Cloning destination git repository" +git config --global user.name "$INPUT_USER_NAME" +git config --global user.email "$INPUT_USER_EMAIL" +git clone "https://x-access-token:$API_TOKEN_GITHUB@github.com/$INPUT_REPOSITORY.git" "$CLONE_DIR" + +BASE_DIR=$(pwd) +cd "$CLONE_DIR" + +echo "Creating new branch: ${INPUT_DST_BRANCH}" +git checkout -b "$INPUT_DST_BRANCH" +git reset --hard "origin/$INPUT_DST_BRANCH" || true + +DEST_COPY="$CLONE_DIR/$INPUT_DST" +if [ "$INPUT_DST" != "" ] +then + mkdir -p $DEST_COPY +fi + +echo "Copying contents to git repo" +cp -R $BASE_DIR/$INPUT_SRC "$DEST_COPY" + +echo "Adding git commit" +git add . +if git status | grep -q "Changes to be committed" +then + git commit --message "$INPUT_COMMIT_MESSAGE" + echo "Pushing git commit" + git push -u origin HEAD:"$INPUT_DST_BRANCH" + + if [ "$INPUT_PR_CREATE" == "true" ] + then + PR_DESCRIPTION_ESCAPED="${INPUT_PR_DESCRIPTION//$'\n'/\\n}" + + curl --connect-timeout 10 \ + -u "${INPUT_USER_NAME}:${API_TOKEN_GITHUB}" \ + -X POST -H 'Content-Type: application/json' \ + --data "{\"head\":\"$INPUT_DST_BRANCH\",\"base\":\"${INPUT_PR_BASE}\", \"title\": \"${INPUT_PR_TITLE}\", \"body\": \"${PR_DESCRIPTION_ESCAPED}\"}" \ + "https://api.github.com/repos/{$INPUT_REPOSITORY}/pulls" + fi +else + echo "No changes detected" +fi \ No newline at end of file