diff --git a/.circleci/config.yml b/.circleci/config.yml index 43ffd03..da93e93 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,6 +23,6 @@ workflows: - orb-tools/continue: pipeline_number: << pipeline.number >> vcs_type: << pipeline.project.type >> - orb_name: aws-codeDeploy-status + orb_name: aws-code-deploy requires: [orb-tools/lint, orb-tools/pack, orb-tools/review, shellcheck/check] filters: *filters diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 8b2850c..7653499 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -4,7 +4,7 @@ orbs: # Reference your orb's jobs and commands below as they will exist when built. orb-tools: circleci/orb-tools@12.0 # The orb definition is intentionally not included here. It will be injected into the pipeline. - aws-codeDeploy-status: {} + aws-code-deploy: sidlinux22/aws-code-deploy@0.0.1 # Use this tag to ensure test jobs always run, # even though the downstream publish job will only run on release tags. @@ -28,7 +28,17 @@ jobs: steps: - checkout # Run your orb's commands to validate them. - - aws-codeDeploy-status/get-deployment-id + - aws-code-deploy/get_deployment_id: + application_name: "change_to_codeDeploy_application_name" + deployment_group_name: "change_to_codeDeploy_deployment_group_name" + # Get the latest deployment ID for the specified application and deployment group + # Add CodeDeploy Steps like copying Artifcate to S3 etc... + # Fetch new CodeDeploy deployment ID status. + - aws-code-deploy/get_deployment_status: + application_name: "change_to_codeDeploy_application_name" + deployment_group_name: "change_to_codeDeploy_deployment_group_name" + deployment_start_timeout: 300 + deployment_completion_timeout: 600 workflows: test-deploy: jobs: @@ -40,7 +50,7 @@ workflows: - orb-tools/pack: filters: *release-filters - orb-tools/publish: - orb_name: sidlinux22/aws-codeDeploy-status + orb_name: sidlinux22/aws-code-deploy vcs_type: << pipeline.project.type >> pub_type: production # Ensure this job requires all test jobs and the pack job. diff --git a/README.md b/README.md index 61c08cb..b588aeb 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,84 @@ -# Orb Template - +# CircleCi Orbs [sidlinux22/aws-code-deploy](https://circleci.com/developer/orbs/orb/sidlinux22/aws-code-deploy) [![CircleCI Build Status](https://circleci.com/gh/sidlinux22/aws-codeDeploy-orb.svg?style=shield "CircleCI Build Status")](https://circleci.com/gh/sidlinux22/aws-codeDeploy-orb) [![CircleCI Orb Version](https://badges.circleci.com/orbs/sidlinux22/aws-codeDeploy-orb.svg)](https://circleci.com/developer/orbs/orb/sidlinux22/aws-codeDeploy-orb) [![GitHub License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/sidlinux22/aws-codeDeploy-orb/master/LICENSE) [![CircleCI Community](https://img.shields.io/badge/community-CircleCI%20Discuss-343434.svg)](https://discuss.circleci.com/c/ecosystem/orbs) +This CircleCi Orb is designed to interact with AWS CodeDeploy and retrieve deployment-related information. It has several functions to perform different tasks related to CodeDeploy deployments. + +## Script Components + +1. Import Statements: + - The script imports necessary modules such as `os`, `sys`, `boto3`, `time`, `logging`, and `ThreadPoolExecutor` from the `concurrent.futures` module. + +2. Logging Configuration: + - The logging module is configured to log messages with a specific format and level. + +3. `fetch_target_deployment` Function: + - This function fetches information about a target deployment in CodeDeploy. + - It takes the `application_name`, `deployment_group_name`, and `pre_deploy_id` as input parameters. + - It uses the `boto3` client for CodeDeploy to retrieve the deployment information. + - The function waits for the deployment to start and checks for any errors or timeouts. + - It returns the `deploy_id` and `instance_ids` associated with the deployment. + +4. `fetch_target_status` Function: + - This function retrieves the status of a specific target instance in a CodeDeploy deployment. + - It takes the `deploy_id` and `target_id` as input parameters. + - It uses the `boto3` client for CodeDeploy to fetch the target's status. + - It returns the `target_id` and `status` of the target instance. -A project template for Orbs. +5. `fetch_code_deploy_status` Function: + - This function fetches the status of all target instances in a CodeDeploy deployment. + - It calls the `fetch_target_deployment` function to get the `deploy_id` and `instance_ids`. + - It continuously checks the status of each instance in the deployment until completion. + - It counts the instances in different status categories such as completed, in progress, failed, stopped, pending, ready, and skipped. + - It checks for various conditions such as completion, failure, timeouts, and returns appropriate exit codes. -This repository is designed to be automatically ingested and modified by the CircleCI CLI's `orb init` command. +6. `fetch_deployment_id` Function: + - This function retrieves the deployment ID for a specified application and deployment group. + - It takes the `application_name` and `deployment_group_name` as input parameters. + - It uses the `boto3` client for CodeDeploy to fetch the deployment ID. + - It prints the deployment ID and logs the information. -_**Edit this area to include a custom title and description.**_ +7. Main Execution Block: + - The script checks the command-line arguments provided and validates them. + - It determines the appropriate action based on the provided flag (`--get-deployment-status` or `--get-deployment-id`). + - It retrieves the `application_name` and `deployment_group_name` from the command-line arguments. + - If the flag is `--get-deployment-status`, it + + fetches the `pre_deploy_id` and calls the `fetch_code_deploy_status` function. + - If the flag is `--get-deployment-id`, it calls the `fetch_deployment_id` function. + - It handles any errors that occur during execution and logs them. + - It exits with an appropriate exit code based on the execution result. + +This script is designed to be run from the command line and provides two main functionalities: fetching the status of a CodeDeploy deployment and retrieving the deployment ID. It utilizes the `boto3` library to interact with AWS CodeDeploy and requires AWS credentials to be set up properly. + +Please note that certain values in the script, such as `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_REGION`, are expected to be provided as environment variables. Additionally, when running the script, specific command-line arguments are required depending on the desired functionality. + +For detailed usage instructions, you can refer to the comments in the `__main__` block of the script, which provide usage examples for each flag. --- ## Resources -[CircleCI Orb Registry Page](https://circleci.com/developer/orbs/orb/sidlinux22/aws-codeDeploy-orb) - The official registry page of this orb for all versions, executors, commands, and jobs described. - -[CircleCI Orb Docs](https://circleci.com/docs/orb-intro/#section=configuration) - Docs for using, creating, and publishing CircleCI Orbs. +- [CircleCI Orb Registry Page](https://circleci.com/developer/orbs/orb/sidlinux22/aws-code-deploy) - The official registry page of this orb for all versions, executors, commands, and jobs described. +- [CircleCI Orb Docs](https://circleci.com/docs/orb-intro/#section=configuration) - Docs for using, creating, and publishing CircleCI Orbs. ### How to Contribute -We welcome [issues](https://github.com/sidlinux22/aws-codeDeploy-orb/issues) to and [pull requests](https://github.com/sidlinux22/aws-codeDeploy-orb/pulls) against this repository! +We welcome [issues](https://github.com/sidlinux22/aws-codeDeploy-orb/issues) and [pull requests](https://github.com/sidlinux22/aws-codeDeploy-orb/pulls) to this repository! ### How to Publish An Update + 1. Merge pull requests with desired changes to the main branch. - For the best experience, squash-and-merge and use [Conventional Commit Messages](https://conventionalcommits.org/). 2. Find the current version of the orb. - You can run `circleci orb info sidlinux22/aws-code-deploy | grep "Latest"` to see the current version. -3. Create a [new Release](https://github.com/sidlinux22/aws-codeDeploy-orb/releases/new) on GitHub. - - Click "Choose a tag" and _create_ a new [semantically versioned](http://semver.org/) tag. (ex: v1.0.0) +3. Create a [new Release](https://github.com/sidlinux22/aws-code-deploy/releases/new) on GitHub. + - Click "Choose a tag" and create a new [semantically versioned](http://semver.org/) tag (e.g., v1.0.0). - We will have an opportunity to change this before we publish if needed after the next step. -4. Click _"+ Auto-generate release notes"_. - - This will create a summary of all of the merged pull requests since the previous release. - - If you have used _[Conventional Commit Messages](https://conventionalcommits.org/)_ it will be easy to determine what types of changes were made, allowing you to ensure the correct version tag is being published. -5. Now ensure the version tag selected is semantically accurate based on the changes included. -6. Click _"Publish Release"_. - - This will push a new tag and trigger your publishing pipeline on CircleCI. \ No newline at end of file +4. Click "+ Auto-generate release notes". + - This will create a summary of all the merged pull requests since the previous release. + - If you have used [Conventional Commit Messages](https://conventionalcommits.org/), it will be easy to determine what types of changes were made, allowing you to ensure the correct version tag is being published. +5. Ensure the selected version tag is semantically accurate based on the included changes. +6. Click "Publish Release". + - This will push a new tag and trigger your publishing pipeline on CircleCI. diff --git a/src/@orb.yml b/src/@orb.yml index 9cd404c..ca07c87 100755 --- a/src/@orb.yml +++ b/src/@orb.yml @@ -1,12 +1,12 @@ version: 2.1 description: > - Sample orb description + This CircleCi orb is designed to interact with AWS CodeDeploy and retrieve deployment-related information. It has several functions to perform different tasks related to CodeDeploy deployments. # This information will be displayed in the orb registry and is not mandatory. display: home_url: "https://github.com/sidlinux22/aws-codeDeploy-orb/tree/main#readme" source_url: "https://github.com/sidlinux22/aws-codeDeploy-orb" -# If your orb requires other orbs, you can import them like this. Otherwise remove the "orbs" stanza. -# orbs: -# hello: circleci/hello-build@0.0.5 +# Orb requires other orbs, you can import them like this. +orbs: + aws-cli: circleci/aws-cli@4.0.0 diff --git a/src/commands/get-deployment-id.yml b/src/commands/get_deployment_id.yml similarity index 57% rename from src/commands/get-deployment-id.yml rename to src/commands/get_deployment_id.yml index b647cef..c338616 100755 --- a/src/commands/get-deployment-id.yml +++ b/src/commands/get_deployment_id.yml @@ -1,16 +1,16 @@ description: > This command is to get CodeDeploy latest deployment. parameters: - application-name: + application_name: type: string description: "The name of the CodeDeploy application." - deployment-group-name: + deployment_group_name: type: string description: "The name of the CodeDeploy deployment group." steps: - run: environment: - APP_NAME: <> - DEPLOY_GROUP_NAME: <> - name: CodeDeploy - Get the latest Deployment ID for <> - command: <> \ No newline at end of file + APP_NAME: <> + DEPLOY_GROUP_NAME: <> + name: CodeDeploy - Get the latest Deployment ID for <> + command: <> diff --git a/src/commands/get-deployment-status.yml b/src/commands/get_deployment_status.yml similarity index 52% rename from src/commands/get-deployment-status.yml rename to src/commands/get_deployment_status.yml index a026414..466e889 100644 --- a/src/commands/get-deployment-status.yml +++ b/src/commands/get_deployment_status.yml @@ -1,30 +1,30 @@ description: > This command is to get CodeDeploy latest deployment. parameters: - application-name: + application_name: type: string description: "The name of the CodeDeploy application." - deployment-group-name: + deployment_group_name: type: string description: "The name of the CodeDeploy deployment group." - deployment-start-timeout: + deployment_start_timeout: type: integer default: 180 description: "Timeout for deployment start." - deployment-completion-timeout: + deployment_completion_timeout: type: integer default: 600 description: "Timeout for deployment completion." - pre-deployment-id: + pre_deployment_id: default: '' type: string description: "Latest previous deployment ID." steps: - run: environment: - APP_NAME: <> - DEPLOY_GROUP_NAME: <> - DEPLOYMENT_START_TIMEOUT: <> - DEPLOYMENT_COMPLETION_TIMEOUT: <> - name: CodeDeploy - Fetch <> deployment status. - command: <> \ No newline at end of file + APP_NAME: <> + DEPLOY_GROUP_NAME: <> + DEPLOYMENT_START_TIMEOUT: <> + DEPLOYMENT_COMPLETION_TIMEOUT: <> + name: CodeDeploy - Fetch <> deployment status. + command: <> diff --git a/src/examples/example.yml b/src/examples/example.yml index fbcacd4..166b035 100755 --- a/src/examples/example.yml +++ b/src/examples/example.yml @@ -24,17 +24,17 @@ usage: region: AWS_REGION # Provide AWS access key, secret access key, and region as environment variables # Fetch latest deployment ID before new deployment start. - - aws-code-deploy/get-deployment-id: - application-name: "change_to_codeDeploy_application-name" - deployment-group-name: "change_to_codeDeploy_deployment-group-name" + - aws-code-deploy/get_deployment_id: + application_name: "change_to_codeDeploy_application_name" + deployment_group_name: "change_to_codeDeploy_deployment_group_name" # Get the latest deployment ID for the specified application and deployment group # Add CodeDeploy Steps like copying Artifcate to S3 etc... # Fetch new CodeDeploy deployment ID status. - - aws-code-deploy/get-deployment-status: - application-name: "change_to_codeDeploy_application-name" - deployment-group-name: "change_to_codeDeploy_deployment-group-name" - deployment-start-timeout: "Optional_change_to_codeDeploy_deployment-start-timeout" - deployment-completion-timeout: "Optional_change_to_codeDeploy_deployment-completion-timeout" + - aws-code-deploy/get_deployment_status: + application_name: "change_to_codeDeploy_application_name" + deployment_group_name: "change_to_codeDeploy_deployment_group_name" + deployment_start_timeout: "Optional_change_to_codeDeploy_deployment_start_timeout" + deployment_completion_timeout: "Optional_change_to_codeDeploy_deployment_completion_timeout" # Get the status of the new CodeDeploy deployment using the latest deployment ID workflows: version: 2 @@ -44,4 +44,4 @@ usage: filters: branches: ignore: /develop|master/ - # Specify branch filters for the workflow execution \ No newline at end of file + # Specify branch filters for the workflow execution diff --git a/src/executors/default.yml b/src/executors/default.yml index fc8b47a..5263f8d 100755 --- a/src/executors/default.yml +++ b/src/executors/default.yml @@ -14,4 +14,4 @@ parameters: docker: - image: cimg/aws:<> -resource_class: <> \ No newline at end of file +resource_class: <> diff --git a/src/jobs/get-deployment-id.yml b/src/jobs/get_deployment_id.yml similarity index 71% rename from src/jobs/get-deployment-id.yml rename to src/jobs/get_deployment_id.yml index 8c5c791..865eb2c 100755 --- a/src/jobs/get-deployment-id.yml +++ b/src/jobs/get_deployment_id.yml @@ -1,11 +1,11 @@ description: > A job to get CodeDeploy latest deployment. parameters: - application-name: + application_name: type: string default: '' description: "The name of the CodeDeploy application." - deployment-group-name: + deployment_group_name: type: string default: '' description: "The name of the CodeDeploy deployment group." @@ -15,6 +15,6 @@ parameters: default: default executor: << parameters.executor >> steps: - - get-deployment-id: - application-name: << parameters.application-name >> - deployment-group-name: << parameters.deployment-group-name>> \ No newline at end of file + - get_deployment_id: + application_name: << parameters.application_name >> + deployment_group_name: << parameters.deployment_group_name>> diff --git a/src/jobs/get-deployment-status.yml b/src/jobs/get_deployment_status.yml similarity index 71% rename from src/jobs/get-deployment-status.yml rename to src/jobs/get_deployment_status.yml index 2d9da12..868677e 100644 --- a/src/jobs/get-deployment-status.yml +++ b/src/jobs/get_deployment_status.yml @@ -1,19 +1,19 @@ description: > A job to get CodeDeploy latest deployment. parameters: - application-name: + application_name: type: string default: '' description: "The name of the CodeDeploy application." - deployment-group-name: + deployment_group_name: type: string default: '' description: "The name of the CodeDeploy deployment group." - deployment-start-timeout: + deployment_start_timeout: type: integer default: 180 description: "Timeout for deployment start." - deployment-completion-timeout: + deployment_completion_timeout: type: integer default: 600 description: "Timeout for deployment completion." @@ -23,6 +23,6 @@ parameters: default: default executor: << parameters.executor >> steps: - - get-deployment-status: - application-name: << parameters.application-name >> - deployment-group-name: << parameters.deployment-group-name>> \ No newline at end of file + - get_deployment_status: + application_name: << parameters.application_name >> + deployment_group_name: << parameters.deployment_group_name>> diff --git a/src/scripts/aws-codeDeploy-orb.py b/src/scripts/aws_codeDeploy_orb.py similarity index 98% rename from src/scripts/aws-codeDeploy-orb.py rename to src/scripts/aws_codeDeploy_orb.py index ff2c144..20f55e6 100644 --- a/src/scripts/aws-codeDeploy-orb.py +++ b/src/scripts/aws_codeDeploy_orb.py @@ -153,8 +153,8 @@ def fetch_deployment_id(application_name, deployment_group_name): if __name__ == "__main__": try: if len(sys.argv) < 2 or sys.argv[1] not in ["--get-deployment-status", "--get-deployment-id"]: - logger.error("Usage: python script.py --get-deployment-status ") - logger.error(" python script.py --get-deployment-id ") + logger.error("Usage: python script.py --get-deployment-status ") + logger.error(" python script.py --get-deployment-id ") exit(1) flag = sys.argv[1] diff --git a/src/scripts/get-deployment-id.sh b/src/scripts/get_deployment_id.sh similarity index 84% rename from src/scripts/get-deployment-id.sh rename to src/scripts/get_deployment_id.sh index 4786a5f..86fdfef 100644 --- a/src/scripts/get-deployment-id.sh +++ b/src/scripts/get_deployment_id.sh @@ -6,13 +6,13 @@ if ! python3 -c "import boto3" >/dev/null 2>&1; then fi # Define the URL for the Python script -script_url="https://raw.githubusercontent.com/sidlinux22/aws-codeDeploy-orb/dev-0.0.1/src/scripts/aws-codeDeploy-orb.py" +script_url="https://raw.githubusercontent.com/sidlinux22/aws-codeDeploy-orb/dev-0.0.1/src/scripts/aws_codeDeploy_orb.py" # Retrieve Python script from GitHub and save it as a file curl -sSLJO "$script_url" # Run the Python script with the parameters and capture the output -PRE_Deployment_ID=$(python3 aws-codeDeploy-orb.py --get-deployment-id "${APP_NAME}" "${DEPLOY_GROUP_NAME}") +PRE_Deployment_ID=$(python3 aws_codeDeploy_orb.py --get-deployment-id "${APP_NAME}" "${DEPLOY_GROUP_NAME}") # Print the result echo "Latest Deployment ID is $PRE_Deployment_ID" diff --git a/src/scripts/get-deployment-status.sh b/src/scripts/get_deployment_status.sh similarity index 85% rename from src/scripts/get-deployment-status.sh rename to src/scripts/get_deployment_status.sh index 4c68786..4faa652 100644 --- a/src/scripts/get-deployment-status.sh +++ b/src/scripts/get_deployment_status.sh @@ -13,10 +13,10 @@ if [[ -z "${PRE_Deployment_ID}" ]]; then fi # Define the URL for the Python script -script_url="https://raw.githubusercontent.com/sidlinux22/aws-codeDeploy-orb/dev-0.0.1/src/scripts/aws-codeDeploy-orb.py" +script_url="https://raw.githubusercontent.com/sidlinux22/aws-codeDeploy-orb/dev-0.0.1/src/scripts/aws_codeDeploy_orb.py" # Retrieve Python script from GitHub and save it as a file curl -sSLJO "$script_url" # Run the Python script with the parameters and capture the output -python3 aws-codeDeploy-orb.py --get-deployment-status "${APP_NAME}" "${DEPLOY_GROUP_NAME}" "${PRE_Deployment_ID}" \ No newline at end of file +python3 aws_codeDeploy_orb.py --get-deployment-status "${APP_NAME}" "${DEPLOY_GROUP_NAME}" "${PRE_Deployment_ID}" \ No newline at end of file