Skip to content

Commit 1c18dd9

Browse files
authored
Initial github action workflow (#11)
1 parent 086a273 commit 1c18dd9

File tree

8 files changed

+190
-52
lines changed

8 files changed

+190
-52
lines changed

.github/workflows/deploy.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

deploy/deploy-ecr.sh

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
#!/bin/bash
22
#
3-
# Script to deploy a container image to an AWS Lambda Function
4-
#
5-
# REQUIRES:
6-
# jq (https://jqlang.github.io/jq/)
7-
# docker (https://docs.docker.com/desktop/) > version Docker 1.5
8-
# AWS CLI (https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
3+
# Script to deploy a container image to an AWS ECR.
94
#
105
# Command line arguments:
116
# [1] registry: Registry URI
127
# [2] repository: Name of repository to create
13-
# [3] prefix: Prefix for environment deploying to
14-
# [4] profile: Name of profile used to authenticate AWS CLI commands
8+
# [3] local: Whether script is being run locally
159
#
16-
# Example usage: ./deploy-ecr.sh "account-id.dkr.ecr.region.amazonaws.com" "container-image-name" "confluence-dev1" "confluence-named-profile"
10+
# Example usage: ./delpoy-ecr.sh "account-id.dkr.ecr.region.amazonaws.com" "docker-container-image"
1711

1812
REGISTRY=$1
19-
IMAGE_NAME=$2
20-
PREFIX=$3
21-
PROFILE=$4
13+
REPOSITORY=$2
14+
IS_LOCAL=$3
2215

23-
REPOSITORY=$PREFIX-$IMAGE_NAME
16+
# Determine if repo exists
17+
response=$(aws ecr describe-repositories --repository-names "$REPOSITORY" 2>&1)
18+
repo=$(echo "$response" | jq '.repositories[0].repositoryName')
19+
repo="${repo%\"}" # Remove suffix double quote
20+
repo="${repo#\"}" # Remove prefix double quote
2421

25-
# ECR Repo
26-
response=$(aws ecr describe-repositories --repository-names "$REPOSITORY" --profile "$PROFILE" 2>&1)
27-
if [[ $response == *"RepositoryNotFoundException"* ]]; then
22+
if [[ "$repo" == "$REPOSITORY" ]]; then
23+
echo "Repository exists: '$REPOSITORY' and will not be created."
24+
else
25+
# Creat repo
2826
echo "Respository does not exist. Creating repository: $REPOSITORY."
29-
# Create repo
3027
response=$(aws ecr create-repository --repository-name "$REPOSITORY" \
3128
--image-tag-mutability "MUTABLE" \
3229
--image-scanning-configuration scanOnPush=false \
33-
--encryption-configuration encryptionType="AES256" \
34-
--profile "$PROFILE" )
35-
30+
--encryption-configuration encryptionType="AES256" )
31+
3632
# Test if repo was created
3733
status=$(echo "$response" | jq '.repository.repositoryName')
3834
status="${status%\"}" # Remove suffix double quote
@@ -41,23 +37,18 @@ if [[ $response == *"RepositoryNotFoundException"* ]]; then
4137
echo "Repository was created."
4238
else
4339
echo "Respository could not be created."
44-
echo "Response: $response"
45-
exit 1
4640
fi
47-
else
48-
repo=$(echo "$response" | jq '.repositories[0].repositoryName')
49-
repo="${repo%\"}" # Remove suffix double quote
50-
repo="${repo#\"}" # Remove prefix double quote
51-
echo "Repository exists: '$REPOSITORY' and will not be created."
5241
fi
5342

54-
# Login
55-
docker login -u AWS https://$REGISTRY -p $(aws --profile $PROFILE ecr get-login-password --region us-west-2)
43+
if [[ "$IS_LOCAL" == "true" ]]; then
44+
# Login
45+
docker login -u AWS https://$REGISTRY -p $(aws ecr get-login-password --region us-west-2)
5646

57-
# Build
58-
cd ..
59-
docker build -t $REGISTRY/$REPOSITORY .
47+
# Build
48+
cd ..
49+
docker build -t $REGISTRY/$REPOSITORY .
6050

61-
# # Push
62-
docker push $REGISTRY/$REPOSITORY
63-
cd deploy
51+
# Push
52+
docker push $REGISTRY/$REPOSITORY
53+
cd deploy
54+
fi

deploy/deploy.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,28 @@
1313
# [2] repository: Name of repository to create
1414
# [3] prefix: Prefix to use for AWS resources associated with environment deploying to
1515
# [4] s3_state_bucket: Name of the S3 bucket to store Terraform state in (no need for s3:// prefix)
16-
# [5] profile: Name of profile used to authenticate AWS CLI commands
16+
# [5] is_local: Whether the script is executing locally (not in GitHub action): "true" or "false"
17+
#
18+
# Note, you need to be "logged in" via `aws configure` in order to deploy to AWS
1719
#
18-
# Example usage: ./deploy.sh "account-id.dkr.ecr.region.amazonaws.com" "container-image-name" "prefix-for-environment" "s3-state-bucket-name" "confluence-named-profile"
20+
# Example usage: ./deploy.sh "account-id.dkr.ecr.region.amazonaws.com" "container-image-name" "prefix-for-environment" "s3-state-bucket-name" "is-local"
1921

2022
REGISTRY=$1
21-
REPOSITORY=$2
23+
NAME=$2
2224
PREFIX=$3
2325
S3_STATE=$4
24-
PROFILE=$5
26+
IS_LOCAL=$5
2527

28+
REPOSITORY=$PREFIX-$NAME
2629

2730
# Deploy Container Image
28-
./deploy-ecr.sh $REGISTRY $REPOSITORY $PREFIX $PROFILE
31+
cd deploy/
32+
echo "./deploy-ecr.sh $REGISTRY $REPOSITORY $IS_LOCAL"
33+
./deploy-ecr.sh $REGISTRY $REPOSITORY $IS_LOCAL
34+
cd ..
2935

3036
# Deploy Terraform
3137
cd terraform/
32-
terraform init -reconfigure -backend-config="bucket=$S3_STATE" -backend-config="key=metroman.tfstate" -backend-config="region=us-west-2" -backend-config="profile=$PROFILE"
33-
terraform apply -var-file="conf.tfvars" -auto-approve
38+
terraform init -reconfigure -backend-config="bucket=$S3_STATE" -backend-config="key=$NAME.tfstate" -backend-config="region=us-west-2"
39+
terraform apply -auto-approve
3440
cd ..

deploy/terraform/conf.tfvars

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ provider "aws" {
1616
tags = local.default_tags
1717
}
1818
region = var.aws_region
19-
profile = var.profile
2019
}
2120

2221
# Data sources
@@ -47,7 +46,7 @@ locals {
4746
account_id = data.aws_caller_identity.current.account_id
4847
default_tags = length(var.default_tags) == 0 ? {
4948
application : var.app_name,
50-
environment : var.environment,
49+
environment : lower(var.environment),
5150
version : var.app_version
5251
} : var.default_tags
5352
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,3 @@ variable "prefix" {
3030
type = string
3131
description = "Prefix to add to all AWS resources as a unique identifier"
3232
}
33-
34-
variable "profile" {
35-
type = string
36-
description = "Named profile to build infrastructure with"
37-
}

0 commit comments

Comments
 (0)