Skip to content

Instantly share code, notes, and snippets.

@iammuho
Last active April 13, 2024 18:51
Show Gist options
  • Save iammuho/a6fe87cfff4ac6cadde30a84c46c4aa4 to your computer and use it in GitHub Desktop.
Save iammuho/a6fe87cfff4ac6cadde30a84c46c4aa4 to your computer and use it in GitHub Desktop.
Github action to update a file with github app
name: Sync File to RepoB
on:
push:
branches:
- main
env:
REPO_OWNER: owner
REPO_NAME: repoB
jobs:
update-file:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}
owner: ${{ env.REPO_OWNER }}
repositories: ${{ env.REPO_NAME }}
- uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
ref: main
repository: ${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}
persist-credentials: false
- name: Commit changes
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
FILE_TO_COMMIT: abc3.txt
DESTINATION_BRANCH: main
run: |
# change the file
echo "Hello, World3!" > $FILE_TO_COMMIT
export TODAY=$( date -u '+%Y-%m-%d' )
export MESSAGE="chore: regenerate $FILE_TO_COMMIT for $TODAY"
export SHA=$( git rev-parse $DESTINATION_BRANCH:$FILE_TO_COMMIT )
export CONTENT=$( base64 -i $FILE_TO_COMMIT )
gh api --method PUT /repos/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}/contents/$FILE_TO_COMMIT \
--field message="$MESSAGE" \
--field content="$CONTENT" \
--field encoding="base64" \
--field branch="$DESTINATION_BRANCH" \
--field sha="$SHA"
name: Sync File to RepoB
on:
push:
branches:
- main
env:
REPO_OWNER: owner
REPO_NAME: repoB
BASE_BRANCH: main
DESTINATION_BRANCH: update-${{ github.run_id }}
jobs:
update-file:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}
owner: ${{ env.REPO_OWNER }}
repositories: ${{ env.REPO_NAME }}
- uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
ref: main
repository: ${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}
persist-credentials: false
- name: Create branch
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
export SHA=$(git rev-parse ${{ env.BASE_BRANCH }})
gh api --method POST /repos/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}/git/refs -f ref='refs/heads/${{ env.DESTINATION_BRANCH }}' -f sha=$SHA
- name: Commit changes using GitHub API
env:
MESSAGE: "chore: regenerate abc3.txt"
GH_TOKEN: ${{ steps.app-token.outputs.token }}
FILE_TO_COMMIT: abc3.txt
run: |
echo "Hello, World3!" > $FILE_TO_COMMIT
export TODAY=$( date -u '+%Y-%m-%d' )
export SHA=$( git rev-parse $BASE_BRANCH:$FILE_TO_COMMIT )
export CONTENT=$( base64 -i $FILE_TO_COMMIT )
gh api --method PUT /repos/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}/contents/$FILE_TO_COMMIT \
--field message="$MESSAGE" \
--field content="$CONTENT" \
--field encoding="base64" \
--field branch="$DESTINATION_BRANCH" \
--field sha="$SHA"
- name: Create PR
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PR_TITLE: "chore: regenerate abc3.txt"
PR_BODY: "This PR is auto-generated by a GitHub Action"
run: |
gh pr create --base $BASE_BRANCH --head $DESTINATION_BRANCH --title "$PR_TITLE" --body "$PR_BODY" --repo ${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment