name: CI/CD
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [13.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Cache node modules
uses: actions/cache@v1
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.os }}-build-${{ hashFiles('**/package-lock.json') }}
- name: Install packages
run: npm install
- name: Build
run: npm run build
- name: Upload staticfiles to Aliyun OSS
uses: manyuanrong/[email protected]
with:
endpoint: ${{ secrets.OSS_ENDPOINT }}
access-key-id: ${{ secrets.OSS_ID }}
access-key-secret: ${{ secrets.OSS_KEY }}
- run: ossutil cp -rf build/ oss://<myoss>
- name: Dockerize
id: dockerize
uses: elgohr/Publish-Docker-Github-Action@master
with:
name: ${{ secrets.DOCKER_URL }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
registry: ${{ secrets.DOCKER_REGISTRY }}
snapshot: true
- name: Send docker image tag to telegram
uses: appleboy/telegram-action@master
with:
token: ${{ secrets.TG_BOT_TOKEN }}
to: ${{ secrets.TG_BOT_TO }}
message: "${{ github.repository }} uploaded a new docker image: ${{ secrets.DOCKER_URL }}:${{ steps.dockerize.outputs.snapshot-tag }}"
步骤
- checkout 拉代码
- 设置 node 环境
- 针对 package-lock.json 做缓存检查,如果变了就不加载.npm
- npm install 做缓存,存入.npm
- npm run build
- 推送静态资源到 OSS
- Dockerize 并 push 到自己的 Registry ( tag 会自动生成)
- 搞定以后把 docker image name 以及 tag 推到 Telegram Bot 里
没做的
- 只做到 push docker image 这步,自动部署没搞,现在是全手动修改 tag,并重启 docker-compose
- jobs,workflow 按职责分开,但是目前看来没必要
抛砖引玉
如果用 docker-compose 来管理多个项目,各位的 CI/CD 是怎么做的呢?如果有 test 以及 prod 环境又有什么高见呢?