Skip to content

Commit 9f03deb

Browse files
committed
[sdlf-cicd] github support as an alternative to codecommit
Replace CodeCommit entirely with GitHub - all SDLF repositories (components, main and team repositories) can now be hosted on GitHub. This does *not* replace CodeBuild and CodePipeline, GitHub Actions are not used. Creating repositories is done through CloudFormation third-party resource types: https://github.com/aws-ia/cloudformation-github-resource-providers/tree/main/GitHub-Repositories-Repository Currently SDLF is quite rigid in terms of setup for GitHub, in part due to limitations of the aforementioned resource types. * Setup a CodeConnection to GitHub. * Populate `/SDLF/GitHub/CodeConnection` in SSM Parameter Store with the ARN of the CodeConnection. * Put the access token in `/SDLF/GitHub/AccessToken` in SSM Parameter Store as a secure string. * Enable the third-party resource type `GitHub::Repositories::Repository` on CloudFormation Registry. Do not forget to configure it. * Use `-f github` when deploying SDLF with `deploy.sh`. Resource type configuration example: ``` { "GitHubAccess": { "AccessToken": "{{resolve:ssm-secure:/cfn/github/accesstoken:1}}" } } ```
1 parent d79ec8e commit 9f03deb

10 files changed

+502
-115
lines changed

deploy.sh

