- skeleton, 즉 뼈대만 갖춘 파일로
stage들을 정의만 했다.
- 도커 이미지, aws ci user, 환경변수, job 관련 script는 정의하지 않은 상태
- script 는 echo "~~" 이 수준으로 skeleton 작업했다는 말
ex)
Test and Lint:
image: docker:20.10.0
services:
- docker:20.10.0-dind
stage: Test and Lint
script:
- apk add python3-dev py-pip libffi-dev openssl-dev gcc libc-dev make
- pip3 install -I docker-compose==1.26.2
- docker-compose run --rm app sh -c "python manage.py wait_for_db && python manage.py test && flake8"
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^(master|production)$/ || $CI_COMMIT_BRANCH =~ /^(master|production)$/'
pull request가 트리거로 작용
(= .gitlab.yml파일에 정의하였으므로)
test와 lint stage가 진행됐고 아래와 같이 통과됐다.
마스터에 merge와 관련된 stage별 job들이 실행되고 있다.
즉, pipeline이 구축되고 잘 흘러가고 있는 것을 확인하고 있는 중이다.
stages pipeline 작업이 완료됐다.
ec2 인스턴스가 종료됐는지 aws 콘솔을 확인해보자.
작동중인 인스턴스가 없음을 확인할 수 있다.
git checkout master
git pull origin master
일반적으로 위와 같은 상황에서 시작할 것이다.
git checkout production
git merge master
git push origin
destroy pipeline 까지 진행하자.
이로써 모든 인스턴스가 종료됐다.
image:
name: hashicorp/terraform:0.14.3
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
stages:
- Test and Lint
- Build and Push
- Staging Plan
- Staging Apply
- Production Plan
- Production Apply
- Destroy
Test and Lint:
image: docker:20.10.0
services:
- docker:20.10.0-dind
stage: Test and Lint
script:
- apk add python3-dev py-pip libffi-dev openssl-dev gcc libc-dev make
- pip3 install -I docker-compose==1.26.2
- docker-compose run --rm app sh -c "python manage.py wait_for_db && python manage.py test && flake8"
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^(master|production)$/ || $CI_COMMIT_BRANCH =~ /^(master|production)$/'
Validate Terraform:
stage: Test and Lint
script:
- cd deploy/
- terraform init -backend=false
- terraform validate
- terraform fmt -check
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^(master|production)$/ || $CI_COMMIT_BRANCH =~ /^(master|production)$/'
Build and Push:
image: docker:20.10.0
services:
- docker:20.10.0-dind
stage: Build and Push
script:
- apk add python3 py-pip
- pip3 install awscli==1.18.8
- docker build --compress -t $ECR_REPO:$CI_COMMIT_SHORT_SHA .
- $(aws ecr get-login --no-include-email --region us-east-1)
- docker push $ECR_REPO:$CI_COMMIT_SHORT_SHA
- docker tag $ECR_REPO:$CI_COMMIT_SHORT_SHA $ECR_REPO:latest
- docker push $ECR_REPO:latest
rules:
- if: '$CI_COMMIT_BRANCH =~ /^(master|production)$/'
Staging Plan:
stage: Staging Plan
script:
- cd deploy/
- export TF_VAR_ecr_image_api=$ECR_REPO:$CI_COMMIT_SHORT_SHA
- terraform init
- terraform workspace select staging || terraform workspace new staging
- terraform plan
rules:
- if: '$CI_COMMIT_BRANCH =~ /^(master|production)$/'
Staging Apply:
stage: Staging Apply
script:
- cd deploy/
- export TF_VAR_ecr_image=$ECR_REPO:$CI_COMMIT_SHORT_SHA
- terraform init
- terraform workspace select staging
- terraform apply -auto-approve
rules:
- if: '$CI_COMMIT_BRANCH =~ /^(master|production)$/'
Production Plan:
stage: Production Plan
script:
- cd deploy/
- export TF_VAR_ecr_image=$ECR_REPO:$CI_COMMIT_SHORT_SHA
- terraform init
- terraform workspace select production || terraform workspace new production
- terraform plan
rules:
- if: '$CI_COMMIT_BRANCH == "production"'
Production Apply:
stage: Production Apply
script:
- cd deploy/
- export TF_VAR_ecr_image=$ECR_REPO:$CI_COMMIT_SHORT_SHA
- terraform init
- terraform workspace select production
- terraform apply -auto-approve
rules:
- if: '$CI_COMMIT_BRANCH == "production"'
Staging Destroy:
stage: Destroy
script:
- cd deploy/
- terraform init
- terraform workspace select staging
- terraform destroy -auto-approve
rules:
- if: '$CI_COMMIT_BRANCH =~ /^(master|production)$/'
when: manual
Production Destroy:
stage: Destroy
script:
- cd deploy/
- terraform init
- terraform workspace select production
- terraform destroy -auto-approve
rules:
- if: '$CI_COMMIT_BRANCH == "production"'
when: manual
내부적으로 awscli가 job을 실행할 때
어떤 파이썬 실행문을 작동하는지는 파악하지 못했지만....
도커 이미지에 python, pip 그리고 awscli까지 설치는 진행했다.