Files
medicalalert-web-reloaded/bitbucket-pipelines.yml
Jay Sharma ff5b56dc44 Merged in feature/schedule (pull request #43)
Add schedule

* Add schedule

* Merged develop into feature/schedule

Approved-by: Rachit Bhargava
2024-03-06 20:42:13 +00:00

434 lines
16 KiB
YAML

definitions:
set-variables: &set-variables
- variables:
- name: VERSION
default: "latest"
# --------------------------------------------------------
set-variables-jobs: &set-variables-schedule-pipelines
- variables:
- name: ACTION
default: "create-schedule-pipelines"
allowed-values: # Optionally restrict variable values
- "create-schedule-pipelines"
- "delete-schedule-pipelines"
- "list-schedule-pipelines"
- name: PIPELINE_NAME
default: "common-jobs-dev"
allowed-values: # Optionally restrict variable values
- "backup-mysql-monthly-prod"
- "backup-static-monthly-prod"
- "cleanup-backup-monthly-prod"
- "common-jobs-dev" # for patch with default values
- name: BRANCH_NAME
default: "feature/backups"
allowed-values: # Optionally restrict variable values
- "develop"
- "feature/backups"
- "feature/backup"
- name: CRONTIME
default: ENTER_CRON_SCHEDULE
# -------------------------------------------------------
# allowed-values: # Optionally restrict variable values
# - "0 * * * 1 ? *" # On 1st every month
# - "0 * * * * 7 *" # Every sunday
# - ""
# - "ENTER_CRON_SCHEDULE"
# ----------------------------------------------------
set-variables-jobs: &set-variables-jobs-dev
- variables:
- name: ENV
default: "dev"
allowed-values: # Optionally restrict variable values
- "dev"
- name: JOBNAME
default: "wp-auto-patch"
allowed-values: # Optionally restrict variable values
- "wp-auto-patch"
- "mysql-backup"
- "wp-data-backup"
- "mysql-restore"
- "wp-data-restore"
- name: SUBDOMAIN
default: dev01
allowed-values: # Optionally restrict variable values
- "dev01"
- "www"
- "dev02"
- "dev03"
- "dev04"
- "dev05"
- name: RESTORE_VERSION
default: "PASTE_RESTORE_VERSION_FILE_NAME"
set-variables-jobs: &set-variables-jobs
- variables:
- name: ENV
default: "qa"
allowed-values: # Optionally restrict variable values
- "qa"
- "stage"
- "prod"
- name: JOBNAME
default: "mysql-backup"
allowed-values: # Optionally restrict variable values
- "mysql-backup"
- "wp-data-backup"
- "mysql-restore"
- "wp-data-restore"
- name: SUBDOMAIN
default: www
allowed-values: # Optionally restrict variable values
- "www"
- name: RESTORE_VERSION
default: "PASTE_RESTORE_VERSION_FILE_NAME"
set-variables-subdomain: &set-variables-subdomain
- variables:
- name: ENV
default: "dev"
allowed-values: # Optionally restrict variable values
- "dev"
- name: URL_DOMAIN
default: dev01.dev-medicalalert.com
allowed-values: # Optionally restrict variable values
- "dev01.dev-medicalalert.com"
- "dev02.dev-medicalalert.com"
- "dev03.dev-medicalalert.com"
- "dev04.dev-medicalalert.com"
- "dev05.dev-medicalalert.com"
- name: MYSQL_FILE_NAME
default: "MYSQL_FILE_NAME_TO_DEPLOY"
- name: STATIC_FILE_NAME
default: "STATIC_FILE_NAME_TO_DEPLOY"
steps:
- step: &deploy
name: deploy to $ENV environment
image: atlassian/default-image:3
script:
# check if env file exists
- if [ -e setenv.sh ]; then
- cat setenv.sh
- source setenv.sh
- fi
- echo "This is for $ENV environment"
- chmod 755 ./validation.sh && ./validation.sh default && echo validation-done || exit 9
- ls -lrth && cd build/${ENV}
- curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install
- curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl
- export DB_PASSWORD=$(aws ssm get-parameters --names $ENV-medicalalert-masterdbpass --query "Parameters[*].{Value:Value}" --output text)
- export DB_PASSWORD=`echo -n "$DB_PASSWORD" | base64` && sed -i "/DB_PASSWORD/c\ DB_PASSWORD:\ $DB_PASSWORD" secrets.yaml
- envsubst < deployment.tpl > deployment.yaml
- cat deployment.yaml
- pipe: atlassian/aws-eks-kubectl-run:2.2.0
variables:
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
CLUSTER_NAME: $CLUSTER
KUBECTL_COMMAND: "apply"
RESOURCE_PATH: "../${ENV}"
KUBECTL_ARGS: ""
KUBECTL_APPLY_ARGS: "-f"
DEBUG: "true"
after-script:
- aws eks update-kubeconfig --name caresage-eks-cluster-$ENV --region us-east-1
- sleep 10 && while kubectl get po -A |grep -i medicalalert-web|egrep -i '0/1|crash'; do sleep 10; echo "--- still getting 0/1 or crash pods-----"; done
- chmod 755 ./multidev-sync.sh && ./multidev-sync.sh
pipelines:
# our custom pipeline, what shows up Bitbucket web app
custom:
# -------------------------------------------
schedule-pipelines:
- <<: *set-variables-schedule-pipelines
- step:
name: vars for schedule-pipelines
script:
- echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> schedule-setenv.sh
- echo "export BITBUCKET_WORKSPACE=$BITBUCKET_WORKSPACE" >> schedule-setenv.sh
- echo "export TOKEN=$TOKEN" >> schedule-setenv.sh
- echo "export ACTION=$ACTION" >> schedule-setenv.sh
- echo "export PIPELINE_NAME=$PIPELINE_NAME" >> schedule-setenv.sh
- echo "export BRANCH_NAME=$BRANCH_NAME" >> schedule-setenv.sh
- echo "export CRONTIME=\"$CRONTIME\"" >> schedule-setenv.sh
artifacts: # define the artifacts to be passed to each future step
- schedule-setenv.sh
- step:
name: Actions for schedule-pipelines
image: atlassian/pipelines-awscli
script:
- cat schedule-setenv.sh && chmod 755 ./schedule-pipeline.sh && ./schedule-pipeline.sh $ACTION
# -------------------------------------------------
backup-mysql-monthly-prod:
- step:
name: vars for common-jobs-prod
script:
- echo "export JOBNAME=mysql-backup" >> setenv.sh
- echo "export ENV=prod" >> setenv.sh
- echo "export CLUSTER=caresage-eks-cluster-prod" >> setenv.sh
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export SUBDOMAIN=www" >> setenv.sh
- echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh
- echo "export TOKEN=$TOKEN" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
name: common jobs for SQL and WWW backup and restore DEV
image: atlassian/pipelines-awscli
script:
- source setenv.sh && chmod 755 ./common-jobs-bitbucket.sh
- ./common-jobs-bitbucket.sh $JOBNAME
backup-static-monthly-prod:
- step:
name: vars for common-jobs-prod
script:
- echo "export JOBNAME=wp-data-backup" >> setenv.sh
- echo "export ENV=prod" >> setenv.sh
- echo "export CLUSTER=caresage-eks-cluster-prod" >> setenv.sh
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export SUBDOMAIN=www" >> setenv.sh
- echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh
- echo "export TOKEN=$TOKEN" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
name: common jobs for SQL and WWW backup and restore DEV
image: atlassian/pipelines-awscli
script:
- source setenv.sh && chmod 755 ./common-jobs-bitbucket.sh && ./common-jobs-bitbucket.sh $JOBNAME
cleanup-backup-monthly-prod:
- step:
name: vars for common-jobs-prod
script:
- echo "export JOBNAME=cleanup-backup" >> setenv.sh
- echo "export ENV=prod" >> setenv.sh
- echo "export CLUSTER=caresage-eks-cluster-prod" >> setenv.sh
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export SUBDOMAIN=www" >> setenv.sh
- echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh
- echo "export TOKEN=$TOKEN" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
name: common jobs for SQL and WWW backup and restore DEV
image: atlassian/pipelines-awscli
script:
- source setenv.sh && chmod 755 ./common-jobs-bitbucket.sh && ./common-jobs-bitbucket.sh $JOBNAME
deploy-dev:
- <<: *set-variables
- step:
<<: *deploy
deployment: dev
name: "deploy to dev"
deploy-qa:
- <<: *set-variables
- step:
<<: *deploy
deployment: qa
name: "deploy to qa"
release-stg:
- <<: *set-variables
- step:
<<: *deploy
deployment: stage
name: "deploy to stage"
release-prod:
- <<: *set-variables
- step:
<<: *deploy
deployment: prod
name: "deploy to prod"
common-jobs-dev:
- <<: *set-variables-jobs-dev
- step:
name: vars for common-jobs-dev
script:
- echo "export JOBNAME=$JOBNAME" >> setenv.sh
- echo "export ENV=$ENV" >> setenv.sh
- echo "export CLUSTER=caresage-eks-cluster-$ENV" >> setenv.sh
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export RESTORE_VERSION=$RESTORE_VERSION" >> setenv.sh
- echo "export SUBDOMAIN=$SUBDOMAIN" >> setenv.sh
- echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh
- echo "export TOKEN=$TOKEN" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
name: common jobs for SQL and WWW backup and restore DEV
image: atlassian/pipelines-awscli
script:
- source setenv.sh && chmod 755 ./common-jobs-bitbucket.sh
- ./common-jobs-bitbucket.sh $JOBNAME
common-jobs:
- <<: *set-variables-jobs
- step:
name: vars for common-jobs qa/stage/prod
script:
- echo "export JOBNAME=$JOBNAME" >> setenv.sh
- echo "export ENV=$ENV" >> setenv.sh
- echo "export CLUSTER=caresage-eks-cluster-$ENV" >> setenv.sh
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export RESTORE_VERSION=$RESTORE_VERSION" >> setenv.sh
- echo "export SUBDOMAIN=$SUBDOMAIN" >> setenv.sh
- echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh
- echo "export TOKEN=$TOKEN" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
name: common jobs for SQL and WWW backup and restore.
image: atlassian/pipelines-awscli
script:
- source setenv.sh && chmod 755 ./common-jobs-bitbucket.sh
- ./common-jobs-bitbucket.sh $JOBNAME
subdomain-deploy:
- <<: *set-variables-subdomain
- step:
name: vars for subdomain
script:
- echo "export SUBDOMAIN=`echo $URL_DOMAIN | awk -F. '{print $1}'`" >> setenv.sh
- echo "export ENV=$ENV" >> setenv.sh
- echo "export CLUSTER=caresage-eks-cluster-$ENV" >> setenv.sh
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export RESTORE_VERSION=$RESTORE_VERSION" >> setenv.sh
- echo "export URL_DOMAIN=$URL_DOMAIN" >> setenv.sh
- echo "export MYSQL_FILE_NAME=$MYSQL_FILE_NAME" >> setenv.sh
- echo "export STATIC_FILE_NAME=$STATIC_FILE_NAME" >> setenv.sh
- echo "export BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG" >> setenv.sh
- echo "export TOKEN=$TOKEN" >> setenv.sh
artifacts:
- setenv.sh
- step:
name: Deploy the wp source code
trigger: manual
image: atlassian/pipelines-awscli
script:
- source setenv.sh
- chmod 755 ./common-jobs-bitbucket.sh && ./common-jobs-bitbucket.sh subdomain_deploy
artifacts:
- setenv.sh
- step:
name: Deploy the mysql database
trigger: manual
image: atlassian/pipelines-awscli
script:
- source setenv.sh
- chmod 755 ./common-jobs-bitbucket.sh && ./common-jobs-bitbucket.sh mysql-restore $MYSQL_FILE_NAME
artifacts:
- setenv.sh
- step:
name: Deploy the static data
trigger: manual
image: atlassian/pipelines-awscli
script:
- source setenv.sh
- chmod 755 ./common-jobs-bitbucket.sh && ./common-jobs-bitbucket.sh wp-data-restore $STATIC_FILE_NAME
release:
- <<: *set-variables
- step:
name: "plan for the release and tag the given version."
image: atlassian/pipelines-awscli
script:
#- chmod 755 ./validation.sh && ./validation.sh default
- echo "make sure that release version file and plan has been added to the release repo"
- source release
- export MANIFEST=$(aws ecr batch-get-image --repository-name medicalalert-web --image-ids imageTag=$VERSION --output json | jq --raw-output --join-output '.images[0].imageManifest')
- aws ecr put-image --repository-name medicalalert-web --image-tag $RELEASE --image-manifest "$MANIFEST" && echo ------ image $VERSION has been retagged as $RELEASE || exit 9
- echo "export VERSION=$RELEASE" >> setenv.sh
- echo "export ENV=stage" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
<<: *deploy
deployment: stage
name: "deploy the release to stage"
trigger: manual
- step:
name: vars for prod
script:
- source release
- echo "export VERSION=$RELEASE" >> setenv.sh
- echo "export ENV=prod" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
<<: *deploy
deployment: prod
name: "deploy the release to production"
trigger: manual
pull-requests:
develop:
- step:
name: build the application and push that to dev env.
image: atlassian/default-image:3
script:
- chmod 755 ./validation.sh && ./validation.sh default
- curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install
- export BUILD_D=$BITBUCKET_BRANCH_$BITBUCKET_COMMIT_$BITBUCKET_BUILD_NUMBER
- export IMAGE="medicalalert-web" && export VERSION=$BITBUCKET_BUILD_NUMBER
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 716593996126.dkr.ecr.us-east-1.amazonaws.com
- docker build -t ${IMAGE} .
- docker tag ${IMAGE} ${IMAGE}:${VERSION}
- pipe: atlassian/aws-ecr-push-image:1.6.2
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
IMAGE_NAME: $IMAGE
TAGS: $VERSION
services:
- docker
caches:
- docker
- step:
name: vars for dev
script:
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export ENV=dev" >> setenv.sh
artifacts: # define the artifacts to be passed to each future step
- setenv.sh
- step:
<<: *deploy
deployment: dev
name: "deploy to dev"
- step:
name: vars for qa
script:
- echo "export VERSION=$BITBUCKET_BUILD_NUMBER" >> setenv.sh
- echo "export ENV=qa" >> setenv.sh
artifacts:
- setenv.sh
- step:
<<: *deploy
deployment: qa
name: "deploy to qa"
trigger: manual