Skip to content

Commit

Permalink
Merge branch 'main' into ops/workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
DrizzlyOwl committed Jan 20, 2023
2 parents ee7b7ea + 653064e commit 169c6c4
Show file tree
Hide file tree
Showing 23 changed files with 684 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.database.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MSSQL_INITIAL_DATABASE="sip"
MSSQL_SA_PASSWORD="Your_password123"
ACCEPT_EULA="Y"
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ASPNETCORE_ENVIRONMENT="Development"
ApiKeys__0='{\"userName\": \"apikey\",\"apiKey\": \"apikey\"}'
ConnectionStrings__DefaultConnection="Data Source=db;Initial Catalog=sip;persist security info=True;User id=sa;Password=Your_password123"
43 changes: 43 additions & 0 deletions .github/workflows/build-and-push-image-development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Continuous delivery (development)

on:
push:
branches:
- main

jobs:
build-and-push-image-development:
name: Build and push image development
environment: dev
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Azure Container Registry login
uses: docker/login-action@v2
with:
username: ${{ secrets.DEVELOPMENT_AZURE_ACR_CLIENTID }}
password: ${{ secrets.DEVELOPMENT_AZURE_ACR_SECRET }}
registry: ${{ secrets.DEVELOPMENT_ACR_URL }}

- name: Prepare tags
id: prepare-tags
run: |
DOCKER_IMAGE=${{ secrets.DEVELOPMENT_ACR_URL }}/tramsapi-app
VERSION=latest
TAGS="${DOCKER_IMAGE}:${VERSION}"
if [ "${{ github.event_name }}" = "push" ]; then
VERSION=sha-${GITHUB_SHA}
TAGS="$TAGS,${DOCKER_IMAGE}:${VERSION}"
fi
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
echo "deploy-version=${VERSION}" >> $GITHUB_OUTPUT
- name: Push image
uses: docker/build-push-action@v3
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.prepare-tags.outputs.tags }}
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,23 @@ _NCrunch*

# node_modules
CypressTests/node_modules

# Homebrew
Brewfile.lock.json

# Environment variables
.env
.env.*
.env.local
.env.*.local
!.env.development.local.example
!.env.database.example

### Terraform
.terraformrc*
terraform.rc*
*.tfstate*
*.tfvars*
!terraform.tfvars.example
.terraform/
backend.vars
39 changes: 39 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
FROM mcr.microsoft.com/dotnet/sdk:3.1-bullseye AS build
WORKDIR /build

ENV DEBIAN_FRONTEND=noninteractive

COPY . .

RUN mkdir -p /app/SQL
RUN touch /app/SQL/DbMigrationScriptLegacy.sql
RUN touch /app/SQL/DbMigrationScript.sql

RUN dotnet restore TramsDataApi.sln
RUN dotnet new tool-manifest
RUN dotnet tool install dotnet-ef --version 6.0.5
ENV PATH="$PATH:/root/.dotnet/tools"

RUN dotnet ef migrations script --output /app/SQL/DbMigrationScriptLegacy.sql --project TramsDataApi --context TramsDataApi.DatabaseModels.LegacyTramsDbContext --idempotent -v
RUN dotnet ef migrations script --output /app/SQL/DbMigrationScript.sql --project TramsDataApi --context TramsDataApi.DatabaseModels.TramsDbContext --idempotent --no-build -v

# this build has no effect on ef migrations because it is a "Release" configuration
RUN dotnet build -c Release TramsDataApi.sln --no-restore
RUN dotnet publish TramsDataApi -c Release -o /app --no-restore

ARG ASPNET_IMAGE_TAG
FROM mcr.microsoft.com/dotnet/aspnet:3.1-bullseye-slim AS final

RUN apt-get update
RUN apt-get install unixodbc curl gnupg -y
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/11/prod.list | tee /etc/apt/sources.list.d/msprod.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install msodbcsql18 mssql-tools18 -y

COPY --from=build /app /app

