|
| 1 | +name: Setup repository from template |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + visibility: |
| 7 | + description: 'Visibility' |
| 8 | + required: true |
| 9 | + default: 'private' |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - private |
| 13 | + - public |
| 14 | + python: |
| 15 | + description: 'Python/Jupyter Notebook' |
| 16 | + required: true |
| 17 | + default: false |
| 18 | + type: boolean |
| 19 | + javascript: |
| 20 | + description: 'TypeScript/JavaScript' |
| 21 | + required: true |
| 22 | + default: false |
| 23 | + type: boolean |
| 24 | + terraform: |
| 25 | + description: 'Terraform' |
| 26 | + required: true |
| 27 | + default: false |
| 28 | + type: boolean |
| 29 | + golang: |
| 30 | + description: 'Go' |
| 31 | + required: true |
| 32 | + default: false |
| 33 | + type: boolean |
| 34 | + vercel: |
| 35 | + description: 'Vercel' |
| 36 | + required: true |
| 37 | + default: false |
| 38 | + type: boolean |
| 39 | +jobs: |
| 40 | + common-setup: |
| 41 | + name: Common Setup |
| 42 | + outputs: |
| 43 | + run_jobs: ${{ steps.check-template.outputs.run_jobs}} |
| 44 | + runs-on: ubuntu-22.04 |
| 45 | + env: |
| 46 | + REPO_SETUP_TOKEN: ${{ secrets.REPO_SETUP_TOKEN }} |
| 47 | + steps: |
| 48 | + - name: Do not run setup on template repository |
| 49 | + id: check-template |
| 50 | + shell: bash {0} |
| 51 | + # Using the GitHub rest API allows us to identify if the current repository |
| 52 | + # is a template repository or not. |
| 53 | + run: | |
| 54 | + not_template=$(curl --silent -X GET -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github+json" https://api.github.com/repos/$GITHUB_REPOSITORY | jq --exit-status '.is_template == false'); |
| 55 | + echo "run_jobs=$not_template" >> $GITHUB_OUTPUT |
| 56 | + - uses: actions/checkout@v3 |
| 57 | + with: |
| 58 | + # Cannot use the built-in $GITHUB_TOKEN since we need webhook permission |
| 59 | + token: ${{ env.REPO_SETUP_TOKEN }} |
| 60 | + |
| 61 | + ### RESTRICT RUNNABLE GITHUB ACTIONS |
| 62 | + - name: Set runnable actions to 'selected' |
| 63 | + shell: bash |
| 64 | + run: | |
| 65 | + curl -X PUT -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/$GITHUB_REPOSITORY/actions/permissions -d '{"enabled":true,"allowed_actions":"selected"}' |
| 66 | + - name: Restrict runnable actions |
| 67 | + shell: bash |
| 68 | + run: | |
| 69 | + curl -X PUT -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/$GITHUB_REPOSITORY/actions/permissions/selected-actions -d '{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["aws-actions/amazon-ecr-login@v1", "aws-actions/configure-aws-credentials@v2", "trufflesecurity/trufflehog@v3.26.0", "returntocorp/semgrep", "tenable/terrascan-action@main"]}' |
| 70 | +
|
| 71 | + ### PYTHON SETUP |
| 72 | + - name: python-setup |
| 73 | + if: ${{ inputs.python && steps.check-template.outputs.run_jobs == 'true' }} |
| 74 | + shell: bash |
| 75 | + # Copy the bandit action workflow file to the appropriate location |
| 76 | + run: | |
| 77 | + cp template-files/python/bandit-ci.yml .github/workflows/bandit-ci.yml |
| 78 | + cat template-files/python/.gitignore >> .gitignore |
| 79 | +
|
| 80 | + ### JS/TS SETUP |
| 81 | + - name: javascript-setup |
| 82 | + if: ${{ inputs.javascript && steps.check-template.outputs.run_jobs == 'true' }} |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + cat template-files/js/.gitignore >> .gitignore |
| 86 | +
|
| 87 | + ### TERRAFORM SETUP |
| 88 | + - name: terraform-setup |
| 89 | + if: ${{ inputs.terraform && steps.check-template.outputs.run_jobs == 'true' }} |
| 90 | + shell: bash |
| 91 | + run: | |
| 92 | + cat template-files/terraform/.gitignore >> .gitignore |
| 93 | + cp template-files/terraform/atlantis.yaml atlantis.yaml |
| 94 | + cp template-files/terraform/terrascan-ci.yml .github/workflows/terrascan-ci.yml |
| 95 | +
|
| 96 | + ### TEMPLATE FILE |
| 97 | + - name: move-template-file |
| 98 | + if: ${{ github.event.inputs.terraform == 'true' || github.event.inputs.python == 'true' }} |
| 99 | + id: move-output-template |
| 100 | + shell: bash |
| 101 | + # Copy the logging template file to the workflows folder |
| 102 | + run: | |
| 103 | + cp template-files/common/output-template.json .github/workflows/output-template.json |
| 104 | +
|
| 105 | + ### GOLANG SETUP |
| 106 | + - name: golang-setup |
| 107 | + if: ${{ inputs.golang && steps.check-template.outputs.run_jobs == 'true' }} |
| 108 | + shell: bash |
| 109 | + run: | |
| 110 | + cat template-files/go/.gitignore >> .gitignore |
| 111 | +
|
| 112 | + - name: commit-job-changes |
| 113 | + shell: bash {0} |
| 114 | + # Commit the changes we've made for this job since artifacting all of them would be difficult |
| 115 | + run: | |
| 116 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" && \ |
| 117 | + git config --global user.name "github-actions[bot]" && \ |
| 118 | + git add --all |
| 119 | + if ! git diff-index --quiet HEAD; then |
| 120 | + git commit -m 'Repository Setup' |
| 121 | + git push origin main -f |
| 122 | + fi |
| 123 | +
|
| 124 | + cleanup: |
| 125 | + name: Cleanup |
| 126 | + needs: [common-setup] |
| 127 | + if: ${{ needs.common-setup.outputs.run_jobs == 'true' }} |
| 128 | + env: |
| 129 | + REPO_SETUP_TOKEN: ${{ secrets.REPO_SETUP_TOKEN }} |
| 130 | + runs-on: ubuntu-22.04 |
| 131 | + steps: |
| 132 | + - uses: actions/checkout@v3 |
| 133 | + with: |
| 134 | + # Cannot use the built-in $GITHUB_TOKEN since we need webhook permission |
| 135 | + token: ${{ env.REPO_SETUP_TOKEN }} |
| 136 | + # include the ref to the default branch so we get the changes from the previous jobs |
| 137 | + ref: main |
| 138 | + - name: Clean up template files |
| 139 | + shell: bash |
| 140 | + run: | |
| 141 | + rm -rf template-files |
| 142 | + rm -f .github/workflows/setup-repository.yml |
| 143 | + - name: Reinitialize git repository |
| 144 | + shell: bash |
| 145 | + # We use `git checkout --orphan` to create a branch in a git init-like state and get a clean history |
| 146 | + run: | |
| 147 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" && \ |
| 148 | + git config --global user.name "github-actions[bot]" && \ |
| 149 | + git checkout --orphan temp-branch && \ |
| 150 | + git add . && \ |
| 151 | + git commit -m 'Repository Setup' && \ |
| 152 | + git push origin temp-branch:main -f |
| 153 | + - name: Protect main branch |
| 154 | + shell: bash |
| 155 | + run: | |
| 156 | + curl -X PUT -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/$GITHUB_REPOSITORY/branches/main/protection -d '{"required_status_checks":null,"enforce_admins":null,"required_pull_request_reviews":{"dismissal_restrictions":{"users":[],"teams":[],"apps":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":1,"require_last_push_approval":false,"bypass_pull_request_allowances":{"users":[],"teams":["security-eng","platform-eng"]}},"restrictions":null,"required_linear_history":false,"allow_force_pushes":false,"allow_deletions":false,"block_creations":false,"required_conversation_resolution":true,"lock_branch":false,"allow_fork_syncing":false}' |
| 157 | + ### This must be done at the end of the workflow after all changes have been committed due to org-wide restrictions |
| 158 | + ### SEMGREP SETUP USING REPOSITORY RULESETS |
| 159 | + ### DEPRECATED SINCE SEMGREP IS AUTO-CONFIGURED FOR ALL REPOS EXCEPT TERRACODE-* |
| 160 | + # - name: install-semgrep-action |
| 161 | + # if: ${{ github.event.inputs.semgrep == 'true' }} |
| 162 | + # id: install-semgrep-action |
| 163 | + # shell: bash |
| 164 | + # # id 279250 represents semgrep ruleset |
| 165 | + # # ids_cleaned is a necessary step, since bash naturally delimits by newline, which breaks single-line read |
| 166 | + # run: | |
| 167 | + # raw=$(curl -L -X GET \ |
| 168 | + # -H "Accept: application/vnd.github+json" \ |
| 169 | + # -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" \ |
| 170 | + # -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 171 | + # https://api.github.com/orgs/scaleapi/rulesets/279250) |
| 172 | + # ids=$(echo "$raw" | jq '.conditions.repository_id.repository_ids[]?') |
| 173 | + # ids_cleaned=${ids//$'\n'/ } |
| 174 | + # ids_cleaned=${ids_cleaned//$'\r'/ } |
| 175 | + # refs=$(echo "$raw" | jq '.conditions.ref_name') |
| 176 | + # names=$(echo "$raw" | jq '.conditions.repository_name.include') |
| 177 | + # read -a id_array <<< $ids_cleaned |
| 178 | + # echo 'Beginning with '${#id_array[*]}' repositories.' |
| 179 | + # id_array+=(${{ github.repository_id }}) |
| 180 | + # echo 'Now there are '${#id_array[*]}' repositories.' |
| 181 | + # json_ids=$(jq --compact-output --null-input '$ARGS.positional' --args -- "${id_array[@]}" | tr -d "\"") |
| 182 | + # body=$(echo '{"conditions": { "ref_name": '$refs', "repository_id": {"repository_ids": '"${json_ids//\" /}"'}}}') |
| 183 | + # curl -L -X PUT \ |
| 184 | + # -H "Accept: application/vnd.github+json" \ |
| 185 | + # -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" \ |
| 186 | + # -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 187 | + # https://api.github.com/orgs/scaleapi/rulesets/279250 \ |
| 188 | + # -d "$body" |
| 189 | + ### TRUFFLEHOG SETUP USING REPOSITORY RULESETS |
| 190 | + ### DEPRECATED |
| 191 | + # - name: install-trufflehog-action |
| 192 | + # if: ${{ github.event.inputs.trufflehog == 'true' }} |
| 193 | + # id: install-trufflehog-action |
| 194 | + # shell: bash |
| 195 | + # # id 279251 represents trufflehog ruleset |
| 196 | + # # ids_cleaned is a necessary step, since bash naturally delimits by newline, which breaks single-line read |
| 197 | + # run: | |
| 198 | + # raw=$(curl -L -X GET \ |
| 199 | + # -H "Accept: application/vnd.github+json" \ |
| 200 | + # -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" \ |
| 201 | + # -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 202 | + # https://api.github.com/orgs/scaleapi/rulesets/279251) |
| 203 | + # ids=$(echo "$raw" | jq '.conditions.repository_id.repository_ids[]?') |
| 204 | + # ids_cleaned=${ids//$'\n'/ } |
| 205 | + # ids_cleaned=${ids_cleaned//$'\r'/ } |
| 206 | + # refs=$(echo "$raw" | jq '.conditions.ref_name') |
| 207 | + # names=$(echo "$raw" | jq '.conditions.repository_name.include') |
| 208 | + # read -a id_array <<< $ids_cleaned |
| 209 | + # id_array+=(${{ github.repository_id }}) |
| 210 | + # json_ids=$(jq --compact-output --null-input '$ARGS.positional' --args -- "${id_array[@]}" | tr -d "\"") |
| 211 | + # body=$(echo '{"conditions": { "ref_name": '$refs', "repository_id": {"repository_ids": '"${json_ids//\" /}"'}}}') |
| 212 | + # curl -L -X PUT \ |
| 213 | + # -H "Accept: application/vnd.github+json" \ |
| 214 | + # -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" \ |
| 215 | + # -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 216 | + # https://api.github.com/orgs/scaleapi/rulesets/279251 \ |
| 217 | + # -d "$body" |
| 218 | + - name: Remove secret REPO_SETUP_TOKEN |
| 219 | + # After re-initializing the repository, we can remove the `REPO_SETUP_TOKEN` secret since it has permissions we don't want to sit around in the repository |
| 220 | + shell: bash |
| 221 | + if: ${{ env.REPO_SETUP_TOKEN }} |
| 222 | + run: | |
| 223 | + curl \ |
| 224 | + -X DELETE --fail \ |
| 225 | + -H "Accept: application/vnd.github.v3+json" \ |
| 226 | + -H "Authorization: Bearer ${{ env.REPO_SETUP_TOKEN }}" \ |
| 227 | + https://api.github.com/repos/$GITHUB_REPOSITORY/actions/secrets/REPO_SETUP_TOKEN |
0 commit comments