+30-2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ devops_account () {
170170
then
171171
GIT_PLATFORM=CodeCommit
172172
GITLAB=false
173+
GITHUB=false
173174
GLUE_JOB_DEPLOYER=false
174175
LAMBDA_LAYER_BUILDER=false
175176
MONITORING=false
@@ -180,6 +181,12 @@ devops_account () {
180181
GITLAB=true
181182
echo "Optional feature: GitLab"
182183
fi
184+
if printf "%s\0" "${FEATURES[@]}" | grep -Fxqz -- "github"
185+
then
186+
GIT_PLATFORM=GitHub
187+
GITHUB=true
188+
echo "Optional feature: GitHub"
189+
fi
183190
if printf "%s\0" "${FEATURES[@]}" | grep -Fxqz -- "gluejobdeployer"
184191
then
185192
GLUE_JOB_DEPLOYER=true
@@ -204,6 +211,7 @@ devops_account () {
204211
echo "-f not specified, set all features to false by default" >&2
205212
GIT_PLATFORM=CodeCommit
206213
GITLAB=false
214+
GITHUB=false
207215
GLUE_JOB_DEPLOYER=false
208216
LAMBDA_LAYER_BUILDER=false
209217
MONITORING=false
@@ -223,7 +231,6 @@ devops_account () {
223231
--parameter-overrides \
224232
pDomainAccounts="$DOMAIN_ACCOUNTS" \
225233
pGitPlatform="$GIT_PLATFORM" \
226-
pEnableGitlab="$GITLAB" \
227234
pEnableGlueJobDeployer="$GLUE_JOB_DEPLOYER" \
228235
pEnableLambdaLayerBuilder="$LAMBDA_LAYER_BUILDER" \
229236
pEnableMonitoring="$MONITORING" \
@@ -235,7 +242,7 @@ devops_account () {
235242
template_protection "$STACK_NAME" "$REGION" "$DEVOPS_AWS_PROFILE"
236243

237244
ARTIFACTS_BUCKET=$(aws --region "$REGION" --profile "$DEVOPS_AWS_PROFILE" ssm get-parameter --name /SDLF/S3/DevOpsArtifactsBucket --query "Parameter.Value" --output text)
238-
REPOSITORIES_TEMPLATE_FILE=$(test "$GITLAB" = true && echo "$DIRNAME"/sdlf-cicd/template-cicd-sdlf-repositories.gitlab.yaml || echo "$DIRNAME"/sdlf-cicd/template-cicd-sdlf-repositories.yaml)
245+
REPOSITORIES_TEMPLATE_FILE="$DIRNAME/sdlf-cicd/template-cicd-sdlf-repositories.${GIT_PLATFORM,,}.yaml"
239246
mkdir "$DIRNAME"/output
240247
aws cloudformation package \
241248
--s3-bucket "$ARTIFACTS_BUCKET" --s3-prefix template-cicd-sdlf-repositories \
@@ -287,6 +294,27 @@ devops_account () {
287294
git push origin main:test
288295
fi
289296
popd || exit
297+
elif "$GITHUB"
298+
GITHUB_ACCESSTOKEN=$(aws --region "$REGION" --profile "$DEVOPS_AWS_PROFILE" ssm get-parameter --with-decryption --name /SDLF/GitHub/AccessToken --query "Parameter.Value" --output text)
299+
GITHUB_REPOSITORY_URL="https://github.com/$REPOSITORY.git"
300+
301+
if [ "$REPOSITORY" = "sdlf-main" ]
302+
then
303+
mkdir sdlf-main
304+
cp sdlf-cicd/README.md sdlf-main/
305+
fi
306+
pushd "$REPOSITORY" || exit
307+
if [ ! -d .git ] # if .git exists, deploy.sh has likely been run before - do not try to push the base repositories
308+
then
309+
git init
310+
git remote add origin "$GITHUB_REPOSITORY_URL" || exit 1
311+
git add .
312+
git commit -m "initial commit"
313+
git push origin main || exit 1
314+
git push origin main:dev
315+
git push origin main:test
316+
fi
317+
popd || exit
290318
else
291319
latest_commit=$(aws --region "$REGION" --profile "$DEVOPS_AWS_PROFILE" codecommit get-branch --repository-name "$REPOSITORY" --branch-name main --query "branch.commitId" --output text)
292320
aws --region "$REGION" --profile "$DEVOPS_AWS_PROFILE" codecommit create-branch --repository-name "$REPOSITORY" --branch-name dev --commit-id "$latest_commit"

sdlf-cicd/nested-stacks/template-cicd-modules-pipelines.yaml

+18-18
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Parameters:
2929
pGitPlatform:
3030
Description: Platform used to host git repositories
3131
Type: String
32-
AllowedValues: [CodeCommit, GitLab]
32+
AllowedValues: [CodeCommit, GitLab, GitHub]
3333

3434
Mappings:
3535
pCodeCommitBranch:
@@ -41,8 +41,8 @@ Mappings:
4141
branch: main
4242

4343
Conditions:
44-
CodeCommitNoGitLab: !Equals [!Ref pGitPlatform, "CodeCommit"]
45-
GitLabNoCodeCommit: !Equals [!Ref pGitPlatform, "GitLab"]
44+
GitPlatformCodeCommit: !Equals [!Ref pGitPlatform, "CodeCommit"]
45+
GitPlatformNoCodeCommit: !Not [!Equals [!Ref pGitPlatform, "CodeCommit"]]
4646

4747
Resources:
4848
rMainRepositoryCodePipelineRole:
@@ -60,10 +60,10 @@ Resources:
6060
Policies:
6161
- PolicyName: root
6262
PolicyDocument:
63-
Version: 2012-10-17
63+
Version: "2012-10-17"
6464
Statement:
6565
- !If
66-
- CodeCommitNoGitLab
66+
- GitPlatformCodeCommit
6767
- Effect: Allow
6868
Action:
6969
- codecommit:GetBranch
@@ -78,7 +78,7 @@ Resources:
7878
- !Sub arn:${AWS::Partition}:codecommit:${AWS::Region}:${AWS::AccountId}:${pCicdRepository}
7979
- !Ref "AWS::NoValue"
8080
- !If
81-
- GitLabNoCodeCommit
81+
- GitPlatformNoCodeCommit
8282
- Effect: Allow
8383
Action:
8484
- codeconnections:UseConnection
@@ -88,11 +88,11 @@ Resources:
8888
Condition:
8989
"ForAllValues:StringLikeIfExists":
9090
"codeconnections:FullRepositoryId":
91-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pMainRepository}"
92-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pCicdRepository}"
91+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pMainRepository}"
92+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pCicdRepository}"
9393
"codestar-connections:FullRepositoryId":
94-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pMainRepository}"
95-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pCicdRepository}"
94+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pMainRepository}"
95+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pCicdRepository}"
9696
- !Ref "AWS::NoValue"
9797
- Effect: Allow
9898
Action:
@@ -112,7 +112,7 @@ Resources:
112112
Resource: !Ref pKMSKey
113113
- PolicyName: lambda-stages
114114
PolicyDocument:
115-
Version: 2012-10-17
115+
Version: "2012-10-17"
116116
Statement:
117117
- Effect: Allow
118118
Action:
@@ -135,7 +135,7 @@ Resources:
135135
Stages:
136136
- Name: Sources
137137
Actions: !If
138-
- CodeCommitNoGitLab
138+
- GitPlatformCodeCommit
139139
- - Name: SourceMain
140140
ActionTypeId:
141141
Category: Source
@@ -172,7 +172,7 @@ Resources:
172172
- Name: SourceMainArtifact
173173
Configuration:
174174
ConnectionArn: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/CodeConnection}}"
175-
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pMainRepository}"
175+
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pMainRepository}"
176176
BranchName: !FindInMap [pCodeCommitBranch, !Ref pEnvironment, branch]
177177
OutputArtifactFormat: CODE_ZIP
178178
RunOrder: 1
@@ -186,7 +186,7 @@ Resources:
186186
- Name: SourceCicdArtifact
187187
Configuration:
188188
ConnectionArn: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/CodeConnection}}"
189-
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pCicdRepository}"
189+
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pCicdRepository}"
190190
BranchName: !FindInMap [pCodeCommitBranch, !Ref pEnvironment, branch]
191191
OutputArtifactFormat: CODE_ZIP
192192
RunOrder: 1
@@ -261,10 +261,10 @@ Resources:
261261

