利用github actions和aliyun私有镜像库,使用docker部署Nodejs项目
1.开通阿里云容器镜像服务个人版本
2.创建镜像仓库获取推送/拉取链接
3.使用github actions自动部署,以下是我的一个模板
在项目根目录创建.github/workflows/deploy.yml
name: Deploy
on:
push:
branches:
- main # 或者你使用的是哪个分支
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@main
- name: Log in to aliyun private docker Registry
uses: docker/login-action@v3
with:
registry: ${{ secrets.ALIYUN_REGISTRY_URL }} #第2步骤图中的 登录阿里云的地址
username: ${{ secrets.ALIYUN_ACCESS_KEY_ID }} #你的阿里云账号
password: ${{ secrets.ALIYUN_ACCESS_KEY_SECRET }} #你的镜像仓库密码,好像可以是阿里云账号密码,也可以设置独立的密码
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.ALIYUN_REGISTRY_TAG }} #第2步骤图中的 第3小点的地址
- name: Copy docker-compose.yml to Server
uses: appleboy/[email protected]
with:
host: ${{ secrets.ALIYUN_HOST }} #需要部署的主机ip地址
username: ${{ secrets.ALIYUN_USERNAME }} #需要部署的主机 ssh 账号
key: ${{ secrets.ALIYUN_KEY }} #需要部署的主机 ssh key
port: 22
source: './docker-compose.yml'
target: '~/your-target/'
overwrite: true
strip_components: 1
- name: Deploy to Aliyun
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.ALIYUN_HOST }}
username: ${{ secrets.ALIYUN_USERNAME }}
key: ${{ secrets.ALIYUN_KEY }}
script: |
docker login --username=${{ secrets.ALIYUN_ACCESS_KEY_ID }} --password=${{ secrets.ALIYUN_ACCESS_KEY_SECRET }} ${{ secrets.ALIYUN_REGISTRY_URL }}
mkdir -p ~/your-path
cd ~/your-path
docker compose pull
docker compose up -d --force-recreate
# docker login --username=${{ secrets.ALIYUN_ACCESS_KEY_ID }} --password=${{ secrets.ALIYUN_ACCESS_KEY_SECRET }} ${{ secrets.ALIYUN_REGISTRY_URL }}
# docker pull ${{ secrets.ALIYUN_REGISTRY_TAG }}
# docker stop yourContainerName || true
# docker rm yourContainerName || true
# docker run -it -d --name yourContainerName -p 3000:3000 ${{ secrets.ALIYUN_REGISTRY_TAG }}
License:
CC BY 4.0