【问题标题】:CircleCI deploy to AWS EC2CircleCI 部署到 AWS EC2
【发布时间】:2019-01-30 06:35:31
【问题描述】:

我找不到任何良好且易于理解的 CircleCI 配置示例来构建和部署到 AWS EC2 实例。到目前为止,这是我所拥有的:

.circleci/config.yml

version: 2
jobs:
    build:
        docker:
            - image: circleci/node:10.7
        steps:
            - checkout
            - restore_cache:
                keys:
                    - v1-dependencies-{{ checksum "package.json" }}
                    - v1-dependencies-
            - run:
                name: Install dependencies
                command: npm install
            - save_cache:
                key: v1-dependencies-{{ checksum "package.json" }}
                paths:
                    - node_modules
            - run:
                name: Lint code
                command: npm run lint
            - run:
                name: Build app
                command: npm run build
            - save_cache:
                key: v1-build-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
                paths:
                    - .next
    deploy:
        docker:
            - image: circleci/node:10.7
        steps:
            - run:
                name: Deploy production
                command: ?
workflows:
    version: 2
    build_and_deploy:
        jobs:
            - build
            - deploy:
                requires:
                    - build

到目前为止,整个构建步骤运行良好,成功进入部署步骤。但是,当正在构建的分支是 master 时,如何将构建部署到我的 EC2 服务器上的文件夹?

【问题讨论】:

  • 将构建部署到 EC2 上的文件夹是什么意思?这是指scp - 对 EC2 实例执行某些操作吗?或者在 pm2 或 Docker 中运行 node 应用程序?
  • 还是只是根据git分支有条件地运行命令?
  • @JackGore 是的,我想在我的 EC2 实例上运行该应用程序。目前我只在 CircleCI 中成功构建。
  • 目前我可以进行本地生产构建,将文件 scp 到我的 EC2 实例,通过 SSH 连接到 EC2 并使用 PM2 启动应用程序。我正在尝试自动化整个部署过程。我很惊讶网上似乎没有任何好的示例/教程,因为我认为这将非常普遍。
  • 这有什么更新吗?我在同一条船上

标签: amazon-web-services amazon-ec2 continuous-integration yaml circleci


【解决方案1】:

来自this article,您可以使用此 config.yml 设置:

version: 2
general:
  branches:
    only:
      - dev
      - staging
      - prod
jobs:
  build:
    docker:
      - image: circleci/openjdk:8-jdk
    steps:
      - checkout
      - run:
          name: Deploy
          command: |
            # 1- Install AWS CLI
            curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
            unzip awscli-bundle.zip
            ./awscli-bundle/install -b ~/bin/aws
            # 2- Get the public IP of the current CircleCI runner
            PUBLIC_IP=$(curl ipinfo.io/ip)
            # 3- Get AWS Region# TODO Don't forget to replcae by your own Region
            AWS_REGION=us-east-2
            # 4- Get SG ID# TODO Don't forget to replace by your own SG ID
            SG_ID=sg-XXXXXXXX
            # 5- Add an ingress rule to the security group
            ~/bin/aws ec2 authorize-security-group-ingress --region $AWS_REGION --group-id $SG_ID \
              --protocol tcp --port 22 --cidr $PUBLIC_IP/24
            # 6- Give the ingress rule some time to propogate
            sleep 5
            # 7- SSH to the server to deploy
            # TODO Change to your username
            EC2_USERNAME=ubuntu
            # TODO Change to your server's URL or public IP
            EC2_PUBLIC_DNS=application-server.example.com
            ssh -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_PUBLIC_DNS \
            # other commands
            # TODO Perform steps to deploy
            # .
            # .
            # .
            # 8- Remove the ingress rule
            ~/bin/aws ec2 revoke-security-group-ingress --region $AWS_REGION --group-id $SG_ID \
              --protocol tcp --port 22 --cidr $PUBLIC_IP/24

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2018-12-04
    • 2021-09-03
    相关资源
    最近更新 更多