262262
rMainRepositoryCodeCommitTriggerRole:
263263
Type: AWS::IAM::Role
264-
Condition: CodeCommitNoGitLab
264+
Condition: GitPlatformCodeCommit
265265
Properties:
266266
AssumeRolePolicyDocument:
267-
Version: 2012-10-17
267+
Version: "2012-10-17"
268268
Statement:
269269
- Effect: Allow
270270
Principal:
@@ -274,7 +274,7 @@ Resources:
274274
Policies:
275275
- PolicyName: sdlf-cicd-events-trigger
276276
PolicyDocument:
277-
Version: 2012-10-17
277+
Version: "2012-10-17"
278278
Statement:
279279
- Effect: Allow
280280
Action: codepipeline:StartPipelineExecution
@@ -283,7 +283,7 @@ Resources:
283283

284284
rMainRepositoryCodePipelineTriggerRule:
285285
Type: AWS::Events::Rule
286-
Condition: CodeCommitNoGitLab
286+
Condition: GitPlatformCodeCommit
287287
Properties:
288288
EventPattern:
289289
source:

sdlf-cicd/template-cicd-domain.yaml

+20-20
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ Mappings:
7070
branch: main
7171

7272
Conditions:
73-
CodeCommitNoGitLab: !Equals [!Ref pGitPlatform, "CodeCommit"]
74-
GitLabNoCodeCommit: !Equals [!Ref pGitPlatform, "GitLab"]
73+
GitPlatformCodeCommit: !Equals [!Ref pGitPlatform, "CodeCommit"]
74+
GitPlatformNoCodeCommit: !Not [!Equals [!Ref pGitPlatform, "CodeCommit"]]
7575
EnableMonitoring: !Equals [!Ref pEnableMonitoring, true]
7676

7777
Resources:
@@ -94,7 +94,7 @@ Resources:
9494
Version: "2012-10-17"
9595
Statement:
9696
- !If
97-
- CodeCommitNoGitLab
97+
- GitPlatformCodeCommit
9898
- Effect: Allow
9999
Action:
100100
- codecommit:GetBranch
@@ -115,7 +115,7 @@ Resources:
115115
- !Ref AWS::NoValue
116116
- !Ref "AWS::NoValue"
117117
- !If
118-
- GitLabNoCodeCommit
118+
- GitPlatformNoCodeCommit
119119
- Effect: Allow
120120
Action:
121121
- codeconnections:UseConnection
@@ -125,22 +125,22 @@ Resources:
125125
Condition:
126126
"ForAllValues:StringLikeIfExists":
127127
"codeconnections:FullRepositoryId":
128-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pCicdRepository}"
129-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pFoundationsRepository}"
130-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pTeamRepository}"
131-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pMainRepository}"
128+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pCicdRepository}"
129+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pFoundationsRepository}"
130+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pTeamRepository}"
131+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pMainRepository}"
132132
- !If
133133
- EnableMonitoring
134-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/{{resolve:ssm:/SDLF/${pGitPlatform}/Monitoring${pGitPlatform}}}"
134+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/{{resolve:ssm:/SDLF/${pGitPlatform}/Monitoring${pGitPlatform}}}"
135135
- !Ref AWS::NoValue
136136
"codestar-connections:FullRepositoryId":
137-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pCicdRepository}"
138-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pFoundationsRepository}"
139-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pTeamRepository}"
140-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pMainRepository}"
137+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pCicdRepository}"
138+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pFoundationsRepository}"
139+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pTeamRepository}"
140+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pMainRepository}"
141141
- !If
142142
- EnableMonitoring
143-
- !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/{{resolve:ssm:/SDLF/${pGitPlatform}/Monitoring${pGitPlatform}}}"
143+
- !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/{{resolve:ssm:/SDLF/${pGitPlatform}/Monitoring${pGitPlatform}}}"
144144
- !Ref AWS::NoValue
145145
- !Ref "AWS::NoValue"
146146
- Effect: Allow
@@ -182,7 +182,7 @@ Resources:
182182
Stages:
183183
- Name: Sources
184184
Actions: !If
185-
- CodeCommitNoGitLab
185+
- GitPlatformCodeCommit
186186
- - Name: sdlf-main
187187
ActionTypeId:
188188
Category: Source
@@ -264,7 +264,7 @@ Resources:
264264
- Name: TemplateSource
265265
Configuration:
266266
ConnectionArn: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/CodeConnection}}"
267-
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pMainRepository}"
267+
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pMainRepository}"
268268
BranchName: !FindInMap [pCodeCommitBranch, !Ref pEnvironment, branch]
269269
OutputArtifactFormat: CODE_ZIP
270270
RunOrder: 1
@@ -278,7 +278,7 @@ Resources:
278278
- Name: SourceCicdArtifact
279279
Configuration:
280280
ConnectionArn: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/CodeConnection}}"
281-
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pCicdRepository}"
281+
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pCicdRepository}"
282282
BranchName: !FindInMap [pCodeCommitBranch, !Ref pEnvironment, branch]
283283
OutputArtifactFormat: CODE_ZIP
284284
RunOrder: 1
@@ -292,7 +292,7 @@ Resources:
292292
- Name: SourceFoundationsArtifact
293293
Configuration:
294294
ConnectionArn: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/CodeConnection}}"
295-
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pFoundationsRepository}"
295+
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pFoundationsRepository}"
296296
BranchName: !FindInMap [pCodeCommitBranch, !Ref pEnvironment, branch]
297297
OutputArtifactFormat: CODE_ZIP
298298
RunOrder: 1
@@ -306,7 +306,7 @@ Resources:
306306
- Name: SourceTeamArtifact
307307
Configuration:
308308
ConnectionArn: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/CodeConnection}}"
309-
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/${pTeamRepository}"
309+
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/${pTeamRepository}"
310310
BranchName: !FindInMap [pCodeCommitBranch, !Ref pEnvironment, branch]
311311
OutputArtifactFormat: CODE_ZIP
312312
RunOrder: 1
@@ -322,7 +322,7 @@ Resources:
322322
- Name: SourceMonitoringArtifact
323323
Configuration:
324324
ConnectionArn: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/CodeConnection}}"
325-
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/GitLab/SdlfGitLabGroup}}/{{resolve:ssm:/SDLF/${pGitPlatform}/Monitoring${pGitPlatform}}}"
325+
FullRepositoryId: !Sub "{{resolve:ssm:/SDLF/${pGitPlatform}/Group}}/{{resolve:ssm:/SDLF/${pGitPlatform}/Monitoring${pGitPlatform}}}"
326326
BranchName: !FindInMap [pCodeCommitBranch, !Ref pEnvironment, branch]
327327
OutputArtifactFormat: CODE_ZIP
328328
RunOrder: 1

