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

[#942] Added lagoon wrapper to cmds. #1244

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 7 additions & 7 deletions .scaffold/tests/bats/deployment1.bats
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ load _helper.deployment.bash
# Configure.
assert_contains "config add --force -l testlagoon -g https://api.lagoon.amazeeio.cloud/graphql -H ssh.lagoon.amazeeio.cloud -P 32222" "$(mock_get_call_args "${mock_lagoon}" 1)"
# Get a list of environments.
assert_contains "-l testlagoon list environments -p testproject --output-json --pretty" "$(mock_get_call_args "${mock_lagoon}" 2)"
assert_contains "-l testlagoon -p testproject list environments --output-json --pretty" "$(mock_get_call_args "${mock_lagoon}" 2)"
# Trigger a deployment.
assert_contains "-l testlagoon deploy branch -p testproject -b testbranch" "$(mock_get_call_args "${mock_lagoon}" 3)"
assert_contains "-l testlagoon -p testproject deploy branch -b testbranch" "$(mock_get_call_args "${mock_lagoon}" 3)"

# Assert that no other deployments ran.
assert_output_not_contains "Started ARTIFACT deployment."
Expand Down Expand Up @@ -309,15 +309,15 @@ load _helper.deployment.bash
# Configure.
assert_contains "config add --force -l testlagoon -g https://api.lagoon.amazeeio.cloud/graphql -H ssh.lagoon.amazeeio.cloud -P 32222" "$(mock_get_call_args "${mock_lagoon}" 1)"
# Get a list of environments.
assert_contains "-l testlagoon list environments -p testproject --output-json --pretty" "$(mock_get_call_args "${mock_lagoon}" 2)"
assert_contains "-l testlagoon -p testproject list environments --output-json --pretty" "$(mock_get_call_args "${mock_lagoon}" 2)"
# Explicitly set DB overwrite flag to 0 due to a bug in Lagoon.
assert_contains "-l testlagoon update variable -p testproject -e testbranch -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global" "$(mock_get_call_args "${mock_lagoon}" 3)"
assert_contains "-l testlagoon -p testproject update variable -e testbranch -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global" "$(mock_get_call_args "${mock_lagoon}" 3)"
# Override DB during re-deployment.
assert_contains "-l testlagoon update variable -p testproject -e testbranch -N DREVOPS_PROVISION_OVERRIDE_DB -V 1 -S global" "$(mock_get_call_args "${mock_lagoon}" 4)"
assert_contains "-l testlagoon -p testproject update variable -e testbranch -N DREVOPS_PROVISION_OVERRIDE_DB -V 1 -S global" "$(mock_get_call_args "${mock_lagoon}" 4)"
# Redeploy.
assert_contains "-l testlagoon deploy latest -p testproject -e testbranch" "$(mock_get_call_args "${mock_lagoon}" 5)"
assert_contains "-l testlagoon -p testproject deploy latest-e testbranch" "$(mock_get_call_args "${mock_lagoon}" 5)"
richardgaunt marked this conversation as resolved.
Show resolved Hide resolved
# Remove a DB import override flag for the current deployment.
assert_contains "-l testlagoon update variable -p testproject -e testbranch -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global" "$(mock_get_call_args "${mock_lagoon}" 6)"
assert_contains "-l testlagoon -p testproject update variable -e testbranch -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global" "$(mock_get_call_args "${mock_lagoon}" 6)"

# Assert that no other deployments ran.
assert_output_not_contains "Started ARTIFACT deployment."
Expand Down
28 changes: 15 additions & 13 deletions scripts/drevops/deploy-lagoon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ if ! command -v lagoon >/dev/null || [ -n "${DREVOPS_DEPLOY_LAGOON_LAGOONCLI_FOR
fi

note "Configuring Lagoon instance."
#shellcheck disable=SC2218
lagoon config add --force -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" -g "${DREVOPS_DEPLOY_LAGOON_INSTANCE_GRAPHQL}" -H "${DREVOPS_DEPLOY_LAGOON_INSTANCE_HOSTNAME}" -P "${DREVOPS_DEPLOY_LAGOON_INSTANCE_PORT}"
lagoon() { command lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" -p "${LAGOON_PROJECT}" "$@"; }

# ACTION: 'destroy'
# Explicitly specifying "destroy" action as a failsafe.
if [ "${DREVOPS_DEPLOY_ACTION}" = "destroy" ]; then
note "Destroying environment: project ${LAGOON_PROJECT}, branch: ${DREVOPS_DEPLOY_BRANCH}."
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" delete environment -p "${LAGOON_PROJECT}" -e "${DREVOPS_DEPLOY_BRANCH}" || true
lagoon delete environment -e "${DREVOPS_DEPLOY_BRANCH}" || true

# ACTION: 'deploy' OR 'deploy_override_db'
else
Expand All @@ -119,7 +121,7 @@ else

# Discover all available environments to check if this is a fresh deployment
# or a re-deployment of the existing environment.
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" list environments -p "${LAGOON_PROJECT}" --output-json --pretty >/tmp/lagoon-envs.json
lagoon list environments --output-json --pretty >/tmp/lagoon-envs.json
names="$(jq -r '.data[] | select(.deploytype | contains("pullrequest")) | .name' /tmp/lagoon-envs.json /dev/null 2>&1 || echo '')"

is_redeploy=0
Expand All @@ -136,39 +138,39 @@ else

# Explicitly set DB overwrite flag to 0 due to a bug in Lagoon.
# @see https://github.com/uselagoon/lagoon/issues/1922
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" update variable -p "${LAGOON_PROJECT}" -e "${deploy_pr_full}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true
lagoon update variable -e "${deploy_pr_full}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true

# Override DB during re-deployment.
if [ "${DREVOPS_DEPLOY_ACTION}" = "deploy_override_db" ]; then
note "Add a DB import override flag for the current deployment."
# To update variable value, we need to remove it and add again.
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" update variable -p "${LAGOON_PROJECT}" -e "${deploy_pr_full}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 1 -S global || true
lagoon update variable -e "${deploy_pr_full}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 1 -S global || true
fi

note "Redeploying environment: project ${LAGOON_PROJECT}, PR: ${DREVOPS_DEPLOY_PR}."
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" deploy pullrequest -p "${LAGOON_PROJECT}" -n "${DREVOPS_DEPLOY_PR}" --baseBranchName "${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -R "origin/${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -H "${DREVOPS_DEPLOY_BRANCH}" -M "${DREVOPS_DEPLOY_PR_HEAD}" -t "${deploy_pr_full}"
lagoon deploy pullrequest -n "${DREVOPS_DEPLOY_PR}" --baseBranchName "${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -R "origin/${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -H "${DREVOPS_DEPLOY_BRANCH}" -M "${DREVOPS_DEPLOY_PR_HEAD}" -t "${deploy_pr_full}"

if [ "${DREVOPS_DEPLOY_ACTION:-}" = "deploy_override_db" ]; then
note "Waiting for deployment to be queued."
sleep 10

note "Remove a DB import override flag for the current deployment."
# Note that a variable will be read by Lagoon during queuing of the build.
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" update variable -p "${LAGOON_PROJECT}" -e "${deploy_pr_full}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true
lagoon update variable -e "${deploy_pr_full}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true
fi

# Deployment of the fresh environment.
else
# If PR deployments are not configured in Lagoon - it will filter it out and will not deploy.
note "Deploying environment: project ${LAGOON_PROJECT}, PR: ${DREVOPS_DEPLOY_PR}."
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" deploy pullrequest -p "${LAGOON_PROJECT}" -n "${DREVOPS_DEPLOY_PR}" --baseBranchName "${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -R "origin/${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -H "${DREVOPS_DEPLOY_BRANCH}" -M "${DREVOPS_DEPLOY_PR_HEAD}" -t "${deploy_pr_full}"
lagoon deploy pullrequest -n "${DREVOPS_DEPLOY_PR}" --baseBranchName "${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -R "origin/${DREVOPS_DEPLOY_PR_BASE_BRANCH}" -H "${DREVOPS_DEPLOY_BRANCH}" -M "${DREVOPS_DEPLOY_PR_HEAD}" -t "${deploy_pr_full}"
fi

# Deploy branch.
else
# Discover all available environments to check if this is a fresh deployment
# or a re-deployment of the existing environment.
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" list environments -p "${LAGOON_PROJECT}" --output-json --pretty >/tmp/lagoon-envs.json
lagoon list environments --output-json --pretty >/tmp/lagoon-envs.json
names="$(jq -r '.data[] | select(.deploytype | contains("branch")) | .name' /tmp/lagoon-envs.json /dev/null 2>&1 || echo '')"

is_redeploy=0
Expand All @@ -185,32 +187,32 @@ else

# Explicitly set DB overwrite flag to 0 due to a bug in Lagoon.
# @see https://github.com/uselagoon/lagoon/issues/1922
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" update variable -p "${LAGOON_PROJECT}" -e "${DREVOPS_DEPLOY_BRANCH}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true
lagoon update variable -e "${DREVOPS_DEPLOY_BRANCH}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true

# Override DB during re-deployment.
if [ "${DREVOPS_DEPLOY_ACTION:-}" = "deploy_override_db" ]; then
note "Add a DB import override flag for the current deployment."
# To update variable value, we need to remove it and add again.
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" update variable -p "${LAGOON_PROJECT}" -e "${DREVOPS_DEPLOY_BRANCH}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 1 -S global || true
lagoon update variable -e "${DREVOPS_DEPLOY_BRANCH}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 1 -S global || true
fi

note "Redeploying environment: project ${LAGOON_PROJECT}, branch: ${DREVOPS_DEPLOY_BRANCH}."
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" deploy latest -p "${LAGOON_PROJECT}" -e "${DREVOPS_DEPLOY_BRANCH}" || true
lagoon deploy latest -e "${DREVOPS_DEPLOY_BRANCH}" || true

if [ "${DREVOPS_DEPLOY_ACTION:-}" = "deploy_override_db" ]; then
note "Waiting for deployment to be queued."
sleep 10

note "Remove a DB import override flag for the current deployment."
# Note that a variable will be read by Lagoon during queuing of the build.
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" update variable -p "${LAGOON_PROJECT}" -e "${DREVOPS_DEPLOY_BRANCH}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true
lagoon update variable -e "${DREVOPS_DEPLOY_BRANCH}" -N DREVOPS_PROVISION_OVERRIDE_DB -V 0 -S global || true
fi

# Deployment of the fresh environment.
else
# If current branch deployments does not match a regex in Lagoon - it will filter it out and will not deploy.
note "Deploying environment: project ${LAGOON_PROJECT}, branch: ${DREVOPS_DEPLOY_BRANCH}."
lagoon --force --skip-update-check -i "${DREVOPS_DEPLOY_SSH_FILE}" -l "${DREVOPS_DEPLOY_LAGOON_INSTANCE}" deploy branch -p "${LAGOON_PROJECT}" -b "${DREVOPS_DEPLOY_BRANCH}"
lagoon deploy branch -b "${DREVOPS_DEPLOY_BRANCH}"
fi
fi
fi
Expand Down
4 changes: 3 additions & 1 deletion scripts/drevops/task-custom-lagoon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ if ! command -v lagoon >/dev/null || [ -n "${DREVOPS_TASK_LAGOON_INSTALL_CLI_FOR
fi

note "Configuring Lagoon instance."
#shellcheck disable=SC2218
lagoon config add --force -l "${DREVOPS_TASK_LAGOON_INSTANCE}" -g "${DREVOPS_TASK_LAGOON_INSTANCE_GRAPHQL}" -H "${DREVOPS_TASK_LAGOON_INSTANCE_HOSTNAME}" -P "${DREVOPS_TASK_LAGOON_INSTANCE_PORT}"
lagoon() { command lagoon --force --skip-update-check -i "${DREVOPS_TASK_SSH_FILE}" -l "${DREVOPS_TASK_LAGOON_INSTANCE}" -p "${DREVOPS_TASK_LAGOON_PROJECT}" "$@"; }

note "Creating ${DREVOPS_TASK_LAGOON_NAME} task: project ${DREVOPS_TASK_LAGOON_PROJECT}, branch: ${DREVOPS_TASK_LAGOON_BRANCH}."
lagoon --force --skip-update-check -i "${DREVOPS_TASK_SSH_FILE}" -l "${DREVOPS_TASK_LAGOON_INSTANCE}" run custom -p "${DREVOPS_TASK_LAGOON_PROJECT}" -e "${DREVOPS_TASK_LAGOON_BRANCH}" -N "${DREVOPS_TASK_LAGOON_NAME}" -c "${DREVOPS_TASK_LAGOON_COMMAND}"
lagoon run custom -e "${DREVOPS_TASK_LAGOON_BRANCH}" -N "${DREVOPS_TASK_LAGOON_NAME}" -c "${DREVOPS_TASK_LAGOON_COMMAND}"

pass "Finished Lagoon task ${DREVOPS_TASK_LAGOON_NAME}."
Loading