|
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - content |
| 8 | + |
| 9 | +env: |
| 10 | + ECR_REPOSITORY: beans-api |
| 11 | + TASK_DEFINITION: beans-api |
| 12 | + CONTAINER_NAME: beans-api |
| 13 | + ECS_SERVICE: beans-api-service |
| 14 | + ECS_CLUSTER: beans-api-cluster |
| 15 | + |
| 16 | +jobs: |
| 17 | + lint: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@v4 |
| 21 | + - uses: actions/setup-python@v5 |
| 22 | + with: |
| 23 | + python-version: "3.12" |
| 24 | + - run: pip install ruff |
| 25 | + - run: ruff check module-19-ci-cd/solution/main.py module-19-ci-cd/solution/quality_check.py |
| 26 | + |
| 27 | + quality-check: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + needs: lint |
| 30 | + steps: |
| 31 | + - uses: actions/checkout@v4 |
| 32 | + - uses: actions/setup-python@v5 |
| 33 | + with: |
| 34 | + python-version: "3.12" |
| 35 | + - uses: actions/cache@v4 |
| 36 | + with: |
| 37 | + path: ~/.cache/pip |
| 38 | + key: pip-deepchecks-${{ hashFiles('module-19-ci-cd/solution/requirements-quality.txt') }} |
| 39 | + - uses: actions/cache@v4 |
| 40 | + with: |
| 41 | + path: ~/.cache/huggingface |
| 42 | + key: hf-beans-vit |
| 43 | + - run: pip install -r module-19-ci-cd/solution/requirements-quality.txt |
| 44 | + - name: Run Deepchecks quality gate |
| 45 | + env: |
| 46 | + TOKENIZERS_PARALLELISM: "false" |
| 47 | + run: python module-19-ci-cd/solution/quality_check.py |
| 48 | + |
| 49 | + build: |
| 50 | + runs-on: ubuntu-latest |
| 51 | + needs: [lint, quality-check] |
| 52 | + steps: |
| 53 | + - uses: actions/checkout@v4 |
| 54 | + - name: Configure AWS credentials |
| 55 | + uses: aws-actions/configure-aws-credentials@v4 |
| 56 | + with: |
| 57 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 58 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 59 | + aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }} |
| 60 | + aws-region: ${{ secrets.AWS_REGION }} |
| 61 | + - name: Login to ECR |
| 62 | + uses: aws-actions/amazon-ecr-login@v2 |
| 63 | + - name: Build and push image |
| 64 | + env: |
| 65 | + ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }} |
| 66 | + run: | |
| 67 | + IMAGE_TAG="${GITHUB_SHA:0:8}-$(date +%s)" |
| 68 | + IMAGE_URI="$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" |
| 69 | + docker build -t $IMAGE_URI module-19-ci-cd/solution |
| 70 | + docker push $IMAGE_URI |
| 71 | + docker tag $IMAGE_URI $ECR_REGISTRY/$ECR_REPOSITORY:latest |
| 72 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest |
| 73 | +
|
| 74 | + deploy: |
| 75 | + runs-on: ubuntu-latest |
| 76 | + needs: build |
| 77 | + steps: |
| 78 | + - name: Configure AWS credentials |
| 79 | + uses: aws-actions/configure-aws-credentials@v4 |
| 80 | + with: |
| 81 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 82 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 83 | + aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }} |
| 84 | + aws-region: ${{ secrets.AWS_REGION }} |
| 85 | + - name: Download current task definition |
| 86 | + run: | |
| 87 | + aws ecs describe-task-definition \ |
| 88 | + --task-definition ${{ env.TASK_DEFINITION }} \ |
| 89 | + --query taskDefinition > task-definition.json |
| 90 | + - name: Render ECS task definition |
| 91 | + id: task-def |
| 92 | + uses: aws-actions/amazon-ecs-render-task-definition@v1 |
| 93 | + with: |
| 94 | + task-definition: task-definition.json |
| 95 | + container-name: ${{ env.CONTAINER_NAME }} |
| 96 | + image: ${{ secrets.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest |
| 97 | + - name: Deploy to ECS |
| 98 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v2 |
| 99 | + with: |
| 100 | + task-definition: ${{ steps.task-def.outputs.task-definition }} |
| 101 | + service: ${{ env.ECS_SERVICE }} |
| 102 | + cluster: ${{ env.ECS_CLUSTER }} |
| 103 | + wait-for-service-stability: true |
0 commit comments