Skip to content
Closed
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
27 changes: 27 additions & 0 deletions .github/workflows/git-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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

- name: Push changed file
uses: ./git-commit
env:
API_TOKEN_GITHUB: ${{ secrets.GITHUB_TOKEN }}
with:
src: 'test02.md'
dst_branch: 'git-commit-test'
user_email: 'lj@horizon.io'
pr_create: 'true'
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" ]
52 changes: 52 additions & 0 deletions git-commit/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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
user_email:
description: 'Email for the git commit'
required: true
user_name:
description: 'GitHub username for the commit. Defaults to GITHUB_ACTOR'
required: false
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'
73 changes: 73 additions & 0 deletions git-commit/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/sh

set -e
set -x

if [ -z "$INPUT_REPOSITORY" ]
then
INPUT_REPOSITORY="${GITHUB_REPOSITORY}"
fi

if [ -z "$INPUT_USER_NAME" ]
then
INPUT_USER_NAME="${GITHUB_ACTOR}"
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
PR_TITLE="Automatic update from: ${GITHUB_REPOSITORY}"
fi

if [ -z "$INPUT_PR_DESCRIPTION" ]
then
PR_DESCRIPTION=${INPUT_COMMIT_MESSAGE}
fi

echo "Printing environment variables"
printenv

CLONE_DIR=$(mktemp -d)

echo "Cloning destination git repository"
git config --global user.email "$INPUT_USER_EMAIL"
git config --global user.name "$INPUT_USER_NAME"
git clone --branch "$GITHUB_HEAD_REF" "https://x-access-token:$API_TOKEN_GITHUB@github.com/$INPUT_REPOSITORY.git" "$CLONE_DIR"

DEST_COPY="$CLONE_DIR/$INPUT_DST"

echo "Copying contents to git repo"
cp -R "$INPUT_SRC" "$DEST_COPY"

cd "$CLONE_DIR"

echo "Creating new branch: ${INPUT_DST_BRANCH}"
git checkout -b "$INPUT_DST_BRANCH"
git reset --hard "origin/$INPUT_DST_BRANCH" || true
OUTPUT_BRANCH="$INPUT_DST_BRANCH"

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:"$OUTPUT_BRANCH"

if [ "$INPUT_PR_CREATE" == "true" ]
then
PR_DESCRIPTION_ESCAPED="${PR_DESCRIPTION//$'\n'/\\n}"

curl --connect-timeout 10 \
-u "${INPUT_USER_NAME}:${API_TOKEN_GITHUB}" \
-X POST -H 'Content-Type: application/json' \
--data "{\"head\":\"$OUTPUT_BRANCH\",\"base\":\"${INPUT_PR_BASE}\", \"title\": \"${PR_TITLE}\", \"body\": \"${PR_DESCRIPTION_ESCAPED}\"}" \
"https://api.github.com/repos/{$INPUT_REPOSITORY}/pulls"
fi
else
echo "No changes detected"
fi
1 change: 1 addition & 0 deletions test02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1dec6a42382aea780832977954c58f5ef632bb57