Files
doczyai-pipelines/bitbucket-pipelines.yml
T
2024-07-15 14:28:19 +02:00

397 lines
18 KiB
YAML

definitions:
scripts:
- script: &aws-context-dev
export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE;
export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token;
echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token;
export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId');
export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey');
export AWS_SECURITY_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken');
export AWS_REGION='us-east-2';
- script: &aws-context-uat
export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO_UAT:role/$OIDC_ROLE_UAT;
export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token;
echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token;
export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId');
export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey');
export AWS_SECURITY_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken');
export AWS_REGION='us-east-2';
- script: &aws-context-prod
export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO_PROD:role/$OIDC_ROLE_PROD;
export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token;
echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token;
export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId');
export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey');
export AWS_SECURITY_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken');
export AWS_REGION='us-east-2';
- script: &ssm-send-command-dev
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids $DEV_INSTANCE_ID_TF --region $AWS_DEFAULT_REGION --parameters commands='[
"sudo -u ubuntu -i <<EOF",
"export HOME=/home/ubuntu",
"git config --global --add safe.directory /home/ubuntu/doczy.ai",
"if [ -d /home/ubuntu/doczy.ai ]; then cd /home/ubuntu/doczy.ai && git fetch && git pull; else cd /home/ubuntu && git clone git@bitbucket.org:aarete/doczy.ai.git; fi",
"EOF"
]'
- script: &ssm-send-command-uat
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids $UAT_INSTANCE_ID_TF --region $AWS_DEFAULT_REGION --parameters commands='[
"sudo -u ubuntu -i <<EOF",
"export HOME=/home/ubuntu",
"git config --global --add safe.directory /home/ubuntu/doczy.ai",
"if [ -d /home/ubuntu/doczy.ai ]; then cd /home/ubuntu/doczy.ai && git fetch && git pull; else cd /home/ubuntu && git clone git@bitbucket.org:aarete/doczy.ai.git; fi",
"EOF"
]'
- script: &ssm-send-command-prod
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids $PROD_INSTANCE_ID_TF --region $AWS_DEFAULT_REGION --parameters commands='[
"sudo -u ubuntu -i <<EOF",
"export HOME=/home/ubuntu",
"git config --global --add safe.directory /home/ubuntu/doczy.ai",
"if [ -d /home/ubuntu/doczy.ai ]; then cd /home/ubuntu/doczy.ai && git fetch && git pull; else cd /home/ubuntu && git clone git@bitbucket.org:aarete/doczy.ai.git; fi",
"EOF"
]'
- step: &snowflake_deploy
name: "Deploy Snowflake Changes"
image: python:3.7
size: 2x
script:
- python -m pip install --upgrade pip
- pip install snowflake-connector-python
- pip install schemachange --upgrade
# Setting the database name dynamically based on the branch name
- if [ "$BITBUCKET_BRANCH" == "DEV" ]; then export SNOWFLAKE_DATABASE=$DEV_SNOWFLAKE_DATABASE; export SNOWFLAKE_ROLE=$DEV_SNOWFLAKE_ROLE; fi
- if [ "$BITBUCKET_BRANCH" == "UAT" ]; then export SNOWFLAKE_DATABASE=$UAT_SNOWFLAKE_DATABASE; export SNOWFLAKE_ROLE=$UAT_SNOWFLAKE_ROLE; fi
- if [ "$BITBUCKET_BRANCH" == "PROD" ]; then export SNOWFLAKE_DATABASE=$PROD_SNOWFLAKE_DATABASE; export SNOWFLAKE_ROLE=$PROD_SNOWFLAKE_ROLE; fi
- schemachange -a $SNOWFLAKE_ACCOUNT -u $SNOWFLAKE_USER -r $SNOWFLAKE_ROLE -w $SNOWFLAKE_WAREHOUSE -d $SNOWFLAKE_DATABASE -c $SNOWFLAKE_DATABASE.SCHEMACHANGE.CHANGE_HISTORY --create-change-history-table -v
trigger: automatic
# trigger: manual
caches:
- pip
- step: &streamlit_deploy
name: "Deploy streamlit to EC2"
oidc: true
image: python:3.8
script:
- python -m pip install --upgrade pip
- apt-get update && apt-get install -y jq git
- pip install awscli
- export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE
- export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
- echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
- export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600)
- export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId')
- export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey')
- export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken')
- aws sts get-caller-identity
- aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids $DEV_INSTANCE_ID --region $AWS_DEFAULT_REGION --parameters commands='["cd /home/ubuntu/streamlit","su -l ubuntu","(if [ -d doczy.ai ]; then cd doczy.ai && git fetch && git pull; else git clone git@bitbucket.org:aarete/doczy.ai.git; fi)"]'
trigger: automatic # uncomment after testing
- step: &airflow_dags_deploy
name: "Deploy Airflow DAGs to S3 bucket"
image: python:3.8
oidc: true
script:
- python -m pip install --upgrade pip
- apt-get update && apt-get install -y jq git
- pip install awscli
- export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE
- export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
- echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
- export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600)
- export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId')
- export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey')
- export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken')
- aws sts get-caller-identity
- aws s3 sync ./airflow/dags s3://doczy-dev-infra-mwaa-resources/dags
trigger: automatic # uncomment after testing
pipelines:
branches:
DEV:
- step:
<<: *snowflake_deploy
condition:
changesets:
includePaths:
- snowflake/DEV/*
- snowflake/DEV/**/*
- snowflake/DEV/**/**/*
- step:
image: python:3.8
name: "Deploy streamlit to EC2"
oidc: true
script:
- python -m pip install --upgrade pip
- apt-get update && apt-get install -y jq git
- pip install awscli
- *aws-context-dev
- *ssm-send-command-dev
condition:
changesets:
includePaths:
- streamlit/*
- streamlit/**/*
- step:
<<: *airflow_dags_deploy
condition:
changesets:
includePaths:
- airflow/dags/*
- step:
name: Apply textract-pipeline on DEV
image: hugree/terraform-with-snowsql:latest
oidc: true
script:
- *aws-context-dev
- pip install -r requirements.txt
- python terraform.py apply --environment=dev --module=textract-pipeline/terraform
- step:
name: Apply devops-pipeline on DEV
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-dev
- pip install -r requirements.txt
- python terraform.py apply --environment=dev --module=devops-pipeline/other-resources
- step:
name: Apply streamlit-server on DEV
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-dev
- pip install -r requirements.txt
- python terraform.py apply --environment=dev --module=streamlit-server
# UAT and PROD pipelines will be updated with Streamlit steps once it is tested and ready
UAT:
- step:
name: Apply textract-pipeline on UAT
image: hugree/terraform-with-snowsql:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- python terraform.py apply --environment=uat --module=textract-pipeline/terraform
- step:
name: Apply devops-pipeline on UAT
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- python terraform.py apply --environment=uat --module=devops-pipeline/other-resources
- step:
name: Apply streamlit-server on UAT
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- python terraform.py apply --environment=uat --module=streamlit-server
- step:
<<: *snowflake_deploy
condition:
changesets:
includePaths:
- snowflake/UAT/*
- snowflake/UAT/**/*
- snowflake/UAT/**/**/*
- step:
image: python:3.8
name: "Deploy streamlit to EC2"
oidc: true
trigger: automatic # Keeping it manual as the server will be created first and then referenced in this
script:
- python -m pip install --upgrade pip
- apt-get update && apt-get install -y jq git
- pip install awscli
- *aws-context-uat
- *ssm-send-command-uat
condition:
changesets:
includePaths:
- streamlit/*
- streamlit/**/*
- step:
<<: *airflow_dags_deploy
trigger: manual
condition:
changesets:
includePaths:
- airflow/dags/*
PROD:
- step:
name: Apply devops-pipeline on PROD
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-prod
- aws sts get-caller-identity --output text
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="PROD" python terraform.py apply --environment=prod --module=devops-pipeline/other-resources
- step:
name: Apply streamlit-server on PROD
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-prod
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="PROD" python terraform.py apply --environment=prod --module=streamlit-server
- step:
name: Apply textract-pipeline on PROD
image: hugree/terraform-with-snowsql:latest
oidc: true
script:
- *aws-context-prod
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="PROD" python terraform.py apply --environment=prod --module=textract-pipeline/terraform
- step:
<<: *snowflake_deploy
condition:
changesets:
includePaths:
- snowflake/PROD/*
- snowflake/PROD/**/*
- snowflake/PROD/**/**/*
- step:
image: python:3.8
name: "Deploy streamlit to EC2"
oidc: true
trigger: automatic # Keeping it manual as the server will be created first and then referenced in this
script:
- python -m pip install --upgrade pip
- apt-get update && apt-get install -y jq git
- pip install awscli
- *aws-context-prod
- *ssm-send-command-prod
condition:
changesets:
includePaths:
- streamlit/*
- streamlit/**/*
- step:
<<: *airflow_dags_deploy
trigger: manual
condition:
changesets:
includePaths:
- airflow/dags/*
feature/DOC-595-cicd-for-prod:
- parallel:
# - step:
# name: Plan devops-pipeline on PROD
# image: hugree/bitbucket-aws-python38-tf154:latest
# oidc: true
# script:
# - *aws-context-prod
# - aws sts get-caller-identity --output text
# - pip install -r requirements.txt
# - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=prod --module=devops-pipeline/other-resources
# - step:
# name: Plan streamlit-server on PROD
# image: hugree/bitbucket-aws-python38-tf154:latest
# oidc: true
# script:
# - *aws-context-prod
# - pip install -r requirements.txt
# - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=prod --module=streamlit-server
# - step:
# name: Plan textract-pipeline on PROD
# image: hugree/terraform-with-snowsql:latest
# oidc: true
# script:
# - *aws-context-prod
# - pip install -r requirements.txt
# - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=prod --module=textract-pipeline/terraform
- step:
name: Apply textract-pipeline on UAT
image: hugree/terraform-with-snowsql:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="DEV" python terraform.py apply --environment=uat --module=textract-pipeline/terraform
feature/DOC-536-smart-git-diff:
- parallel:
- step:
name: Plan devops-pipeline on DEV
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-dev
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=devops-pipeline/other-resources
- step:
name: Plan streamlit-server on DEV
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-dev
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=streamlit-server
- step:
name: Plan textract-pipeline on DEV
image: hugree/terraform-with-snowsql:latest
oidc: true
script:
- *aws-context-dev
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=textract-pipeline/terraform
- step:
name: Plan devops-pipeline on UAT
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=devops-pipeline/other-resources
- step:
name: Plan streamlit-server on UAT
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=streamlit-server
- step:
name: Plan textract-pipeline on UAT
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=textract-pipeline/terraform
feature/DOC-535-snowsql-in-cicd:
- parallel:
- step:
image: hugree/terraform-with-snowsql:latest
name: Run Snowsql on DEV
oidc: true
script:
- *aws-context-dev
- pip install -r requirements.txt
- IS_DEPLOY_ALL=true python terraform.py apply --environment=dev --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create"
- step:
image: hugree/terraform-with-snowsql:latest
name: Run Snowsql on UAT
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- IS_DEPLOY_ALL=true python terraform.py apply --environment=uat --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create"
feature/UAT-pdf-layer-test:
- step:
name: Apply textract-pipeline on UAT (pdf layer)
image: hugree/bitbucket-aws-python38-tf154:latest
oidc: true
script:
- *aws-context-uat
- pip install -r requirements.txt
- BB_PIPELINE_BRANCH="UAT" python terraform.py apply --environment=uat --module=textract-pipeline/terraform --apply-extra-args="-target=module.pdf_layer"