GithubActions进行CICD一键部署
Created|Updated
|Word Count:855|Post Views:
基本概念
Github Actions 是 Github 推出的持续集成工具
workflow
: 一个 workflow
工作流就是一个完整的过程,每个workflow
包含一组 jobs
任务。
job : jobs
任务包含一个或多个job
,每个 job
包含一系列的 steps
步骤。
step
: 每个 step
步骤可以执行指令或者使用一个 action
动作。
action
: 每个 action
动作就是一个通用的基本单元。
workflow
必须存储在项目根路径下的 .github/workflows
中,每个workflow对应一个具体的 .yml
文件
workflow 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| name: summerbot's CI/CD
on: push: branches: [ main ]
jobs: build: runs-on: ubuntu-latest
steps: - name: checkout uses: actions/checkout@v3
- name: Set up JDK 11 uses: actions/setup-java@v2 with: java-version: '11' distribution: 'temurin' cache: 'maven'
- name: Build with Maven run: mvn -B -DskipTests=true package --file pom.xml
- name: Deploy to server uses: easingthemes/ssh-deploy@v2.2.11 env: ARGS: '-avz --delete' SOURCE: 'target/summerbot.jar' TARGET: '/home/summerbot' REMOTE_HOST: ${{ secrets.HOST }} REMOTE_USER: ${{ secrets.SSH_USERNAME }} SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Run java -jar if: success() uses: fifsky/ssh-action@master with: user: ${{ secrets.SSH_USERNAME }} host: ${{ secrets.HOST }} key: ${{ secrets.SSH_PRIVATE_KEY }} command: screen -R summerbot -X stuff $'^C java -jar /home/summerbot/summerbot.jar\n'
|
workflow 文件(docker)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| name: summerbot's docker CI/CD
on: push: branches: [ main ]
env: IMAGE_TOTAL_NAME: ${{ secrets.DOCKER_USER }}/${{ secrets.SERVICE_CONTAINER_NAME }} IMAGE_TAG: latest
jobs: build: runs-on: ubuntu-latest
steps: - name: checkout uses: actions/checkout@v2
- name: Set up JDK 11 uses: actions/setup-java@v2 with: java-version: '11' distribution: 'temurin' cache: 'maven'
- name: Build with Maven run: mvn -B package --file pom.xml
- name: Set up QEMU uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx uses: docker/setup-buildx-action@v2
- name: Login to DockerHub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_USER }} password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push uses: docker/build-push-action@v3 with: context: . push: true tags: ${{ env.IMAGE_TOTAL_NAME }}:${{ env.IMAGE_TAG }}
- name: executing docker container uses: appleboy/ssh-action@master with: host: ${{ secrets.HOST }} username: ${{ secrets.SSH_USERNAME }} key: ${{ secrets.SSH_PRIVATE_KEY }} script: | docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }} docker pull ${{ env.IMAGE_TOTAL_NAME }}:${{ env.IMAGE_TAG }} docker stop ${{ secrets.SERVICE_CONTAINER_NAME }} docker rm ${{ secrets.SERVICE_CONTAINER_NAME }} docker run -d \ --name ${{ secrets.SERVICE_CONTAINER_NAME }} \ -p 23333:8080 \ ${{ env.IMAGE_TOTAL_NAME }}:${{ env.IMAGE_TAG }}
|
参考文章:
让你满意的GitHub Actions详解 - 简书 (jianshu.com)
给screen发送一个命令运行,并保持screen不退出。51CTO博客
论部署后端项目 | 经验分享博客 (cxy621.top)