|
1 | 1 | #!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +COMMAND=$1 |
| 5 | + |
| 6 | +## Setup variables for checks |
| 7 | +GITHUB_ORG=$(echo ${REPOSITORY} | cut -d "/" -f 2) |
| 8 | +GITHUB_REPO=$(echo ${REPOSITORY} | cut -d "/" -f 3) |
| 9 | +RANDOM_SEED=${randomSeed} |
| 10 | +REGION=${region} |
2 | 11 |
|
3 | 12 | # In order to set project env-vars, we must encrypt secrets |
4 | 13 | # Using gh client allows us to set the secret without installing another |
5 | 14 | # binary just to encrypt the secrets |
6 | | - |
7 | | -# Login GH client |
8 | | -# GITHUB_ACCESS_TOKEN is injected when zero apply runs |
9 | | -gh auth login --with-token <<EOF |
10 | | -$GITHUB_ACCESS_TOKEN |
| 15 | +setup () { |
| 16 | + # Login GH client |
| 17 | + # GITHUB_ACCESS_TOKEN is injected when zero apply runs |
| 18 | + gh auth login --with-token <<EOF |
| 19 | + $GITHUB_ACCESS_TOKEN |
11 | 20 | EOF |
12 | 21 |
|
13 | | -gh auth status |
14 | | - |
15 | | -AWS_KEY_PAIR=$(aws secretsmanager get-secret-value --region ${REGION} --secret-id=${PROJECT_NAME}-ci-user-aws-keys${RANDOM_SEED}) |
16 | | -AWS_ACCESS_KEY_ID=$(echo ${AWS_KEY_PAIR} | jq -r '.SecretString'| jq -r .access_key_id) |
17 | | -AWS_SECRET_ACCESS_KEY=$(echo ${AWS_KEY_PAIR} | jq -r '.SecretString'| jq -r .secret_key) |
18 | | - |
19 | | -## IMPORTANT: Set secret operates on the nearest .git repo even if you specify a different repository |
20 | | -pushd $PROJECT_DIR && \ |
21 | | -gh secret set AWS_ACCESS_KEY_ID --repos="$GITHUB_REPO" --body="$AWS_ACCESS_KEY_ID" && \ |
22 | | -gh secret set AWS_SECRET_ACCESS_KEY --repos="$GITHUB_REPO" --body="$AWS_SECRET_ACCESS_KEY" && \ |
23 | | -popd |
24 | | - |
25 | | -## Branch Protect for PRs |
26 | | -## By default we setup Pull-request checks of [lint, unit-test] in `.github/workflows/pull-request.yml` |
27 | | -## And we will enforce both the checks pass before PR can be merged into default branch |
28 | | -DEFAULT_BRANCH=master |
29 | | -curl -XPUT "https://api.github.com/repos/$GITHUB_ORG/$GITHUB_REPO/branches/$DEFAULT_BRANCH/protection" \ |
30 | | ---header "Authorization: token $GITHUB_ACCESS_TOKEN" \ |
31 | | ---header 'Content-Type: application/json' \ |
32 | | ---data '{ |
33 | | - "required_status_checks": { |
34 | | - "strict": false, |
35 | | - "contexts": ["unit-test"] |
36 | | - }, |
37 | | - "enforce_admins": false, |
38 | | - "required_pull_request_reviews": null, |
39 | | - "restrictions": null |
40 | | -}' |
41 | | - |
42 | | -## Rerun github actions workflow, since the first time github action is ran there are no AWS credentials |
43 | | -## so it will always fail, begining of this script we inject the AWS credentials, therefore now we can rerun the workflow |
44 | | -MOST_RECENT_RUN_ID=$(curl -XGET --url "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/actions/runs" \ |
45 | | ---header "Authorization: token $GITHUB_ACCESS_TOKEN" --header 'Content-Type: application/json' | jq -r ".workflow_runs[0].id") |
46 | | -## Triggering the rerun |
47 | | -curl -XPOST --url "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/actions/runs/${MOST_RECENT_RUN_ID}/rerun" \ |
48 | | ---header "Authorization: token $GITHUB_ACCESS_TOKEN" --header 'Content-Type: application/json' |
49 | | - |
50 | | -echo "Github actions environment variables setup successfully." |
| 22 | + gh auth status |
| 23 | + |
| 24 | + AWS_KEY_PAIR=$(aws secretsmanager get-secret-value --region ${REGION} --secret-id=${PROJECT_NAME}-ci-user-aws-keys${RANDOM_SEED}) |
| 25 | + AWS_ACCESS_KEY_ID=$(echo ${AWS_KEY_PAIR} | jq -r '.SecretString'| jq -r .access_key_id) |
| 26 | + AWS_SECRET_ACCESS_KEY=$(echo ${AWS_KEY_PAIR} | jq -r '.SecretString'| jq -r .secret_key) |
| 27 | + |
| 28 | + ## IMPORTANT: Set secret operates on the nearest .git repo even if you specify a different repository |
| 29 | + pushd $PROJECT_DIR && \ |
| 30 | + gh secret set AWS_ACCESS_KEY_ID --repos="$GITHUB_REPO" --body="$AWS_ACCESS_KEY_ID" && \ |
| 31 | + gh secret set AWS_SECRET_ACCESS_KEY --repos="$GITHUB_REPO" --body="$AWS_SECRET_ACCESS_KEY" && \ |
| 32 | + popd |
| 33 | + |
| 34 | + ## Branch Protect for PRs |
| 35 | + ## By default we setup Pull-request checks of [lint, unit-test] in `.github/workflows/pull-request.yml` |
| 36 | + ## And we will enforce both the checks pass before PR can be merged into default branch |
| 37 | + DEFAULT_BRANCH=master |
| 38 | + curl -XPUT "https://api.github.com/repos/$GITHUB_ORG/$GITHUB_REPO/branches/$DEFAULT_BRANCH/protection" \ |
| 39 | + --header "Authorization: token $GITHUB_ACCESS_TOKEN" \ |
| 40 | + --header 'Content-Type: application/json' \ |
| 41 | + --data '{ |
| 42 | + "required_status_checks": { |
| 43 | + "strict": false, |
| 44 | + "contexts": ["unit-test"] |
| 45 | + }, |
| 46 | + "enforce_admins": false, |
| 47 | + "required_pull_request_reviews": null, |
| 48 | + "restrictions": null |
| 49 | + }' |
| 50 | + |
| 51 | + ## Rerun github actions workflow, since the first time github action is ran there are no AWS credentials |
| 52 | + ## so it will always fail, begining of this script we inject the AWS credentials, therefore now we can rerun the workflow |
| 53 | + MOST_RECENT_RUN_ID=$(curl -XGET --url "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/actions/runs" \ |
| 54 | + --header "Authorization: token $GITHUB_ACCESS_TOKEN" --header 'Content-Type: application/json' | jq -r ".workflow_runs[0].id") |
| 55 | + ## Triggering the rerun |
| 56 | + curl -XPOST --url "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/actions/runs/${MOST_RECENT_RUN_ID}/rerun" \ |
| 57 | + --header "Authorization: token $GITHUB_ACCESS_TOKEN" --header 'Content-Type: application/json' |
| 58 | + |
| 59 | + echo "Github actions environment variables setup successfully." |
| 60 | +} |
| 61 | + |
| 62 | +check () { |
| 63 | + # Check if required binaries are installed on user's environment |
| 64 | + sh scripts/required-bins.sh gh |
| 65 | + # Check github token is able to access this repo |
| 66 | + curl -s -XGET "https://api.github.com/repos/$GITHUB_ORG/${GITHUB_REPO}" \ |
| 67 | + --header "Authorization: token $GITHUB_ACCESS_TOKEN" --header 'Content-Type: application/json' | jq -e ".name == \"${GITHUB_REPO}\"" |
| 68 | +} |
| 69 | + |
| 70 | +echo "Running command $COMMAND" |
| 71 | +$COMMAND |
0 commit comments