version: 2.1 # --- Constants --- # globals: images: node: &node cimg/node:lts postgres: &postgres circleci/postgres:9.6.5 caches: dependencies: &dependencies dependencies-{{ checksum "package.json" }} docker_hub: user-id: &dockerhub-user-id anthonyjhoiro password: &dockerhub-password $DOCKERHUB_PASSWORD image: &dockerhub-image devops-tpfinal-back # --- Commands --- # commands: setup_dependencies: description: Setup the environment steps: - checkout - restore_cache: keys: - *dependencies # --- Jobs definitions --- # jobs: build: &shared-config docker: - image: *node auth: username: *dockerhub-user-id password: *dockerhub-password working_directory: ~/app steps: - checkout - restore_cache: keys: - *dependencies # fallback to using the latest cache if no exact match is found - dependencies- - run: yarn install - save_cache: paths: - node_modules key: *dependencies test: <<: *shared-config steps: - setup_dependencies - run: yarn test test-e2e: <<: *shared-config docker: - image: *node - image: *postgres environment: POSTGRES_DB: postgres POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres steps: - setup_dependencies - run: command: yarn test:e2e environment: DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres lint: <<: *shared-config steps: - setup_dependencies - run: name: Check files format command: yarn format:check - run: name: Lint files command: yarn lint docker-build: <<: *shared-config parameters: tags: default: "" type: string environment: DOCKER_HUB_PWD: *dockerhub-password DOCKER_HUB_USER_ID: *dockerhub-user-id DOCKER_HUB_IMAGE: *dockerhub-image steps: - checkout - setup_remote_docker: version: 20.10.7 - run: name: Build docker image command: docker build -t app . - deploy: name: Publish image to Docker Hub command: | echo $DOCKERHUB_PASSWORD | docker login -u $DOCKER_HUB_USER_ID --password-stdin for tag in <> do docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$tag docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$tag done # docker tag app $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:$CIRCLE_BUILD_NUM # # docker push $DOCKER_HUB_USER_ID/$DOCKER_HUB_IMAGE:latest deploy-heroku: <<: *shared-config docker: - image: buildpack-deps:trusty auth: username: *dockerhub-user-id password: *dockerhub-password steps: - setup_dependencies - run: name: Heroku Deploy command: git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git HEAD:main - run: name: Smoke Test command: | HTTPCODE=`curl -s -o /dev/null -w "%{http_code}" https://$HEROKU_APP_NAME.herokuapp.com/` if [ "$HTTPCODE" -ne 200 ];then echo "heroku app not responding, failing deploy" exit 1 fi # --- Workflow definition --- # workflows: version: 2 build-test-and-lint: jobs: - build - test: requires: - build - test-e2e: requires: - build - lint: requires: - build - docker-build: tags: "latest $CIRCLE_BUILD_NUM" requires: - test - test-e2e - lint filters: branches: only: - main - docker-build: tags: "dev $CIRCLE_BUILD_NUM-dev" requires: - test - test-e2e - lint filters: branches: only: - develop - deploy-heroku: requires: - test - test-e2e - lint filters: branches: only: - main