Skip to content

Commit 2f1e459

Browse files
V1.0 Source Ready
0 parents  commit 2f1e459

File tree

4 files changed

+275
-0
lines changed

4 files changed

+275
-0
lines changed

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM debian:stable-slim
2+
3+
RUN apt-get update && apt-get install -y git
4+
5+
COPY entrypoint.sh /entrypoint.sh
6+
7+
RUN chmod 777 entrypoint.sh
8+
9+
ENTRYPOINT ["/entrypoint.sh"]

README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<p align="center"><img src="https://cdn.svarun.dev/gh/actions-small.png" width="150px"/></p>
2+
3+
# Github Workflow Sync - ***Github Action***
4+
Github Action To Sync Github Action's Workflow Files Across Repositories
5+
6+
## Use Case 🤔 ?
7+
_This Github Action can come in handy when you have lot of projects like i do._
8+
_where in some case certain projects users action workflow which are common across projects._
9+
_Example : [Project 1][project1] & [Project 2][project2] it can be pain to keep all the workflow updated with Github Action's Module's version._
10+
11+
>_Here where this action comes in and reduces your stress 😉 it can update all your repository actions file based on the config provided_
12+
13+
## ⚙️ Configuration
14+
15+
| **Argument** | Description |
16+
| --- | :---: |
17+
| `github_token` | **Required** Token to use to get repos and write secrets. `${{secrets.GITHUB_TOKEN}}` will not work. instead **Personal Access Token Required*** |
18+
| `repositories` | **Required** New line deliminated regex expressions to select repositories. Repositires are limited to those in whcich the token user is an owner or collaborator. |
19+
| `workflow_files` | **Required** New line deliminated regex expressions. workflow files to be copied to provided repositores |
20+
| `dry_run` | Run everything except for nothing will be pushed. |
21+
22+
### Personal Access Token Scope
23+
#### [Github Personal Token](https://github.com/settings/tokens/new?description=gh-workflow-sync) <small> Is required with the below scope </small>
24+
25+
![https://cdn.svarun.dev/gh/varunsridharan/action-github-workflow-sync/scope.jpg](https://cdn.svarun.dev/gh/varunsridharan/action-github-workflow-sync/scope.jpg)
26+
27+
> ℹ️ Full ***Repo*** is only required when you need to update private repository
28+
> if your are updating only public repository then just select `public_repo` inside ***repo*** scope
29+
30+
***[Click Here To Generate A Token](https://github.com/settings/tokens/new?description=gh-workflow-sync)***
31+
32+
### `workflow_files`
33+
**Example**
34+
```yaml
35+
WORKFLOW_FILES: |
36+
your-workflow-file1.yml
37+
your-workflow-file2.yml
38+
```
39+
40+
**Example | Custom File Location**
41+
```yaml
42+
WORKFLOW_FILES: |
43+
./your-folder/your-workflow-file1.yml
44+
./your-folder/your-workflow-file2.yml
45+
```
46+
47+
**Example | Custom File Name**
48+
Action will locate `your-workflow-file1.yml` from in your repository where this action is used & `your-custom-workflow-file.yml` will be the new file name which will be used to store in the repository you provided
49+
```yaml
50+
WORKFLOW_FILES: |
51+
./your-folder/your-workflow-file1.yml=your-custom-workflow-file.yml
52+
```
53+
54+
---
55+
56+
## 🚀 Usage
57+
58+
### Step 1
59+
Create a [New Repository](https://github.com/new) or use our [Repository Template](https://github.com/varunsridharan/template-github-workflow-sync)
60+
61+
### Step 2
62+
if you have used our template repository then edit the file inside `.github/workflows/workflow-sync.yml`
63+
64+
OR
65+
66+
Create a new file in `.github/workflows/` named ***workflow-sync.yml** and copy & paste the below file content
67+
68+
#### `workflow-sync.yml` content
69+
```yaml
70+
name: Workflow Sync
71+
72+
on:
73+
push:
74+
branches:
75+
- master
76+
env:
77+
DRY_RUN: false
78+
REPOSITORIES: |
79+
80+
WORKFLOW_FILES: |
81+
82+
jobs:
83+
Github_Workflow_Sync:
84+
runs-on: ubuntu-latest
85+
steps:
86+
- name: Fetching Local Repository
87+
uses: actions/checkout@master
88+
- name: Running Workflow Sync
89+
uses: varunsridharan/action-github-workflow-sync@master
90+
with:
91+
DRY_RUN: ${{ env.DRY_RUN }}
92+
REPOSITORIES: ${{ env.REPOSITORIES }}
93+
WORKFLOW_FILES: ${{ env.WORKFLOW_FILES }}
94+
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
95+
96+
```
97+
---
98+
99+
## 🤝 Contributing
100+
If you would like to help, please take a look at the list of [issues](issues/).
101+
102+
## 💰 Sponsor
103+
[I][twitter] fell in love with open-source in 2013 and there has been no looking back since! You can read more about me [here][website].
104+
If you, or your company, use any of my projects or like what I’m doing, kindly consider backing me. I'm in this for the long run.
105+
106+
- ☕ How about we get to know each other over coffee? Buy me a cup for just [**$9.99**][buymeacoffee]
107+
- ☕️☕️ How about buying me just 2 cups of coffee each month? You can do that for as little as [**$9.99**][buymeacoffee]
108+
- 🔰 We love bettering open-source projects. Support 1-hour of open-source maintenance for [**$24.99 one-time?**][paypal]
109+
- 🚀 Love open-source tools? Me too! How about supporting one hour of open-source development for just [**$49.99 one-time ?**][paypal]
110+
111+
## 📝 License & Conduct
112+
- [**General Public License v3.0 license**](LICENSE) © [Varun Sridharan](website)
113+
- [Code of Conduct](code-of-conduct.md)
114+
115+
## 📣 Feedback
116+
- ⭐ This repository if this project helped you! :wink:
117+
- Create An [🔧 Issue](issues/) if you need help / found a bug
118+
119+
## Connect & Say 👋
120+
- **Follow** me on [👨‍💻 Github][github] and stay updated on free and open-source software
121+
- **Follow** me on [🐦 Twitter][twitter] to get updates on my latest open source projects
122+
- **Message** me on [📠 Telegram][telegram]
123+
- **Follow** my pet on [Instagram][sofythelabrador] for some _dog-tastic_ updates!
124+
125+
---
126+
127+
<p align="center">
128+
<i>Built With ♥ By <a href="https://go.svarun.dev/twitter" target="_blank" rel="noopener noreferrer">Varun Sridharan</a> 🇮🇳 </i>
129+
</p>
130+
131+
---
132+
133+
<!-- Personl Links -->
134+
[project1]: https://github.com/varunsridharan/wc-product-subtitle/blob/master/.github/workflows/push-to-master.yml
135+
[project2]: https://github.com/varunsridharan/sku-shortlink-for-woocommerce/blob/master/.github/workflows/push-to-master.yml
136+
[paypal]: https://go.svarun.dev/paypal
137+
[buymeacoffee]: https://go.svarun.dev/buymeacoffee
138+
[sofythelabrador]: https://www.instagram.com/sofythelabrador/
139+
[github]: https://go.svarun.dev/github/
140+
[twitter]: https://go.svarun.dev/twitter/
141+
[telegram]: https://go.svarun.dev/telegram/
142+
[email]: https://go.svarun.dev/contact/email/
143+
[website]: https://go.svarun.dev/website/

action.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: 'Github Workflow Sync'
2+
description: 'Github Action To Sync Github Actions Workflow Files Across Repositories'
3+
author: 'varunsridharan'
4+
branding:
5+
icon: 'upload-cloud'
6+
color: 'blue'
7+
inputs:
8+
REPOSITORIES:
9+
description: 'Github Repos To Sync To'
10+
default: ''
11+
required: true
12+
WORKFLOW_FILES:
13+
description: 'Local Workflow Files To Sync'
14+
default: ''
15+
required: true
16+
GITHUB_TOKEN:
17+
description: "Token to use to get repos and write secrets"
18+
required: true
19+
DRY_RUN:
20+
description: "Run everything except for nothing will be Updated."
21+
required: false
22+
default: 'false'
23+
24+
runs:
25+
using: 'docker'
26+
image: 'Dockerfile'
27+
args:
28+
- ${{ inputs.REPOSITORIES }}
29+
- ${{ inputs.WORKFLOW_FILES }}
30+
- ${{ inputs.GITHUB_TOKEN }}
31+
- ${{ inputs.DRY_RUN }}

entrypoint.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
RAW_REPOSITORIES="$INPUT_REPOSITORIES"
5+
RAW_WORKFLOW_FILES="$INPUT_WORKFLOW_FILES"
6+
GITHUB_TOKEN="$INPUT_GITHUB_TOKEN"
7+
DRY_RUN="$INPUT_DRY_RUN"
8+
REPOSITORIES=($RAW_REPOSITORIES)
9+
WORKFLOW_FILES=($RAW_WORKFLOW_FILES)
10+
TEMP_PATH="/github_workflow_sync/"
11+
cd /
12+
echo " "
13+
echo "⚙️ Basic Setup"
14+
echo "DRY_RUN: $DRY_RUN"
15+
git config --global user.email "[email protected]" && git config --global user.name "GH Actions Workflow Sync Bot"
16+
mkdir "$TEMP_PATH"
17+
cd "$TEMP_PATH"
18+
echo " "
19+
20+
# Loops All Provided Repos
21+
for R in "${REPOSITORIES[@]}"; do
22+
echo "###[group] 📓 $R"
23+
24+
REPO_URL="https://x-access-token:${GITHUB_TOKEN}@github.com/${R}.git"
25+
GIT_PATH="${TEMP_PATH}${R}"
26+
LOCAL_PATH="${GIT_PATH}/.github/workflows/"
27+
DEST_STATUS="Updated"
28+
echo "Git URL : $REPO_URL"
29+
git clone $REPO_URL ${R}
30+
echo " "
31+
32+
if [ ! -d "$LOCAL_PATH" ]; then
33+
mkdir -p $LOCAL_PATH
34+
DEST_STATUS="Created"
35+
else
36+
DEST_STATUS="Updated"
37+
fi
38+
39+
cd $GIT_PATH
40+
# Loops All Provided Workflows
41+
for WOF in "${WORKFLOW_FILES[@]}"; do
42+
WORKFLOW_FILE=($(echo $WOF | tr "=" "\n"))
43+
SRC_FILE=${WORKFLOW_FILE[0]}
44+
DEST_FILE=""
45+
SRC_FULL_PATH=""
46+
SRC_FILE_NAME=$(basename "$SRC_FILE")
47+
SRC_FILE_EXT="${SRC_FILE_NAME##*.}"
48+
49+
if [ "$SRC_FILE_EXT" != "yml" ]; then
50+
echo "⚠️ ${SRC_FILE} Is Not A Valid Github Actions Workflow File"
51+
else
52+
if [ ${WORKFLOW_FILE[1]+yes} ]; then
53+
DEST_FILE="${WORKFLOW_FILE[1]}"
54+
fi
55+
56+
if [ -f "$GITHUB_WORKSPACE/.github/workflows/$SRC_FILE" ]; then
57+
SRC_FULL_PATH="${GITHUB_WORKSPACE}/.github/workflows/${SRC_FILE}"
58+
elif [ -f "${GITHUB_WORKSPACE}/${SRC_FILE}" ]; then
59+
SRC_FULL_PATH="${GITHUB_WORKSPACE}/${SRC_FILE}"
60+
fi
61+
62+
if [ "$SRC_FULL_PATH" != "" ]; then
63+
cp "$SRC_FULL_PATH" "${LOCAL_PATH}${DEST_FILE}"
64+
65+
if [ "$(git status --porcelain)" != "" ]; then
66+
git add $LOCAL_PATH -f
67+
git commit -m "💬 #${GITHUB_RUN_NUMBER} - Workflow File ${DEST_STATUS} / ⚡ Triggered By ${GITHUB_REPOSITORY}@${GITHUB_SHA}"
68+
else
69+
echo "✅ Nothing Changed For Workflow : ${SRC_FILE}"
70+
fi
71+
else
72+
echo "🛑 ${SRC_FILE} Not Found In ${GITHUB_REPOSITORY}
73+
Searched In Below Locations
74+
1. $GITHUB_WORKSPACE/.github/workflows/$SRC_FILE
75+
2. ${GITHUB_WORKSPACE}/${SRC_FILE}
76+
"
77+
fi
78+
fi
79+
80+
echo " "
81+
done
82+
83+
if [ -z "$DRY_RUN" ]; then
84+
echo "⚠️ No changes will be pushed to ${R}"
85+
elif [ "$DRY_RUN" = true ]; then
86+
echo "⚠️ No changes will be pushed to ${R}"
87+
else
88+
git push $REPO_URL
89+
fi
90+
echo "###[endgroup]"
91+
cd $TEMP_PATH
92+
done

0 commit comments

Comments
 (0)