|
| 1 | +# **What it does**: Synchronizes each of the github/docs-staging-X repositories with the latest build scripts, workflows, and other files from src/deployments/staging. |
| 2 | +# **Why we have it**: We want to centralize build config in src/deployments/staging for use across multiple repos. |
| 3 | +# **Who does it impact**: Docs engineering, and potentially content writers. |
| 4 | + |
| 5 | +name: Sync Staging Repo Files |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: [main] |
| 10 | + paths: |
| 11 | + - 'src/deployments/staging/build-scripts/*.sh' |
| 12 | + - 'src/deployments/staging/.github/**' |
| 13 | + - 'src/deployments/staging/Dockerfile' |
| 14 | + - 'src/deployments/staging/.env.example' |
| 15 | + - 'src/deployments/staging/README.example.md' |
| 16 | + - 'src/deployments/staging/config/**' |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + # Determine how many staging repos we have and generate a matrix with repo and index |
| 23 | + generate-matrix: |
| 24 | + if: github.repository == 'github/docs-internal' |
| 25 | + runs-on: ubuntu-latest |
| 26 | + outputs: |
| 27 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 28 | + steps: |
| 29 | + - name: Checkout source repository |
| 30 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 31 | + with: |
| 32 | + fetch-depth: 1 # Only need latest commit for config.json |
| 33 | + |
| 34 | + - name: Read configuration |
| 35 | + id: read-config |
| 36 | + run: | |
| 37 | + sudo apt-get update && sudo apt-get install -y jq |
| 38 | + NUMBER_OF_REPOS=$(jq '.number_of_staging_repos' src/deployments/staging/config.json) |
| 39 | + if ! [[ "$NUMBER_OF_REPOS" =~ ^[0-9]+$ ]]; then |
| 40 | + echo "Invalid number_of_staging_repos in config.json: $NUMBER_OF_REPOS" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + echo "number_of_repos=$NUMBER_OF_REPOS" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + - name: Generate repository list with indices |
| 46 | + id: generate-repos |
| 47 | + run: | |
| 48 | + NUMBER_OF_REPOS=${{ steps.read-config.outputs.number_of_repos }} |
| 49 | + repos=() |
| 50 | + for i in $(seq 0 $NUMBER_OF_REPOS); do |
| 51 | + repos+=("{\"repo\": \"github/docs-staging-$i\", \"index\": $i}") |
| 52 | + done |
| 53 | + json_repos=$(printf '%s\n' "${repos[@]}" | jq -s .) |
| 54 | + echo "repos=$json_repos" >> $GITHUB_OUTPUT |
| 55 | +
|
| 56 | + - name: Set matrix output with repo and index |
| 57 | + id: set-matrix |
| 58 | + run: | |
| 59 | + repos=${{ steps.generate-repos.outputs.repos }} |
| 60 | + echo "matrix={\"include\": $repos}" >> $GITHUB_OUTPUT |
| 61 | +
|
| 62 | + - uses: ./.github/actions/slack-alert |
| 63 | + if: ${{ failure() && github.event_name != 'workflow_dispatch' }} |
| 64 | + with: |
| 65 | + slack_channel_id: ${{ secrets.DOCS_ALERTS_SLACK_CHANNEL_ID }} |
| 66 | + slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} |
| 67 | + |
| 68 | + sync: |
| 69 | + if: github.repository == 'github/docs-internal' |
| 70 | + needs: generate-matrix |
| 71 | + runs-on: ubuntu-latest |
| 72 | + strategy: |
| 73 | + fail-fast: false |
| 74 | + matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }} |
| 75 | + steps: |
| 76 | + - name: Checkout source repository |
| 77 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 78 | + with: |
| 79 | + fetch-depth: 0 |
| 80 | + |
| 81 | + - name: Checkout target repository |
| 82 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 83 | + with: |
| 84 | + repository: ${{ matrix.repo }} |
| 85 | + token: ${{ secrets.DOCS_BOT_PAT_READPUBLICKEY }} |
| 86 | + path: target_repo |
| 87 | + fetch-depth: 0 |
| 88 | + |
| 89 | + - name: Synchronize files to target repo |
| 90 | + run: | |
| 91 | + # Create necessary directories if they DNE |
| 92 | + mkdir -p target_repo/build-scripts |
| 93 | + mkdir -p target_repo/.github/workflows |
| 94 | + mkdir -p target_repo/config |
| 95 | +
|
| 96 | + # Copy build scripts |
| 97 | + cp src/deployments/staging/build-scripts/*.sh target_repo/build-scripts/ || true |
| 98 | +
|
| 99 | + # Copy .github directory |
| 100 | + cp -r src/deployments/staging/.github target_repo/ |
| 101 | +
|
| 102 | + # Copy config files |
| 103 | + cp -r src/deployments/staging/config/* target_repo/config/ || true |
| 104 | +
|
| 105 | + # Overwrite Dockerfile |
| 106 | + cp src/deployments/staging/Dockerfile target_repo/Dockerfile |
| 107 | +
|
| 108 | + # Conditional copy for .env if not present |
| 109 | + if [ ! -f target_repo/.env ]; then |
| 110 | + cp src/deployments/staging/.env.example target_repo/.env |
| 111 | + fi |
| 112 | +
|
| 113 | + # Conditional copy for README.md if not present |
| 114 | + if [ ! -f target_repo/README.md ]; then |
| 115 | + cp src/deployments/staging/README.example.md target_repo/README.md |
| 116 | + fi |
| 117 | +
|
| 118 | + - name: Install jq |
| 119 | + run: sudo apt-get update && sudo apt-get install -y jq |
| 120 | + |
| 121 | + - name: Replace template variables |
| 122 | + run: | |
| 123 | + # Determine which values to use based on the index |
| 124 | + INDEX=${{ matrix.index }} |
| 125 | +
|
| 126 | + if [ "$INDEX" -eq 0 ]; then |
| 127 | + DOMAIN=$(jq -r '.server_domain_name.internal' src/deployments/staging/config.json) |
| 128 | + LOADBALANCER=$(jq -r '.load_balancer_type.internal' src/deployments/staging/config.json) |
| 129 | + elif [ "$INDEX" -eq 1 ]; then |
| 130 | + DOMAIN=$(jq -r '.server_domain_name.external' src/deployments/staging/config.json) |
| 131 | + LOADBALANCER=$(jq -r '.load_balancer_type.external' src/deployments/staging/config.json) |
| 132 | + else |
| 133 | + DOMAIN=$(jq -r '.server_domain_name["docs-staging-x"]' src/deployments/staging/config.json) |
| 134 | + LOADBALANCER=$(jq -r '.load_balancer_type.["docs-staging-x"]' src/deployments/staging/config.json) |
| 135 | + |
| 136 | + # Replace {{x}} in the domain variable with the current index |
| 137 | + DOMAIN=$(echo "$DOMAIN" | sed "s/{{x}}/$INDEX/g") |
| 138 | + fi |
| 139 | +
|
| 140 | + # Perform replacements in target_repo files |
| 141 | + # Replace the server_domain_name and load_balancer_type |
| 142 | + find target_repo -type f -exec sed -i "s|{{server_domain_name}}|$DOMAIN|g" {} + |
| 143 | + find target_repo -type f -exec sed -i "s|{{load_balancer_type}}|$LOADBALANCER|g" {} + |
| 144 | +
|
| 145 | + # If any files still contain {{x}}, replace them with the current index |
| 146 | + find target_repo -type f -exec sed -i "s/{{x}}/$INDEX/g" {} + |
| 147 | +
|
| 148 | + - name: Commit and push changes |
| 149 | + run: | |
| 150 | + cd target_repo |
| 151 | + git config user.name "github-actions[bot]" |
| 152 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 153 | + git add . |
| 154 | + # If there are changes, commit and push |
| 155 | + if ! git diff --cached --quiet; then |
| 156 | + git commit -m "Synchronize files from source repository with index ${{ matrix.index }}" |
| 157 | + git push |
| 158 | + fi |
| 159 | +
|
| 160 | + - uses: ./.github/actions/slack-alert |
| 161 | + if: ${{ failure() && github.event_name != 'workflow_dispatch' }} |
| 162 | + with: |
| 163 | + slack_channel_id: ${{ secrets.DOCS_ALERTS_SLACK_CHANNEL_ID }} |
| 164 | + slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} |
0 commit comments