WORKDIR /app
COPY ./script/web-docker-entrypoint.sh ./docker-entrypoint.sh
RUN chmod +x ./docker-entrypoint.sh
EXPOSE 80/tcp
40 changes: 40 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: "3.8"
services:
webapi:
build:
context: .
dockerfile: Dockerfile
command: /bin/bash -c "./docker-entrypoint.sh dotnet TramsDataApi.dll"
ports:
- 80:80/tcp
restart: always
depends_on:
- db
- sqlcmd
env_file:
- .env.development
networks:
- dev

db:
image: mcr.microsoft.com/azure-sql-edge:latest
env_file: .env.database
ports:
- 1433:1433
networks:
- dev

sqlcmd:
image: mcr.microsoft.com/mssql-tools:latest
env_file: .env.database
command: /etc/docker-entrypoint.sh
depends_on:
- db
stdin_open: true
volumes:
- ./script/sqlcmd-docker-entrypoint.sh:/etc/docker-entrypoint.sh
networks:
- dev

networks:
dev:
21 changes: 21 additions & 0 deletions script/sqlcmd-docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# exit on failures
set -e
set -o pipefail

MSSQL_INITIAL_DATABASE="${MSSQL_INITIAL_DATABASE:?}"

echo "CREATE DATABASE $MSSQL_INITIAL_DATABASE;" > ./setup.sql
echo "GO" >> ./setup.sql

echo "Creating initial database ..."
until /opt/mssql-tools/bin/sqlcmd -S db -U sa -P "$MSSQL_SA_PASSWORD" -d master -i ./setup.sql
do
echo "not ready yet..."
sleep 1
done

rm ./setup.sql

echo "Created database $MSSQL_INITIAL_DATABASE ..."
33 changes: 33 additions & 0 deletions script/web-docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# exit on failures
set -e
set -o pipefail

ConnectionStrings__DefaultConnection=${ConnectionStrings__DefaultConnection:?}

declare -A mysqlconn

for keyvaluepair in $(echo "$ConnectionStrings__DefaultConnection" | sed "s/ //g; s/;/ /g")
do
IFS=" " read -r -a ARR <<< "${keyvaluepair//=/ }"
mysqlconn[${ARR[0]}]=${ARR[1]}
done

echo "Running TramsDbContext database migrations ..."
until /opt/mssql-tools18/bin/sqlcmd -S "${mysqlconn[Server]}" -U "${mysqlconn[UserId]}" -P "${mysqlconn[Password]}" -d "${mysqlconn[Database]}" -C -i /app/SQL/DbMigrationScript.sql -o /app/SQL/DbMigrationScriptOutput.txt
do
cat /app/SQL/DbMigrationScriptOutput.txt
echo "Retrying database migrations ..."
sleep 5
done

echo "Running LegacyTramsDbContext database migrations ..."
until /opt/mssql-tools18/bin/sqlcmd -S "${mysqlconn[Server]}" -U "${mysqlconn[UserId]}" -P "${mysqlconn[Password]}" -d "${mysqlconn[Database]}" -C -i /app/SQL/DbMigrationScriptLegacy.sql -o /app/SQL/DbMigrationScriptOutputLegacy.txt
do
cat /app/SQL/DbMigrationScriptOutputLegacy.txt
echo "Retrying database migrations ..."
sleep 5
done

exec "$@"
26 changes: 26 additions & 0 deletions terraform/.terraform-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
formatter: "markdown table"
version: "~> 0.16"
settings:
anchor: true
default: true
description: false
escape: true
hide-empty: false
html: true
indent: 2
lockfile: true
read-comments: true
required: true
sensitive: true
type: true
sort:
enabled: true
by: name
output:
file: README.md
mode: inject
template: |-
<!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS -->
1 change: 1 addition & 0 deletions terraform/.terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.3.7
80 changes: 80 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions terraform/Brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
brew "tfenv"
brew "terraform-docs"
brew "tfsec"
brew "az"
brew "coreutils"
brew "jq"
Loading

0 comments on commit 169c6c4

Please sign in to comment.