Sync a branch to private repo #1586
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| on: | |
| schedule: | |
| - cron: "0 8 * * *" | |
| workflow_dispatch: | |
| inputs: | |
| branch_to_sync: | |
| description: Branch to sync to the private repository (repo default branch if left empty) | |
| required: false | |
| name: Sync a branch to private repo | |
| permissions: | |
| contents: read | |
| env: | |
| DEFAULT_BRANCH_TO_SYNC: ${{ github.event.repository.default_branch }} | |
| jobs: | |
| sync_public_repo_to_private: | |
| name: Force sync a branch to the private repository with current git history | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| with: | |
| ref: ${{ github.event.inputs.branch_to_sync || env.DEFAULT_BRANCH_TO_SYNC }} | |
| fetch-depth: 0 | |
| - name: Push a branch to the private repository | |
| env: | |
| BRANCH_TO_SYNC: ${{ github.event.inputs.branch_to_sync || env.DEFAULT_BRANCH_TO_SYNC }} | |
| GHT_USER: ${{ vars.GHT_USER }} | |
| GHT_PRIVATE_REPO_TOKEN: ${{ secrets.GHT_PRIVATE_REPO_TOKEN }} | |
| GHT_SPARTACUS_REPO: ${{ vars.GHT_SPARTACUS_REPO }} | |
| run: | | |
| git config --global credential.helper store | |
| printf 'https://%s:%s@github.tools.sap\n' "${GHT_USER}" "${GHT_PRIVATE_REPO_TOKEN}" > ~/.git-credentials | |
| git push -u "https://github.tools.sap/cx-commerce-storefronts/${GHT_SPARTACUS_REPO}.git" "$BRANCH_TO_SYNC" -f | |
| - name: Include remaining pipeline files to the private remote's branch | |
| env: | |
| BRANCH_TO_SYNC: ${{ github.event.inputs.branch_to_sync || env.DEFAULT_BRANCH_TO_SYNC }} | |
| GHT_USER: ${{ vars.GHT_USER }} | |
| GHT_PRIVATE_REPO_TOKEN: ${{ secrets.GHT_PRIVATE_REPO_TOKEN }} | |
| GHT_SPARTACUS_REPO: ${{ vars.GHT_SPARTACUS_REPO }} | |
| GHT_EMAIL: ${{ vars.GHT_EMAIL }} | |
| run: | | |
| PIPELINE_CONFIG_PATH="./.pipeline/config.yml" | |
| AZURE_CONFIG_PATH="./azure-pipelines.yml" | |
| SONAR_PATH="./sonar-project.properties" | |
| RBSC_DESCRIPTORS_PATH="./rbsc-descriptors.yml" | |
| RELEASE_PACKAGES_LIST_GENERATOR="./ci-scripts/release-packages-list-generator.sh" | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Clone repo" | |
| git clone -b "$BRANCH_TO_SYNC" "https://github.tools.sap/cx-commerce-storefronts/${GHT_SPARTACUS_REPO}.git" | |
| cd "$GHT_SPARTACUS_REPO" | |
| git config --global user.email "$GHT_EMAIL" | |
| git config --global user.name "$GHT_USER" | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Get files from other branch" | |
| git checkout origin/sap-pipeline-files -- . | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Configure pipeline" | |
| sed -i "s%productiveBranch:%productiveBranch: '${BRANCH_TO_SYNC}'%gi" $PIPELINE_CONFIG_PATH | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Verify productiveBranch has been updated with the synched branch name" | |
| if grep -Fq "productiveBranch: '${BRANCH_TO_SYNC}'" $PIPELINE_CONFIG_PATH | |
| then | |
| echo "Branch name has successfully been added to the productiveBranch" | |
| else | |
| echo "Error. Branch name has NOT been added to the productiveBranch" | |
| exit 1 | |
| fi | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Configure azure pipeline trigger" | |
| sed -i "s%trigger:%trigger:\n - ${BRANCH_TO_SYNC}%gi" $AZURE_CONFIG_PATH | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Verify trigger has been updated with the synched branch name" | |
| if grep -Pzo "trigger:\n - ${BRANCH_TO_SYNC}" $AZURE_CONFIG_PATH | |
| then | |
| echo "Branch name has successfully been added to the trigger" | |
| else | |
| echo "Error. Branch name has NOT been added to the trigger" | |
| exit 1 | |
| fi | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Configure azure pipeline build packages list" | |
| RELEASE_PACKAGES=$($RELEASE_PACKAGES_LIST_GENERATOR) | |
| FORMATTED_RELEASE_PACKAGES="" | |
| for PACKAGE in $RELEASE_PACKAGES; do | |
| FORMATTED_RELEASE_PACKAGES+=" - ${PACKAGE}\n" | |
| done | |
| sed -i "s%buildPackages:%buildPackages:\n${FORMATTED_RELEASE_PACKAGES}%gi" $AZURE_CONFIG_PATH | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Verify buildPackages parameter has been updated with the generated release packages list" | |
| if grep -Pzo "buildPackages:\n${FORMATTED_RELEASE_PACKAGES}" $AZURE_CONFIG_PATH | |
| then | |
| echo "List of packages to release has successfully been added to the buildPackages parameter" | |
| else | |
| echo "Error. List of packages to release has NOT been added to the buildPackages parameter" | |
| exit 1 | |
| fi | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Generate RBSC descriptors" | |
| ./ci-scripts/update-rbsc-descriptors.sh | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Verify RBSC descriptors file has been created" | |
| if [[ -f "$RBSC_DESCRIPTORS_PATH" ]] && [[ -s "$RBSC_DESCRIPTORS_PATH" ]]; then | |
| echo "RBSC descriptors file has been successfully generated" | |
| echo "Generated $(grep -c "^- url:" $RBSC_DESCRIPTORS_PATH) package entries" | |
| else | |
| echo "Error. RBSC descriptors file was NOT generated or is empty" | |
| exit 1 | |
| fi | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Configure sonar" | |
| sed -i "s%sonar.branch.name=%sonar.branch.name=${BRANCH_TO_SYNC}%gi" $SONAR_PATH | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Verify sonar.branch.name has been updated with the synched branch name" | |
| if grep -Fq "sonar.branch.name=${BRANCH_TO_SYNC}" $SONAR_PATH | |
| then | |
| echo "Branch name has successfully been added to the sonar.branch.name" | |
| else | |
| echo "Error. Branch name has NOT been added to the sonar.branch.name" | |
| exit 1 | |
| fi | |
| echo "---------------------------------------------------------------------------------------------------------------------------" | |
| echo "Include files to synced branch" | |
| git add . | |
| git commit -m "include remaining pipeline files" | |
| git push origin "$BRANCH_TO_SYNC" | |
| - name: Cleanup credentials | |
| if: always() | |
| run: | | |
| rm -f ~/.git-credentials | |
| git config --global --unset credential.helper || true |