diff --git a/.dockerignore b/.dockerignore index c4aa3d608d96407095b0138e0e86777cb6530d35..ebae7cd6b07f6c557ab76f74463ab80d307db9b0 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,8 @@ logs/ -README.md \ No newline at end of file +README.md +.travis.yml +.gitlab-ci.yml +.gitignore +docker-compose.prod.yml +docker-compose.yml +Dockerfile \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fbebd637e05365f2a3745c1c6c72a17065cf3d0a..eac2fe86756dd2074ac4d18c75733db78de86db6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -35,6 +35,12 @@ stages: .shared_set_develop_image_and_tag: &set_develop_image_and_tag | DEVELOP_IMAGE="$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHORT_SHA" +# Anchor to create release candidate name and tag. +.shared_set_release_candidate_image_and_tag: &set_release_candidate_image_and_tag | + RELEASE_CANDIDATE_NUMBER=$(git rev-list --count origin/develop...origin/$CI_COMMIT_REF_NAME) && RELEASE_CANDIDATE_NUMBER=$((RELEASE_CANDIDATE_NUMBER+1)) + RELEASE_CANDIDATE_TAG_POPULATED="$(echo $CI_COMMIT_REF_NAME | cut -f2 -d/)-rc$RELEASE_CANDIDATE_NUMBER" + RELEASE_CANDIDATE_IMAGE="$CI_REGISTRY_IMAGE/release:$RELEASE_CANDIDATE_TAG_POPULATED" + # Anchor to create master image name and tag. .shared_set_master_image_and_tag: &set_master_image_and_tag | MASTER_IMAGE="$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" @@ -43,7 +49,7 @@ stages: # creating foreign keys, creating stored procedures, enabling change tracking and snapshot retention. .shared_build_image_and_run_setup_script: &build_image_and_run_setup_script | docker build -t mssql . - docker run --rm -d -e ACCEPT_EULA=Y -e SA_PASSWORD=Tmtpass123 -p 1433:1433 -p 8080:8080 --name mssql --entrypoint "/opt/mssql/bin/sqlservr" mssql; + docker run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=$DB_PASSWORD -p 1433:1433 -p 8080:8080 --name mssql --entrypoint "/opt/mssql/bin/sqlservr" mssql; docker exec mssql "./shell_scripts/setup.sh" # Anchor to push an image to the registry with branch tag (used for all branches except Master and Develop). @@ -53,8 +59,13 @@ stages: # Anchor to push an image to the registry with develop tag. .shared_push_to_registry_develop_tag: &push_to_registry_develop_tag | - docker commit -c 'CMD ["sqlservr"]' mssql $BRANCH_IMAGE - docker push $BRANCH_IMAGE + docker commit -c 'CMD ["sqlservr"]' mssql $DEVELOP_IMAGE + docker push $DEVELOP_IMAGE + +# Anchor to push an image to the registry with release candidate tag. +.shared_push_to_registry_release_candidate_tag: &push_to_registry_release_candidate_tag | + docker commit -c 'CMD ["sqlservr"]' mssql $RELEASE_CANDIDATE_IMAGE + docker push $RELEASE_CANDIDATE_IMAGE # Anchor to push an image to the registry with master tag .shared_push_to_registry_master_tag: &push_to_registry_master_tag | @@ -110,6 +121,18 @@ publish_develop_branch: expire_in: 2 weeks +publish_populated_image_release_candidate_branch: + <<: *publish_docker_login_template + script: + - *generate_env + - *set_release_candidate_image_and_tag + - *build_image_and_run_setup_script + - *push_to_registry_release_candidate_tag + only: + - /^(\d+\.)?(\d+\.)?(\d+-rc\.\d+)$/ + - /^release\/.*$/ + + publish_master_branch: <<: *publish_docker_login_template script: diff --git a/Dockerfile b/Dockerfile index 812fe69cc29499bb0e2c15ee202c7a3ec4e2895d..35ab19db61c3c948006e1a3a2709e1938557643f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,24 @@ # Run Latest Linux image SQL Server from Microsoft. FROM mcr.microsoft.com/mssql/server +USER root # Change current working directory. WORKDIR /usr/src/app # Update packages, install curl, node/npm, & tedious -RUN apt-get -y update && \ - apt-get install -y curl && \ - curl -sL https://deb.nodesource.com/setup_6.x | bash - && \ - apt-get install -y nodejs && \ - npm install tedious - -# Update packages, install curl, node/npm, tedious. -RUN apt-get -y update +RUN apt-get -y update > /dev/null 2>&1 && \ + apt-get install -y curl > /dev/null 2>&1 && \ + curl -sL https://deb.nodesource.com/setup_6.x | bash - > /dev/null 2>&1 && \ + apt-get install -y nodejs > /dev/null 2>&1 && \ + apt-get -y install vim > /dev/null 2>&1 && \ + npm install tedious > /dev/null 2>&1 # Bundle app source. COPY . . # Grant permissions for the import-data script to be executable. -RUN chmod +x /usr/src/app/shell_scripts/setup.sh +RUN chmod +x /usr/src/app/shell_scripts/setup.sh && \ + chmod +x /usr/src/app/cleanup.sh #Set path for SQLCMD. ENV PATH "$PATH:/opt/mssql-tools/bin/" diff --git a/cleanup.sh b/cleanup.sh new file mode 100755 index 0000000000000000000000000000000000000000..083a4f054624fc83669001a1ef2cdc3d6267eb26 --- /dev/null +++ b/cleanup.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +# Remove all obsolete files. +rm -rf /usr/src/app/database_scripts /usr/src/app/shell_scripts /usr/src/app/cleanup.sh +echo "Cleanup Complete!" diff --git a/database_scripts/waitForSqlServer.sql b/database_scripts/waitForSqlServer.sql new file mode 100644 index 0000000000000000000000000000000000000000..d08dd13cac0df1864ab9d68a7a6ab3e7d0b65d98 --- /dev/null +++ b/database_scripts/waitForSqlServer.sql @@ -0,0 +1,5 @@ +USE master; +GO + +DECLARE @ServerRunning BIT=(SELECT CASE WHEN COUNT(*) = 1 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END AS IsRunning FROM sys.servers) +SELECT @ServerRunning diff --git a/shell_scripts/create_env_variables.sh b/shell_scripts/create_env_variables.sh index c0b57e421c80b9892b75c1fbe82a92455db29347..e4eb601351ca27e53d9ab6f5dd6109f6effaa0dd 100644 --- a/shell_scripts/create_env_variables.sh +++ b/shell_scripts/create_env_variables.sh @@ -11,4 +11,4 @@ create_env_file() { DB_PASSWORD=$1 TEXT fi -} \ No newline at end of file +} diff --git a/shell_scripts/setup.sh b/shell_scripts/setup.sh index 91e85a25ab815d0d20b482c3e3370d5b43178671..b3df1237b459d2c8b25bb83e8222e953716f0108 100644 --- a/shell_scripts/setup.sh +++ b/shell_scripts/setup.sh @@ -1,6 +1,4 @@ #!/usr/bin/env bash -# Wait for the SQL Server to launch. -sleep 10s # Source the configs. source /usr/src/app/.env @@ -9,25 +7,43 @@ source /usr/src/app/.env export querydb="sqlcmd -S localhost -U sa -P ${DB_PASSWORD}" && \ +sql_counter=1 +while [ $sql_counter -le 60 ] +do + TEMP_VAR=$(sqlcmd -S localhost -U sa -P "$DB_PASSWORD" -i /usr/src/app/database_scripts/waitForSqlServer.sql | grep '1\|0' | cut -d'(' -f1) + echo "Attempting to connect to SQL. Attempt number $sql_counter" + if [ $TEMP_VAR = "1" ] + then + echo "Successfully connected to SQL..." + break; + fi + if (( $sql_counter == 60 )) + then + echo "Failed to connected to SQL after $sql_counter attempts! Please verify your config file values are set correctly." + exit 1; + fi + echo "SQL unavailable..." + ((sql_counter++)) + sleep 1 +done && \ + + # Run the setup script to create the MS SQL Database. echo "Creating MS SQL Schema..." && \ $querydb -i /usr/src/app/database_scripts/createDatabase.sql && \ # Populates the MS SQL Database with mock data. -sleep 10s echo "Inserting Mock Data..." && \ $querydb -i /usr/src/app/database_scripts/insertMockData.sql && \ # Creates foreign key associations within mock MS SQL Database. -sleep 10s echo "Creating Foreign Key References..." && \ $querydb -i /usr/src/app/database_scripts/createForeignKeyReferences.sql && \ # Enables Change Tracking within mock MS SQL Database. -sleep 10s echo "Enabling Change Tracking..." && \ $querydb -i /usr/src/app/database_scripts/enableChangeTracking.sql && \ @@ -36,6 +52,11 @@ $querydb -i /usr/src/app/database_scripts/enableChangeTracking.sql && \ echo "Enabling Snapshot Retention..." && \ $querydb -i /usr/src/app/database_scripts/enableSnapshotRetention.sql && \ + # Creates stored procedures by iterating through every stored procedure script in database_scripts/stored_procedures. echo "Creating Stored Procedures..." && \ -for storedProcedure in /usr/src/app/database_scripts/stored_procedures/*.sql;do $querydb -i "$storedProcedure";done; +for storedProcedure in /usr/src/app/database_scripts/stored_procedures/*.sql;do $querydb -i "$storedProcedure";done + +# Cleanup job used to removed unneeded files after image has been successfully created and populated. +echo "Cleaning up environment..." && \ +. /usr/src/app/cleanup.sh