Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deploy script. #24

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ The SECRET_KEY_BASE below is just a filler one for the purpose of testing locall

Run Docker Image: `docker run -t -p 4000:4000 -e DATABASE_URL='ecto://postgres:@host.docker.internal:5434/database' -e SECRET_KEY_BASE='B8rwzeX3DFLveiJ4cP28lRGc0PWdEr8ZF/hDoPRucw95Nzf2IPnu7lhEB+Yldx6Z' dpul-collections`

## Deployment

1. Connect to VPN
1. `BRANCH=<branch> ./bin/deploy staging`

## Learn more

* Official website: https://www.phoenixframework.org/
Expand Down
66 changes: 66 additions & 0 deletions bin/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash
ENV=$1
BRANCH_NAME="${BRANCH:-main}"
REPOSITORY="${REPO:-dpul-collections}"
JOB_NAME="${JOBNAME:-dpulc}"

# Make sure we're on VPN
if ! nslookup nomad-host-prod1.lib.princeton.edu 2>&1 > /dev/null
then
echo "Unable to connect to nomad-host-prod1. Ensure you're on VPN."
exit 1
fi

## Get Github Token
if ! command -v gh &> /dev/null
then
if [ -z "$GITHUB_TOKEN" ]
then
echo "gh must be installed or a token passed with GITHUB_TOKEN. Run 'brew install gh'."
exit 1
fi
fi

GH_TOKEN="${GITHUB_TOKEN:-$(gh auth token 2> /dev/null)}"

if [ "$GH_TOKEN" = "" ]
then
echo "Github token not set. Run 'gh auth login' and follow the directions."
exit 1
fi

if [[ -z "${ENV}" ]];
then
echo "Missing Environment. Command: 'BRANCH=main ./bin/deploy staging'."
exit
fi

# Create Github Deployment
DEPLOY_OUTPUT=$(curl -s -X POST -H "Accept: application/vnd.github+json-H" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer ${GH_TOKEN}" --data "{\"ref\":\"${BRANCH_NAME}\",\"description\":\"Deploy from Nomad script\", \"auto_merge\": false, \"environment\": \"${ENV}\", \"required_contexts\": [] }" "https://api.github.com/repos/pulibrary/${REPOSITORY}/deployments")
regex='"id": ([0-9]+),'
[[ $DEPLOY_OUTPUT =~ $regex ]]
DEPLOY_ID=${BASH_REMATCH[1]}

if [[ -z "${DEPLOY_ID}" ]]
then
echo "Unable to fetch Deploy ID."
exit 1
fi

# Create "Started" Deployment Status
curl -s -X POST -H "Accept: application/vnd.github+json-H" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer ${GH_TOKEN}" --data "{\"environment\":\"${ENV}\",\"state\":\"in_progress\",\"log_url\": \"https://nomad.lib.princeton.edu/ui/jobs/${JOB_NAME}-${ENV}\", \"description\":\"Deployment started.\"}" "https://api.github.com/repos/pulibrary/${REPOSITORY}/deployments/${DEPLOY_ID}/statuses" > /dev/null

# Deploy using nomad-host-prod1, which has the nomad management key.
ssh [email protected] << EOF
curl -s "https://raw.githubusercontent.com/pulibrary/${REPOSITORY}/${BRANCH_NAME}/config/deploy/${ENV}.hcl" | nomad job run -var "branch_or_sha=sha-$(git ls-remote https://github.com/pulibrary/${REPOSITORY}.git ${BRANCH_NAME} | awk '{ print substr($1,1,7) }')" -
EOF
retcode=$?

if [ $retcode -eq 0 ]
then
# Create "Completed Successfully" Deployment Status
curl -s -X POST -H "Accept: application/vnd.github+json-H" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer ${GH_TOKEN}" --data "{\"environment\":\"${ENV}\",\"state\":\"success\",\"log_url\": \"https://nomad.lib.princeton.edu/ui/jobs/${JOB_NAME}-${ENV}\", \"description\":\"Deployment finished successfully.\"}" "https://api.github.com/repos/pulibrary/${REPOSITORY}/deployments/${DEPLOY_ID}/statuses" > /dev/null
else
# Create "Failed" Deployment Status
curl -s -X POST -H "Accept: application/vnd.github+json-H" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer ${GH_TOKEN}" --data "{\"environment\":\"${ENV}\",\"state\":\"failure\",\"log_url\": \"https://nomad.lib.princeton.edu/ui/jobs/${JOB_NAME}-${ENV}\", \"description\":\"Deployment failed.\"}" "https://api.github.com/repos/pulibrary/${REPOSITORY}/deployments/${DEPLOY_ID}/statuses" > /dev/null
fi
3 changes: 3 additions & 0 deletions config/deploy/staging.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ job "dpulc-staging" {
region = "global"
datacenters = ["dc1"]
type = "service"
update {
auto_revert = true
}
group "web" {
count = 2
network {
Expand Down
Loading