-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_actions.sh
84 lines (73 loc) · 2.38 KB
/
github_actions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
# Retrieve AWS Account ID automatically
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
# Set these variables before running the script
GITHUB_OWNER="YOUR_GITHUB_OWNER"
GITHUB_REPO="YOUR_REPOSITORY"
CODEBUILD_PROJECT_NAME="YOUR_CODEBUILD_PROJECT_NAME"
REGION=$(aws configure get region)
# Verify AWS Account ID was retrieved
if [ -z "$AWS_ACCOUNT_ID" ]; then
echo "Error: Could not retrieve AWS Account ID. Please check your AWS CLI configuration."
exit 1
fi
# Create CodeBuild Policy JSON
cat > codebuild-policy.json << EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"codebuild:StartBuild"
],
"Resource": "arn:aws:codebuild:${REGION}:${AWS_ACCOUNT_ID}:project/${CODEBUILD_PROJECT_NAME}"
}
]
}
EOF
# Create Trust Relationship JSON
cat > trust-relationship.json << EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::${AWS_ACCOUNT_ID}:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:${GITHUB_OWNER}/${GITHUB_REPO}:*"
}
}
}
]
}
EOF
# Default to us-east-1 if no region is set
REGION=${REGION:-us-east-1}
# Create IAM Policy
POLICY_ARN=$(aws iam create-policy \
--policy-name GitHubActionsCodeBuildAccess \
--policy-document file://codebuild-policy.json \
--query 'Policy.Arn' \
--output text)
# Create IAM Role
aws iam create-role \
--role-name GitHubActionsCodeBuildRole \
--assume-role-policy-document file://trust-relationship.json
# Attach Policy to Role
aws iam attach-role-policy \
--role-name GitHubActionsCodeBuildRole \
--policy-arn "$POLICY_ARN"
# Print out the role ARN for use in GitHub Actions
echo "AWS Account ID: ${AWS_ACCOUNT_ID}"
echo "IAM Role ARN: arn:aws:iam::${AWS_ACCOUNT_ID}:role/GitHubActionsCodeBuildRole"
echo "Region: ${REGION}"
# Cleanup temporary files
rm codebuild-policy.json trust-relationship.json