Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
astralwaveio committed May 18, 2023
0 parents commit 4745dc2
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM alpine:latest

RUN apk add --no-cache \
bash \
git

RUN adduser -D ci

ADD *.sh /home/ci/

RUN chmod 555 /home/ci/*.sh

ENTRYPOINT ["/home/ci/entrypoint.sh"]
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2019, Markus Heene <markus.heene@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Sync and merge upstream repository

同步并合并上游存储库和当前存储库; 这是一个用于合并远程更改的 Github Action。

## 用例

- 在保持最新状态的同时保留 repo(而不是 clone 它)。
- 有一个与上游同步的分支,并将更改拉入开发分支。

## 用法

> 注意: 需要在设置项目中设置环境变量: `UPSTREAM_REPO_VAR` ,要同步的仓库地址,以 `.git` 结尾。
- 示例:

```YAML
name: Sync and merge upstream repository

env:
# [Required]: URL of upstream
UPSTREAM_URL: ${{ vars.UPSTREAM_REPO_VAR }}
# [Required]: ${{ secrets.GITHUB_TOKEN }} After the account is generated, no separate settings are required
# Over here, we use a PAT instead to authenticate workflow file changes.
WORKFLOW_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# [Optional]: defaults to main
UPSTREAM_BRANCH: "main"
# [Optional]: defaults to main
DOWNSTREAM_BRANCH: "main"
# [Optional]: fetch arguments
FETCH_ARGS: ""
# [Optional]: merge arguments
MERGE_ARGS: ""
# [Optional]: push arguments
PUSH_ARGS: ""
# [Optional]: toggle to spawn time logs (keeps action active); "true" or "false"; defaults to false
SPAWN_LOGS: "true"

# Runs daily at 03:15
on:
schedule:
- cron: '15 3 * * *'
# Allows manual workflow run (must in default branch to work)
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: GitHub Sync to Upstream Repository
uses: maximohub/sync-merge-upstream-repo@v0.8
with:
upstream_repo: ${{ env.UPSTREAM_URL }}
upstream_branch: ${{ env.UPSTREAM_BRANCH }}
downstream_branch: ${{ env.DOWNSTREAM_BRANCH }}
token: ${{ env.WORKFLOW_TOKEN }}
fetch_args: ${{ env.FETCH_ARGS }}
merge_args: ${{ env.MERGE_ARGS }}
push_args: ${{ env.PUSH_ARGS }}
spawn_logs: ${{ env.SPAWN_LOGS }}
```
此操作每天在 03:15 UTC 同步您在分支的仓库(合并来自上游的更改)。 请注意:GitHub Action 计划的工作流在被推送到队列时通常会面临延迟,延迟通常在 1 小时以内。
注意:如果 `SPAWN_LOGS` 设置为 `true` ,此操作将在根目录中创建一个 `sync-merge-upstream-repo` 文件,其中包含操作运行时的时间戳。这是为了减轻 `GitHub` 在检测到不活动时禁用回购操作的麻烦。

## 发展

在 [`action.yml`](https://github.com/maximohub/sync-merge-upstream-repo/blob/master/action.yml), 我们定义 `inputs`.
然后我们将这些参数传递给 [`Dockerfile`](https://github.com/maximohub/sync-merge-upstream-repo/blob/master/Dockerfile), 然后传递给 [`entrypoint.sh`](https://github.com/maximohub/sync-merge-upstream-repo/blob/master/entrypoint.sh).

`entrypoint.sh` 做的工作如下:

- 设置变量。
- 设置 git 配置。
- 克隆下游存储库。
- 获取上游存储库。
53 changes: 53 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Sync and merge upstream repository
description: sync and merge upstream repository and current repository.
author: Maximo

inputs:
upstream_repo:
description: Upstream repo URL (GitHub)
required: true
upstream_branch:
description: Upstream Branch (which repo to sync)
required: false
default: "main"
downstream_branch:
description: Downstream Branch (Current repo sync)
required: false
default: "main"
token:
description: GitHub Bot token
required: true
fetch_args:
description: Git fetch arguments
required: false
default: ""
merge_args:
description: Git merge arguments
required: false
default: ""
push_args:
description: Git push arguments
required: false
default: ""
spawn_logs:
description: Toggle to spawn `sync-merge-upstream-repo` with time logs
required: true
default: false

runs:
using: "docker"
image: "Dockerfile"
args:
- ${{ inputs.upstream_repo }}
- ${{ inputs.upstream_branch }}
- ${{ inputs.downstream_branch }}
- ${{ inputs.token }}
- ${{ inputs.fetch_args }}
- ${{ inputs.merge_args }}
- ${{ inputs.push_args }}
- ${{ inputs.spawn_logs }}

branding:
icon: "git-merge"
color: "purple"

70 changes: 70 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash

set -x

UPSTREAM_REPO=$1
UPSTREAM_BRANCH=$2
DOWNSTREAM_BRANCH=$3
GITHUB_TOKEN=$4
FETCH_ARGS=$5
MERGE_ARGS=$6
PUSH_ARGS=$7
SPAWN_LOGS=$8

if [[ -z "$UPSTREAM_REPO" ]]; then
echo "Missing \$UPSTREAM_REPO"
exit 1
fi

if [[ -z "$DOWNSTREAM_BRANCH" ]]; then
echo "Missing \$DOWNSTREAM_BRANCH"
echo "Default to ${UPSTREAM_BRANCH}"
DOWNSTREAM_BREANCH=UPSTREAM_BRANCH
fi

if ! echo "$UPSTREAM_REPO" | grep '\.git'; then
UPSTREAM_REPO="https://github.com/${UPSTREAM_REPO_PATH}.git"
fi

echo "UPSTREAM_REPO=$UPSTREAM_REPO"

git clone "https://github.com/${GITHUB_REPOSITORY}.git" work
cd work || { echo "Missing work dir" && exit 2 ; }

git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git config --local user.password ${GITHUB_TOKEN}

git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"

git remote add upstream "$UPSTREAM_REPO"
git fetch ${FETCH_ARGS} upstream
git remote -v

git checkout ${DOWNSTREAM_BRANCH}

case ${SPAWN_LOGS} in
(true) echo -n "sync-merge-upstream-repo https://github.com/maximohub/sync-merge-upstream-repo keeping CI alive."\
"UNIX Time: " >> sync-merge-upstream-repo
date +"%s" >> sync-merge-upstream-repo
git add sync-merge-upstream-repo
git commit sync-merge-upstream-repo -m "Syncing...";;
(false) echo "Not spawning time logs"
esac

git push origin

MERGE_RESULT=$(git merge ${MERGE_ARGS} upstream/${UPSTREAM_BRANCH})


if [[ $MERGE_RESULT == "" ]]
then
exit 1
elif [[ $MERGE_RESULT != *"Already up to date."* ]]
then
git commit -m "Merged upstream"
git push ${PUSH_ARGS} origin ${DOWNSTREAM_BRANCH} || exit $?
fi

cd ..
rm -rf work

0 comments on commit 4745dc2

Please sign in to comment.