3 minute read

GitHub actions run bash script and push to another repository

github_action

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:

https://github.com/iven86/test01 <- Private repo, Where my own scripts there.
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

name: Push commit
on:
  push:
  schedule:
        - cron: '0 0 * * *' # run every day at midd night
jobs:
  report:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/[email protected]
    - 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

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/[email protected]

    - name: Pushes test file
      uses: <Your GitHub user>/[email protected]
      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:

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:

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:

#!/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