|
| 1 | +# Auto sync to public devkit repo when there is push to main branch |
| 2 | + |
| 3 | +name: 'Auto_Sync' |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_run: |
| 7 | + workflows: [Scan] |
| 8 | + types: |
| 9 | + - completed |
| 10 | + branches: [main] |
| 11 | + |
| 12 | +jobs: |
| 13 | + sync-repo: |
| 14 | + if: ${{ github.event.workflow_run.event == 'push' }} |
| 15 | + runs-on: self-hosted |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout source repository |
| 19 | + |
| 20 | + with: |
| 21 | + fetch-depth: 2 |
| 22 | + |
| 23 | + - name: Sync files to temporary directory |
| 24 | + run: | |
| 25 | + mkdir temp_repo |
| 26 | + rsync -av --exclude='.github' ./* temp_repo/ |
| 27 | + |
| 28 | + - name: Set up Git credential |
| 29 | + run: | |
| 30 | + git config --global user.name 'gooishin' |
| 31 | + git config --global user.email '[email protected]' |
| 32 | +
|
| 33 | + - name: Checkout public repository |
| 34 | + uses: actions/checkout@v4 |
| 35 | + with: |
| 36 | + repository: 'intel/edge-developer-kit-reference-scripts' |
| 37 | + token: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }} |
| 38 | + path: 'public_repo' |
| 39 | + |
| 40 | + - name: Get last commit message |
| 41 | + id: get_commit_message |
| 42 | + run: | |
| 43 | + commit_message=$(git log -1 --pretty=%B | tr -d '\n') |
| 44 | + if [ -z "$commit_message" ]; then |
| 45 | + echo "Commit message is empty or only whitespace." |
| 46 | + else |
| 47 | + echo "message=$commit_message" >> $GITHUB_ENV |
| 48 | + echo "The last commit message is $commit_message" |
| 49 | + fi |
| 50 | + |
| 51 | + - name: Create branch at destination repo and delete the branch contents |
| 52 | + run: | |
| 53 | + cd public_repo |
| 54 | + git checkout -b update-branch |
| 55 | + git branch --set-upstream-to=origin/main |
| 56 | + git rm -r * |
| 57 | + |
| 58 | + - name: Copy changes to public repository |
| 59 | + run: | |
| 60 | + cd public_repo |
| 61 | + git checkout update-branch |
| 62 | + rsync -av --exclude='.git' --exclude='.github' --exclude='temp_repo' ../temp_repo/ . |
| 63 | + git add --all |
| 64 | + git commit -m "${{ env.message }}" || echo "No changes to commit" |
| 65 | + |
| 66 | + - name: Push to destination repository |
| 67 | + run: | |
| 68 | + cd public_repo |
| 69 | + git checkout update-branch |
| 70 | + git add . |
| 71 | + git commit -m "${{ env.message }}" || echo "No changes to commit" |
| 72 | + git push origin update-branch |
| 73 | +
|
| 74 | + - name: Clean up temporary directory |
| 75 | + if: always() |
| 76 | + run: | |
| 77 | + rm -rf temp_repo public_repo |
| 78 | + |
| 79 | + - name: Create pull request |
| 80 | + uses: actions/github-script@v6 |
| 81 | + with: |
| 82 | + github-token: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }} |
| 83 | + script: | |
| 84 | + const { data: pullRequests } = await github.rest.pulls.list({ |
| 85 | + owner: 'intel', |
| 86 | + repo: 'edge-developer-kit-reference-scripts', |
| 87 | + state: 'open', |
| 88 | + head: 'intel:update-branch', |
| 89 | + base: 'main' |
| 90 | + }); |
| 91 | + |
| 92 | + if (pullRequests.length === 0) { |
| 93 | + await github.rest.pulls.create({ |
| 94 | + owner: 'intel', |
| 95 | + repo: 'edge-developer-kit-reference-scripts', |
| 96 | + title: '${{ env.message }}', |
| 97 | + head: 'update-branch', |
| 98 | + base: 'main', |
| 99 | + body: '${{ env.message }}' |
| 100 | + }); |
| 101 | + } else { |
| 102 | + console.log('A pull request already exists. Please go to public devkit repo and merge PR first, then re-run this job.', pullRequests[0].html_url); |
| 103 | + } |
0 commit comments