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
56 changes: 56 additions & 0 deletions .github/workflows/git-commit.yml
Original file line number Diff line number Diff line change
@@ -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 }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we call this GITHUB_TOKEN too?

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'
9 changes: 9 additions & 0 deletions git-commit/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine

RUN apk update && \
apk upgrade && \
apk add git curl

ADD ./entrypoint.sh /entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]
55 changes: 55 additions & 0 deletions git-commit/action.yml
Original file line number Diff line number Diff line change
@@ -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'
74 changes: 74 additions & 0 deletions git-commit/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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