This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding script and pipeline for continuous deployment (#100)
* Adding scripts * Moving files * Small fixes
- Loading branch information
1 parent
380a483
commit 48e2396
Showing
4 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Continuous Deployment | ||
|
||
## create-variable-group.sh | ||
|
||
This file is intended to be run manually as a one-time setup to create a variable group with variables in Azure DevOps. | ||
|
||
## deploy-latest-image.sh | ||
|
||
This script can be run manually or in an automated fashion. It will deploy the latest (time based) ACR image of Spektate to an Azure Container Instance. The script has logic to determine if an image of the same name is already running in the Azure Container Instance. An Azure pipelines YAML file at the root of this repo will refer to this file for continuous deployment. | ||
|
||
## deploy-spektate-ci.yaml | ||
|
||
This is an Azure pipelines YAML file that will orchestrate the deployment of the latest Spektate ACR to an Azure Container Instance. It depends on a variable group created by `create-variable-group.sh` and executes `deploy-latest-image.sh` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Expected set ENV VAR below | ||
|
||
# SP_APP_ID="REPLACE_ME" | ||
# SP_PASS="REPLACE_ME" | ||
# SP_TENANT="REPLACE_ME" | ||
# AZDO_ORG_NAME="REPLACE_ME" | ||
# AZDO_PROJECT_NAME="REPLACE_ME" | ||
# MANIFEST_REPO_NAME="REPLACE_ME" | ||
# AZ_STORAGE_NAME="REPLACE_ME" | ||
# PARTITION_KEY_NAME="REPLACE_ME" | ||
# AZ_STORAGE_TABLE_NAME="REPLACE_ME" | ||
# MANIFEST_REPO_PAT="REPLACE_ME" | ||
# AZDO_PIPELINE_PAT="REPLACE_ME" | ||
# AZ_STORAGE_KEY="REPLACE_ME" | ||
|
||
AZDO_ORG_URL="https://dev.azure.com/$AZDO_ORG_NAME" | ||
|
||
# Delete and create variable group | ||
vg_name="spektate-ci-vg" | ||
vg_result=$(az pipelines variable-group list --org $AZDO_ORG_URL -p $AZDO_PROJECT_NAME) | ||
vg_exists=$(echo $vg_result | jq -r --arg vg_name "$vg_name" '.[].name | select(. == $vg_name ) != null') | ||
vg_id=$(echo "$vg_result" | jq -r --arg vg_name "$vg_name" '.[] | select(.name == $vg_name) | .id') | ||
|
||
if [ $vg_id ]; then | ||
echo "variable group to delete is $vg_id" | ||
az pipelines variable-group delete --id "$vg_id" --yes --org $AZDO_ORG_URL -p $AZDO_PROJECT_NAME | ||
fi | ||
|
||
echo "Creating variable group $vg_name" | ||
CREATE_RESULT=$(az pipelines variable-group create --name $vg_name \ | ||
--org $AZDO_ORG_URL \ | ||
-p $AZDO_PROJECT_NAME \ | ||
--variables \ | ||
MANIFEST_REPO_NAME=$MANIFEST_REPO_NAME \ | ||
AZ_STORAGE_NAME=$AZ_STORAGE_NAME \ | ||
PARTITION_KEY_NAME=$PARTITION_KEY_NAME \ | ||
AZ_STORAGE_TABLE_NAME=$AZ_STORAGE_TABLE_NAME \ | ||
SP_APP_ID=$SP_APP_ID \ | ||
SP_TENANT=$SP_TENANT) | ||
|
||
GROUP_ID=$(echo $CREATE_RESULT | jq ".id") | ||
echo "The group id is $GROUP_ID" | ||
|
||
echo "Setting secret variables..." | ||
az pipelines variable-group variable create \ | ||
--org $AZDO_ORG_URL \ | ||
-p $AZDO_PROJECT_NAME \ | ||
--group-id "$GROUP_ID" \ | ||
--secret true \ | ||
--name "MANIFEST_REPO_PAT" \ | ||
--value $MANIFEST_REPO_PAT | ||
|
||
az pipelines variable-group variable create \ | ||
--org $AZDO_ORG_URL \ | ||
-p $AZDO_PROJECT_NAME \ | ||
--group-id "$GROUP_ID" \ | ||
--secret true \ | ||
--name "AZDO_PIPELINE_PAT" \ | ||
--value $AZDO_PIPELINE_PAT | ||
|
||
az pipelines variable-group variable create \ | ||
--org $AZDO_ORG_URL \ | ||
-p $AZDO_PROJECT_NAME \ | ||
--group-id "$GROUP_ID" \ | ||
--secret true \ | ||
--name "AZ_STORAGE_KEY" \ | ||
--value $AZ_STORAGE_KEY | ||
|
||
az pipelines variable-group variable create \ | ||
--org $AZDO_ORG_URL \ | ||
-p $AZDO_PROJECT_NAME \ | ||
--group-id "$GROUP_ID" \ | ||
--secret true \ | ||
--name "SP_PASS" \ | ||
--value $SP_PASS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#Fail fast | ||
set -e | ||
|
||
# ACR | ||
ACR_NAME="spektateacr" | ||
ACR_REPO="spektate" | ||
ACR_PASS=$(az acr credential show -n $ACR_NAME | jq -r ".passwords[0].value") | ||
ACR_SERVER_URL="https://$ACR_NAME.azurecr.io" | ||
|
||
# ACI | ||
CONTAINER_NAME="spektate-ci-env" | ||
SPEKTATE_PORT=5000 | ||
RESTART_POLICY="Always" | ||
|
||
# Expected set ENV VAR below | ||
|
||
# AZDO_ORG_NAME="REPLACE_ME" | ||
# AZDO_PROJECT_NAME="REPLACE_ME" | ||
# MANIFEST_REPO_NAME="REPLACE_ME" | ||
# AZ_STORAGE_NAME="REPLACE_ME" | ||
# PARTITION_KEY_NAME="REPLACE_ME" | ||
# AZ_STORAGE_TABLE_NAME="REPLACE_ME" | ||
# MANIFEST_REPO_PAT="REPLACE_ME" | ||
# AZDO_PIPELINE_PAT="REPLACE_ME" | ||
# AZ_STORAGE_KEY="REPLACE_ME" | ||
|
||
# Login to Azure | ||
# echo "az login --service-principal --username $(SP_APP_ID) --password $(SP_PASS) --tenant $(SP_TENANT)" | ||
# az login --service-principal --username "$(SP_APP_ID)" --password "$(SP_PASS)" --tenant "$(SP_TENANT)" | ||
|
||
# Get the latest image in ACR | ||
LATEST_SPEKTATE_TAG=$(az acr repository show-manifests -n $ACR_NAME --repository $ACR_REPO --top 1 --orderby time_desc --detail | jq -r ".[0].tags[0]") | ||
echo "\nThe latest tag is $LATEST_SPEKTATE_TAG\n" | ||
CUSTOM_IMAGE_NAME="$ACR_NAME.azurecr.io/$ACR_REPO:$LATEST_SPEKTATE_TAG" | ||
|
||
CONTAINER_COUNT=$(az container list -g $RESOURCE_GROUP --query "[?containers[0].name=='$CONTAINER_NAME']" | jq length) | ||
ACI_EXISTS="" | ||
IMAGE_NAME_SAME="" | ||
|
||
# ACI instance already exists? | ||
if [ "$CONTAINER_COUNT" -eq "1" ]; then | ||
ACI_EXISTS=true | ||
CURRENT_IMAGE=$(az container show \ | ||
--name $CONTAINER_NAME \ | ||
--resource-group $RESOURCE_GROUP \ | ||
| jq -r ".containers[0].image") | ||
|
||
echo "The current installed image on $CONTAINER_NAME is $CURRENT_IMAGE\n" | ||
|
||
# Lowercase | ||
LOWER_INSTALLED_IMAGE=$(echo $CURRENT_IMAGE | tr '[:upper:]' '[:lower:]') | ||
LOWER_LATEST_IMAGE=$(echo $CUSTOM_IMAGE_NAME | tr '[:upper:]' '[:lower:]') | ||
|
||
# Remove beginning and ending whitespace | ||
TRIMMED_INSTALLED_IMAGE="$(echo -e "${LOWER_INSTALLED_IMAGE}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" | ||
TRIMMED_LATEST_IMAGE="$(echo -e "${LOWER_LATEST_IMAGE}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" | ||
echo $TRIMMED_INSTALLED_IMAGE | ||
echo $TRIMMED_LATEST_IMAGE | ||
|
||
if [ "$TRIMMED_LATEST_IMAGE" = "$TRIMMED_INSTALLED_IMAGE" ]; then | ||
echo "Images have the same name...\n" | ||
IMAGE_NAME_SAME=1 | ||
fi | ||
fi | ||
|
||
# ACI instance has the same named image installed? | ||
if [ $IMAGE_NAME_SAME ]; then | ||
echo "Restarting $CONTAINER_NAME since the latest image is already installed.\n" | ||
az container restart \ | ||
--name $CONTAINER_NAME \ | ||
--resource-group $RESOURCE_GROUP | ||
echo "Restarted $CONTAINER_NAME. Exiting." | ||
exit 0 | ||
fi | ||
|
||
# If ACI instance already exists, then stop the instance | ||
if [ $ACI_EXISTS ]; then | ||
echo "Stopping container instance '$CONTAINER_NAME'\n" | ||
az container stop \ | ||
--name $CONTAINER_NAME \ | ||
--resource-group $RESOURCE_GROUP | ||
fi | ||
|
||
# Create/re-install image on instance | ||
echo "Install the latest image on the container instance '$CONTAINER_NAME'\n" | ||
az container create \ | ||
--name $CONTAINER_NAME \ | ||
--resource-group $RESOURCE_GROUP \ | ||
--image $CUSTOM_IMAGE_NAME \ | ||
--cpu 1 \ | ||
--memory 1 \ | ||
--registry-login-server "$ACR_NAME.azurecr.io" \ | ||
--registry-username $ACR_NAME \ | ||
--registry-password $ACR_PASS \ | ||
--port 80 $SPEKTATE_PORT \ | ||
--ip-address Public \ | ||
--restart-policy $RESTART_POLICY \ | ||
--environment-variables \ | ||
REACT_APP_MANIFEST=$MANIFEST_REPO_NAME \ | ||
REACT_APP_MANIFEST_ACCESS_TOKEN="$MANIFEST_REPO_PAT" \ | ||
REACT_APP_PIPELINE_ACCESS_TOKEN="$AZDO_PIPELINE_PAT" \ | ||
REACT_APP_PIPELINE_ORG=$AZDO_ORG_NAME \ | ||
REACT_APP_PIPELINE_PROJECT=$AZDO_PROJECT_NAME \ | ||
REACT_APP_SOURCE_REPO_ACCESS_TOKEN="$MANIFEST_REPO_PAT" \ | ||
REACT_APP_STORAGE_ACCESS_KEY="$AZ_STORAGE_KEY" \ | ||
REACT_APP_STORAGE_ACCOUNT_NAME=$AZ_STORAGE_NAME \ | ||
REACT_APP_STORAGE_PARTITION_KEY=$PARTITION_KEY_NAME \ | ||
REACT_APP_STORAGE_TABLE_NAME=$AZ_STORAGE_TABLE_NAME \ | ||
--dns-name-label $CONTAINER_NAME | ||
|
||
# If ACI instance already existed just start it since we stopped it | ||
if [ $ACI_EXISTS ]; then | ||
echo "Starting container instance '$CONTAINER_NAME'\n" | ||
az container start \ | ||
--name $CONTAINER_NAME \ | ||
--resource-group $RESOURCE_GROUP | ||
fi | ||
|
||
# Where to look for the running instance | ||
fqdn=$(az container show -n $CONTAINER_NAME -g $RESOURCE_GROUP | jq -r ".ipAddress.fqdn") | ||
echo "\nVisit http://$fqdn:$SPEKTATE_PORT" | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
trigger: none | ||
|
||
schedules: | ||
- cron: "0 3 * * Mon-Fri" | ||
branches: | ||
include: [master] | ||
displayName: M-F 7:00PM (UTC - 8:00) Pacific daily build | ||
always: true | ||
|
||
variables: | ||
- group: 'spektate-ci-vg' | ||
skipComponentGovernanceDetection: "true" | ||
|
||
jobs: | ||
- job: Spektate_Publish | ||
steps: | ||
- script: | | ||
# Login to Azure | ||
echo "az login --service-principal --username $(SP_APP_ID) --password $(SP_PASS) --tenant $(SP_TENANT)" | ||
az login --service-principal --username "$(SP_APP_ID)" --password "$(SP_PASS)" --tenant "$(SP_TENANT)" | ||
# Make env vars available to child processes by exporting | ||
export MANIFEST_REPO_NAME=$(MANIFEST_REPO_NAME) | ||
export MANIFEST_REPO_PAT=$(MANIFEST_REPO_PAT) | ||
export AZDO_PIPELINE_PAT=$(AZDO_PIPELINE_PAT) | ||
export AZDO_ORG_NAME=$(AZDO_ORG_NAME) | ||
export AZDO_PROJECT_NAME=$(AZDO_PROJECT_NAME) | ||
export AZ_STORAGE_KEY=$(AZ_STORAGE_KEY) | ||
export AZ_STORAGE_NAME=$(AZ_STORAGE_NAME) | ||
export PARTITION_KEY_NAME=$(PARTITION_KEY_NAME) | ||
export AZ_STORAGE_TABLE_NAME=$(AZ_STORAGE_TABLE_NAME) | ||
sh ./scripts/deploy-latest-image.sh | ||
displayName: "Run deploy script" | ||