1+ # Main build pipeline that verifies, builds, and deploys the software
2+ name : Build and Deploy
3+ # Events that trigger the workflow
4+ on :
5+ # Trigger based on push to all branches - TODO
6+ # push:
7+ # branches:
8+ # - 'development'
9+ # - 'feature/**'
10+ # - 'release/**'
11+ # - 'main'
12+ # tags-ignore:
13+ # - '*'
14+ # Run workflow manually from the Actions tab
15+ workflow_dispatch :
16+ inputs :
17+ venue :
18+ type : choice
19+ description : Venue to deploy to
20+ options :
21+ - DEV1
22+ - DEV2
23+ - OPS
24+
25+ # Environment variables
26+ env :
27+ APP_NAME_ENV : ' metroman'
28+
29+ jobs :
30+ build :
31+ name : Build and Deploy
32+ # The type of runner that the job will run on
33+ runs-on : ubuntu-latest
34+ steps :
35+
36+ # DEV1 environment variables
37+ - name : Set Environment Variables
38+ if : github.event.inputs.venue == 'DEV1'
39+ run : |
40+ echo "TARGET_ENV=DEV1" >> $GITHUB_ENV
41+ echo "PREFIX_ENV=confluence-dev1" >> $GITHUB_ENV
42+
43+ # DEV2 environment variables
44+ - name : Set Environment Variables
45+ if : github.event.inputs.venue == 'DEV2'
46+ run : |
47+ echo "TARGET_ENV=DEV2" >> $GITHUB_ENV
48+ echo "PREFIX_ENV=confluence-dev2" >> $GITHUB_ENV
49+
50+ # OPS environment variables
51+ - name : Set Environment Variables
52+ if : github.event.inputs.venue == 'OPS'
53+ run : |
54+ echo "TARGET_ENV=OPS" >> $GITHUB_ENV
55+ echo "PREFIX_ENV=confluence-ops" >> $GITHUB_ENV
56+
57+ # Check out GitHub repo
58+ - uses : actions/checkout@v4
59+
60+ # SNYK IAC scan and report - TODO
61+ # - name: Run Snyk IAC to test and report
62+ # uses: snyk/actions/iac@master
63+ # env:
64+ # SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
65+ # with:
66+ # command: test
67+ # args: >
68+ # --org=${{ secrets.SNYK_ORG_ID }}
69+ # --severity-threshold=high
70+ # --report
71+
72+ # SNYK Python
73+ # - name: Run Snyk Python to test
74+ # uses: snyk/actions/python-3.10@master
75+ # env:
76+ # SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
77+ # with:
78+ # command: test
79+ # args: >
80+ # --org=${{ secrets.SNYK_ORG_ID }}
81+ # --project-name=${{ github.repository }}
82+ # --severity-threshold=high
83+ # --fail-on=all
84+ # - name: Run Snyk Python to report
85+ # uses: snyk/actions/python-3.10@master
86+ # env:
87+ # SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
88+ # with:
89+ # command: monitor
90+ # args: >
91+ # --org=${{ secrets.SNYK_ORG_ID }}
92+ # --project-name=${{ github.repository }}
93+
94+ # Configure credentials
95+ - name : Configure AWS credentials
96+ uses : aws-actions/configure-aws-credentials@v4
97+ with :
98+ aws-access-key-id : ${{ secrets[format('AWS_ACCESS_KEY_ID_{0}', env.TARGET_ENV)] }}
99+ aws-secret-access-key : ${{ secrets[format('AWS_SECRET_ACCESS_KEY_{0}', env.TARGET_ENV)] }}
100+ aws-region : us-west-2
101+ mask-aws-account-id : true
102+
103+ # Login and define registry, repository, and tag names
104+ - name : Login to AWS ECR
105+ id : login-ecr
106+ uses : aws-actions/amazon-ecr-login@v2
107+ with :
108+ mask-password : ' true'
109+ - name : Define ECR registry, repository, and image tag names
110+ run : |
111+ echo "REGISTRY=${{ steps.login-ecr.outputs.registry }}" >> $GITHUB_ENV
112+ echo "REPOSITORY=${PREFIX_ENV}-${APP_NAME_ENV}" >> $GITHUB_ENV
113+ echo "IMAGE_TAG=latest" >> $GITHUB_ENV
114+
115+ # Create ECR repository (if it does not exist)
116+ - name : Create AWS ECR Repository
117+ run : deploy/deploy-ecr.sh $REGISTRY $REPOSITORY
118+
119+ # Build and push Docker container image
120+ - name : Build and Push to AWS ECR
121+ run : |
122+ docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
123+ docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
124+
125+ # Set up Terraform
126+ - name : Setup Terraform
127+ uses : hashicorp/setup-terraform@v3
128+
129+ - name : Define TF_VAR values
130+ run : |
131+ echo "TF_VAR_environment=$TARGET_ENV" >> $GITHUB_ENV
132+ echo "TF_VAR_prefix=$PREFIX_ENV" >> $GITHUB_ENV
133+ echo "TF_IN_AUTOMATION=true" >> $GITHUB_ENV
134+
135+ - name : Initialize Terraform
136+ working-directory : terraform/
137+ run : |
138+ terraform init -reconfigure \
139+ -backend-config="bucket=${PREFIX_ENV}-tf-state" \
140+ -backend-config="key=${APP_NAME_ENV}.tfstate" \
141+ -backend-config="region=${AWS_DEFAULT_REGION}"
142+
143+ - name : Validate Terraform
144+ working-directory : terraform/
145+ run : terraform validate -no-color
146+
147+ # Deploy AWS infrastructure
148+ - name : Deploy Terraform
149+ working-directory : terraform/
150+ run : terraform apply -auto-approve
0 commit comments