|
| 1 | +# ci-cd-pipeline-on-aws-for-python |
| 2 | +**Name:** ci-cd-pipeline-on-aws-for-python |
| 3 | +**Description:** AWS CloudFormation template for CI/CD pipeline on AWS for Python |
| 4 | +**GitHub:** https://github.com/korniichuk/ci-cd-pipeline-on-aws-for-python |
| 5 | + |
| 6 | +## Table of Contents |
| 7 | +* **[Intro](#intro)** |
| 8 | +* **[Create CI/CD pipeline. CLI](#create-cicd-pipeline-cli)** |
| 9 | +* **[Create CI/CD pipeline. Console](#create-cicd-pipeline-console)** |
| 10 | +* **[Validate created pipeline](#validate-created-pipeline)** |
| 11 | + |
| 12 | +## Intro |
| 13 | +This repository includes AWS CloudFormation template for CI/CD pipeline on AWS for Python. CI/CD pipeline based on [AWS CodeCommit](https://aws.amazon.com/codecommit/) (à la GitHub), [AWS CodeBuild](https://aws.amazon.com/codebuild/) (à la Jenkins), and [AWS CodePipeline](https://aws.amazon.com/codepipeline/) (continuous delivery) services. You can see visualization below: |
| 14 | + |
| 15 | + |
| 16 | +CI/CD pipeline validates your Python code against coding style (aka [PEP8](https://www.python.org/dev/peps/pep-0008/)), programming errors, and [cyclomatic complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity). CI/CD pipeline runs your unit tests. Solution based on [Flake8](https://flake8.pycqa.org/en/latest/) and [Pytest](https://docs.pytest.org/en/stable/) libraries. For more details see `buildspec.yml` file in [ci_cd_pipeline_init_code_20200827.zip](https://s3-eu-west-1.amazonaws.com/korniichuk.share/code/ci_cd_pipeline_init_code_20200827.zip) archive. |
| 17 | + |
| 18 | +## Create CI/CD pipeline. CLI |
| 19 | +In a terminal, we can enter the following command: |
| 20 | +``` |
| 21 | +$ aws cloudformation create-stack --stack-name <value> --template-body file://<value> --capabilities CAPABILITY_NAMED_IAM |
| 22 | +``` |
| 23 | + |
| 24 | +Example: |
| 25 | +``` |
| 26 | +$ git clone https://github.com/korniichuk/ci-cd-pipeline-on-aws-for-python.git |
| 27 | +$ cd ci-cd-pipeline-on-aws-for-python |
| 28 | +$ aws cloudformation create-stack --stack-name medium --template-body file://ci_cd_pipeline.yaml --capabilities CAPABILITY_NAMED_IAM |
| 29 | +``` |
| 30 | + |
| 31 | +Example output: |
| 32 | +``` |
| 33 | +{ |
| 34 | + "StackId": "arn:aws:cloudformation:eu-west-1:999999999999:stack/medium/4576d190-e731-11ea-925a-0ab221334260" |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +**Source:** https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html |
| 39 | + |
| 40 | +## Create CI/CD pipeline. Console |
| 41 | +Navigate to https://console.aws.amazon.com/cloudformation/. Click `Create stack`: |
| 42 | + |
| 43 | + |
| 44 | +Select `Upload a template file`. Click `Choose file` and select AWS CloudFormation template from your local machine (e.g. [ci_cd_pipeline.yaml](ci_cd_pipeline.yaml)): |
| 45 | + |
| 46 | + |
| 47 | +Click `Next`: |
| 48 | + |
| 49 | + |
| 50 | +Enter `Stack name` (e.g. `medium`). Click `Next`: |
| 51 | + |
| 52 | + |
| 53 | +Scroll down. Click `Next`: |
| 54 | + |
| 55 | + |
| 56 | +Scroll down. Select `I acknowledge that AWS CloudFormation might create IAM resources with custom names.` checkbox. Click `Create stack`: |
| 57 | + |
| 58 | + |
| 59 | +## Validate created pipeline |
| 60 | +Navigate to https://console.aws.amazon.com/cloudformation/. Click `Stacks` and validate status of your stack: |
| 61 | + |
| 62 | + |
| 63 | +Navigate to https://console.aws.amazon.com/codepipeline/. Validate status of your pipeline: |
| 64 | + |
| 65 | + |
| 66 | +Navigate to https://console.aws.amazon.com/codecommit/. Copy `URL` of your repository: |
| 67 | + |
| 68 | + |
| 69 | +In a terminal, we need to enter the following command: |
| 70 | +``` |
| 71 | +$ git clone URL |
| 72 | +$ cd demo-ci-cd-pipeline |
| 73 | +``` |
| 74 | + |
| 75 | +Where: |
| 76 | + - `URL` -- URL of your repository from the previous step. |
| 77 | + |
| 78 | +Example: |
| 79 | + |
| 80 | +``` |
| 81 | +$ git clone https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/demo-ci-cd-pipeline |
| 82 | +$ cd demo-ci-cd-pipeline |
| 83 | +``` |
| 84 | + |
| 85 | +Next we need to change our Python code and send update to AWS CodeCommit repository. For example, you can change code of `sample.py` file to new code with typo: |
| 86 | +``` |
| 87 | +# -*- coding: utf-8 -*- |
| 88 | +# Name: sample |
| 89 | +# Version: 0.1a2 |
| 90 | +# Owner: Ruslan Korniichuk |
| 91 | +
|
| 92 | +def hello_world(): |
| 93 | + return 'Hello, World1' # Typo is here |
| 94 | +``` |
| 95 | + |
| 96 | +Commit changes and send to remote repo: |
| 97 | +``` |
| 98 | +$ git commit -am "Test failure" |
| 99 | +$ git push origin master |
| 100 | +``` |
| 101 | + |
| 102 | +Navigate to https://console.aws.amazon.com/codepipeline/. **Wait a few minutes.** You can expect `In progress` status first: |
| 103 | + |
| 104 | + |
| 105 | +Wait a few minutes more. You can expect `Failed` status: |
| 106 | + |
| 107 | + |
| 108 | +Select your pipeline and click `Details` in red `Build` block: |
| 109 | + |
| 110 | + |
| 111 | +Click `Link to execution details`: |
| 112 | + |
| 113 | + |
| 114 | +Finally, you can see reason of failure. Our new Python code with typo cannot pass unit tests: |
| 115 | + |
0 commit comments