amazon-web-services - CircleCI 部署到 AWS EC2
问题描述
我找不到任何好的且易于理解的 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
到目前为止,整个构建步骤运行良好,成功进入部署步骤。但是,当正在构建的分支是时,如何将构建部署到我的 EC2 服务器上的文件夹master
?
解决方案
从这篇文章中,你可以使用这个 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
推荐阅读
- stanford-nlp - 斯坦福 CoreNLP 旧版本
- firebird - Firebird 2.5.8、3.0.4 unicode_ci_ai 索引问题
- c# - 不会从 Angular 5 触发 Web API
- javascript - 如何在 React Native 中从一个屏幕切换到另一个屏幕时使用动画进行屏幕转换?
- javascript - 如何在 php.ini 的登录屏幕中显示警报框。如果用户为此代码输入了错误的密码或用户名
- c# - NUnit 中 DbContextOptionsBuilder 的数据类型
- api - 从相扑逻辑下载数据时 curl 失败
- javascript - 使 react-leaflet 可以离线使用
- angular - 在 ng-bootstrap 中打开另一个面板时如何关闭其他面板
- c# - 防止创建控制器每个请求.net核心