|
| 1 | +name: Downport from release to main |
| 2 | +# originally taken from: |
| 3 | +# https://github.tools.sap/kulturpass/kp-commerce/blob/0e9ebcde4c9a8b56b9f7a89f08a401080385367f/.github/workflows/sync-branches.yml |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_dispatch: # manual trigger |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - release |
| 10 | +jobs: |
| 11 | + downport-changes: |
| 12 | + runs-on: ${{ fromJSON(vars.RUNNER_JSON) }} |
| 13 | + steps: |
| 14 | + # as this workflow creates a pull request at the end, we'll first retrieve a token |
| 15 | + # for our service user so that we commit on its behalf |
| 16 | + # see https://github.com/actions/create-github-app-token/blob/93c1f04d6f14ea5b416e7a8dfd80446101c6adef/README.md?plain=1#L92-L120 |
| 17 | + - name: Create GitHub app token |
| 18 | + uses: actions/create-github-app-token@v2 |
| 19 | + id: app-token |
| 20 | + with: |
| 21 | + app-id: ${{ fromJSON(vars.SERVICE_USER_ORG_SCOPED_READ_ACCESS).app-id }} |
| 22 | + private-key: ${{ secrets[fromJSON(vars.SERVICE_USER_ORG_SCOPED_READ_ACCESS).private-key-secret-name] }} |
| 23 | + - name: Get GitHub App User ID |
| 24 | + id: get-user-id |
| 25 | + run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT" |
| 26 | + env: |
| 27 | + GH_HOST: github.tools.sap |
| 28 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 29 | + GH_ENTERPRISE_TOKEN: ${{ steps.app-token.outputs.token }} |
| 30 | + - name: Configure Git |
| 31 | + run: | |
| 32 | + git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]' |
| 33 | + git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.tools.sap' |
| 34 | +
|
| 35 | + - name: Checkout repository |
| 36 | + uses: actions/checkout@v5 |
| 37 | + with: |
| 38 | + fetch-depth: 0 |
| 39 | + |
| 40 | + - name: Fetch remote branches |
| 41 | + run: | |
| 42 | + git fetch origin --no-tags |
| 43 | + git branch --list '*/release*' --remote |
| 44 | +
|
| 45 | + - name: Create branch mapping |
| 46 | + uses: actions/github-script@v7 |
| 47 | + id: branch-mapping |
| 48 | + with: |
| 49 | + script: | |
| 50 | + const branchMapping = { |
| 51 | + 'release': 'main' |
| 52 | + } |
| 53 | + console.log({ branchMapping }) |
| 54 | + return branchMapping |
| 55 | +
|
| 56 | + - name: Sync with next-in-line branch |
| 57 | + run: | |
| 58 | + git fetch origin |
| 59 | + if git show-ref --quiet origin/chore/downport-$GITHUB_REF_NAME |
| 60 | + then |
| 61 | + echo "Using existing branch" |
| 62 | + git checkout -f chore/downport-$GITHUB_REF_NAME |
| 63 | + git pull origin chore/downport-$GITHUB_REF_NAME || true |
| 64 | +
|
| 65 | + echo "Ref of chore/downport-$GITHUB_REF_NAME:" |
| 66 | + git show-ref chore/downport-$GITHUB_REF_NAME |
| 67 | + echo "Ref of origin/$GITHUB_REF_NAME:" |
| 68 | + git show-ref origin/$GITHUB_REF_NAME |
| 69 | +
|
| 70 | + git merge origin/$GITHUB_REF_NAME --no-ff --no-edit --no-verify --verbose |
| 71 | + else |
| 72 | + echo "Creating new branch" |
| 73 | + git branch -f chore/downport-$GITHUB_REF_NAME |
| 74 | + git checkout -f chore/downport-$GITHUB_REF_NAME |
| 75 | + fi |
| 76 | + git push origin chore/downport-$GITHUB_REF_NAME |
| 77 | + gh pr create --base $TARGET --head chore/downport-$GITHUB_REF_NAME --title "Chore: Latest $GITHUB_REF_NAME to $TARGET" --body "" || true |
| 78 | + env: |
| 79 | + TARGET: ${{ fromJSON(steps.branch-mapping.outputs.result)[github.ref_name] }} |
| 80 | + GH_HOST: github.tools.sap |
| 81 | + GH_ENTERPRISE_TOKEN: ${{ steps.app-token.outputs.token }} |
| 82 | + GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} |
0 commit comments