workflow:
project <---> pipeline
pipeline defines jobs (test, build, deploy)
.circleci/config.yml:
version: 2.1
orbs:
node: circleci/node@4.1
aws-cli: circleci/aws-cli@2.0
aws-ecr: circleci/aws-ecr@7.0.0
aws-ecs: circleci/aws-ecs@2.0.0
jobs:
build-static-files:
docker:
- image: cimg/node:15.1
working_directory: ~/app
steps:
- checkout // checkout source code
- node/install-packages: // compare package.json and restore node_modules if identical
pkg-manager: yarn
- run:
name: Run Build
command: yarn run build
- run:
name: Build Tar
command: tar czf build.tgz ./build ./Dockerfile
- save_cache:
key: build-cache-{{ checksum "build.tgz" }}
paths:
- ~/app/build.tgz
build-push-image:
machine: true
working_directory: ~/app
steps:
- restore_cache:
key: build-cache
- run: tar xzf ~/app/build.tgz && cd build
- aws-cli/install:
override-installed: true
- aws-cli/setup:
aws-region: REGION
aws-access-key-id: ACCESS_KEY
aws-secret-access-key: SECRET_ACCESS_KEY
- run:
name: Stop Running Task
command: |
cicdTaskArn=$(aws ecs list-tasks --cluster my-cicd-cluster --service-name my-cicd-service --output text --query taskArns)
if [[ $cicdTaskArn ]];then
aws ecs stop-task --cluster my-cicd-cluster --task $cicdTaskArn
fi
- aws-ecr/build-and-push-image:
checkout: false
account-url: ACCOUNT_URL
aws-access-key-id: ACCESS_KEY
aws-secret-access-key: SECRET_ACCESS_KEY
no-output-timeout: 20m
repo: hello-world
skip-when-tags-exist: false
region: REGION
tag: 'latest'
- aws-ecs/update-service:
cluster-name: 'my-cicd-cluster'
container-image-name-updates: 'container=CICD,tag=latest'
family: 'CICD'
service-name: 'my-cicd-service'
workflows:
Build and Push:
jobs:
- build-static-files
- build-push-image:
requires:
- build-static-files