GitHub actions run bash script and push to another repository
GitHub actions run bash script and push to another repository
Today I going to share with you the idea of how to do automation to run a script for reporting or monitoring your system, cloud, etc…
And I going to use cron job to schedule the task as I used here [cron: ‘0 0 * * *’] to run our script every day at midnight.
I have here two repositories as below shown:
1
https://github.com/iven86/test01 <- Private repo, Where my own scripts there.
1
https://github.com/iven86/test02 <- Public repo, Where I want to share the results and it's could be private too.
At first, You have to create a new token key and add it at secrets under the name API_TOKEN_GITHUB. Then we are going to move to the next step.
Create actions:
Go to your actions tab and on workflows create two as below:
A - main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
name: Push commit
on:
push:
schedule:
- cron: '0 0 * * *' # run every day at midd night
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run script file
run: |
chmod +x ./build.sh
./build.sh
shell: bash
- name: Commit report
run: |
git config --global user.name '<Your GitHub user>'
git config --global user.email '<Your GitHub email>'
git commit -am "<Your commit MSG's>"
git push
B - push.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
name: Push File
on:
push:
schedule:
- cron: '10 0 * * *' # run every day at midd night
jobs:
copy-file:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Pushes test file
uses: <Your GitHub user>/test01@master
env:
API_TOKEN_GITHUB: $
with:
source_file: 'FireFi.txt'
destination_repo: '<Your GitHub user>/test02'
destination_folder: '<Your next destination folder on the next repository>'
user-email: '<Your GitHub email>'
user_name: '<Your GitHub user>'
commit_message: '<Your commit MSG's>'
Create an action.yml file inside your repository:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
name: 'Push a file to another repository'
description: 'Used for pushing a copy of a file to another repository'
inputs:
source_file:
description: 'Source file from the origin directory'
required: true
destination_repo:
description: 'Destination repository'
required: true
destination_folder:
description: 'Directory to push the file to'
required: false
user_email:
description: 'Email for the git commit'
required: true
user_name:
description: 'GitHub username for the commit'
required: true
destination_branch:
description: 'branch to push file to, defaults to master'
required: false
destination_branch_create:
description: 'Destination branch to create for this commit'
required: false
commit_message:
description: 'A custom message for the commmit'
required: false
runs:
using: 'docker'
image: 'Dockerfile'
args:
- $
- $
- $
- $
- $
- $
- $
branding:
icon: 'git-commit'
color: 'green'
Create a Dockerfile inside your repository:
1
2
3
4
5
6
7
8
9
10
11
FROM alpine
RUN apk update && \
apk upgrade && \
apk add git
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
Create an entry point script (entrypoint.sh) inside your repository:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/sh
set -e
set -x
if [ -z "$INPUT_SOURCE_FILE" ]
then
echo "Source file must be defined"
return -1
fi
if [ -z "$INPUT_DESTINATION_BRANCH" ]
then
INPUT_DESTINATION_BRANCH=master
fi
OUTPUT_BRANCH="$INPUT_DESTINATION_BRANCH"
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 --single-branch --branch $INPUT_DESTINATION_BRANCH "https://x-access-token:[email protected]/$INPUT_DESTINATION_REPO.git" "$CLONE_DIR"
echo "Copying contents to git repo"
mkdir -p $CLONE_DIR/$INPUT_DESTINATION_FOLDER
cp -R "$INPUT_SOURCE_FILE" "$CLONE_DIR/$INPUT_DESTINATION_FOLDER"
cd "$CLONE_DIR"
if [ ! -z "$INPUT_DESTINATION_BRANCH_CREATE" ]
then
git checkout -b "$INPUT_DESTINATION_BRANCH_CREATE"
OUTPUT_BRANCH="$INPUT_DESTINATION_BRANCH_CREATE"
fi
if [ -z "$INPUT_COMMIT_MESSAGE" ]
then
INPUT_COMMIT_MESSAGE="Update from https://github.com/${GITHUB_REPOSITORY}/commit/${GITHUB_SHA}"
fi
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
else
echo "No changes detected"
fi
At the end create your own script under the name (build.sh) or you can change it whatever you want.
That’s it, Hope this short article help you
Sources:
https://github.com/marketplace/actions/push-directory-to-another-repository
Hi there 👋 Support me!
Life is an echo—what you send out comes back.