sdlf-cicd/template-cicd-prerequisites.yaml

+21-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ Parameters:
1212
pGitPlatform:
1313
Description: Platform used to host git repositories
1414
Type: String
15-
AllowedValues: [CodeCommit, GitLab]
15+
AllowedValues: [CodeCommit, GitLab, GitHub]
1616
Default: CodeCommit
17-
pEnableGitlab:
18-
Description: Use GitLab instead of CodeCommit for SDLF repositories
19-
Type: String
20-
Default: false
2117
pEnableGlueJobDeployer:
2218
Description: Enable Glue Job Deployer optional feature
2319
Type: String
@@ -41,19 +37,37 @@ Conditions:
4137
GovCloudPartition: !Equals
4238
- !Sub ${AWS::Partition}
4339
- aws-us-gov
40+
GitPlatformGitLab: !Equals [!Ref pGitPlatform, "GitLab"]
41+
GitPlatformGitHub: !Equals [!Ref pGitPlatform, "GitHub"]
4442

4543
Resources:
4644
######## OPTIONAL SDLF FEATURES #########
47-
# when enabling Gitlab support, /SDLF/GitLab/Url and /SDLF/GitLab/AccessToken are required too (as secure strings)
45+
# when enabling GitLab support, /SDLF/GitLab/Url, /SDLF/GitLab/Group (containing the group or user name) and /SDLF/GitLab/AccessToken are required too (as secure strings)
4846
# then enable GitLab::Projects::Project third-party resource type in CloudFormation Registry
4947
rGitlabFeatureSsm:
5048
Type: AWS::SSM::Parameter
5149
Properties:
5250
Name: /SDLF/GitLab/Enabled
5351
Type: String
54-
Value: !Ref pEnableGitlab
52+
Value: !If
53+
- GitPlatformGitLab
54+
- true
55+
- false
5556
Description: Create repositories on GitLab instead of CodeCommit
5657

58+
# when enabling GitHub support, /SDLF/GitHub/Group (containing the org or user name) and /SDLF/GitHub/AccessToken are required too (as secure strings)
59+
# then enable GitHub::Repositories::Repository third-party resource type in CloudFormation Registry
60+
rGithubFeatureSsm:
61+
Type: AWS::SSM::Parameter
62+
Properties:
63+
Name: /SDLF/GitHub/Enabled
64+
Type: String
65+
Value: !If
66+
- GitPlatformGitHub
67+
- true
68+
- false
69+
Description: Create repositories on GitHub instead of CodeCommit
70+
5771
rGitPlatformSsm:
5872
Type: AWS::SSM::Parameter
5973
Properties:

0 commit comments

Comments
 (0)