Github Actions 推送代碼構建 Docker 鏡像并 push 到鏡像倉庫
需要解決的問題
解決 ci/cd 流程,在 推送代碼到 github 時,github 的 action 能夠自動構建代碼并發(fā)布到鏡像倉庫,之后運行 docker-compose 文件,啟動項目訪問。
問題前置分析
目前比較流行的 ci/cd 解決方案為:
- github + jenkins
- Github/gitlab + jenkins + docker hub + Docker/K8s
- Github + docker hub + Docker/k8s
上述兩種方案:
-
配置 github 的 webhook 鉤子,在 push 代碼之后,主動去通知 jenkins,構建鏡像完成項目部署;
-
配置 github 的 webhook 鉤子,在 push 代碼之后,主動去通知 jenkins,完成項目的構建和打包,之后上傳鏡像到 docker hub 倉庫,在 github action 中調用服務器中的 docker 完成服務的部署,實現(xiàn) ci/cd 自動化流程;
上述兩種方案存在的缺點:
- 沒有鏡像倉庫存儲 dokcer images,出現(xiàn)問題沒辦法回滾,擴容等;
- Jenkins 拉取 github 會有較大幾率超時,拉取失?。?/li>
- Jenkins 配置相對繁瑣,上手難度太高;
- 沒有 容器管理工具,無法及時得知 容器運行狀態(tài)。
-
舍棄掉之前的 jenkins,由 github action 完成打包和鏡像的推送,作為容器化之后的首選;
能夠有效解決上述方案存在的缺點,唯一的缺點是需要在 github action 配置時多花時間。
前置準備
這里介紹一些必要的組件,使用方法自己百度。
組件介紹
Docker hub
- Aliyun AKC 服務 https://cr.console.aliyun.com/
- Docker hub 官方 https://hub.docker.com/
- 私有的 Harbor 服務 https://goharbor.io/
容器管理平臺
- Portainer
- Rainbond https://www.rainbond.com/usescene/AppManagement/
- Ranchar/k8s https://docs.rancher.cn/
包含 Docker 和 k8s 的管理平臺
Github action secrets 設置
repo -> settings -> Security -> Actions secrets and variables -> secrets 中設置
deploy-docker.yml
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Build And Deploy Docker
# github action 的觸發(fā)時機
on:
push:
# 指定分支觸發(fā)
branches:
- "master"
# 指定路徑或者文件,當此路徑下的文件發(fā)生變化時,執(zhí)行此 action
paths:
- 'index.html'
# 發(fā)布 tag 時執(zhí)行此 action
tags:
- 'v*'
# 指定分支發(fā)生 pr 操作時觸發(fā)
pull_request:
branches: [ "master" ]
# env 環(huán)境變量,為下面的 action 腳本提供
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ${{ secrets.REGISTRY }}
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
# 提供 docker 元數(shù)據(jù),構建 docker images tag 時使用
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.IMAGE_NAME }}
tags: |
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
# tag event
type=ref,enable=true,priority=600,prefix=,suffix=,event=tag
# 構建鏡像 使用 docker/Build-And-push@v5 插件有問題
- uses: actions/checkout@v2
- name: Build Docker Image
run: |
docker build -t ${{ env.REGISTRY }}/${{ steps.meta.outputs.tags }} -f ./docker/nginx/Dockerfile .
- name: Push Docker Image
run: |
docker login --username=${{ secrets.DOCKER_USERNAME }} --password ${{ secrets.DOCKER_PASSWORD }} ${{ env.REGISTRY }}
docker push ${{ env.REGISTRY }}/${{ steps.meta.outputs.tags }}
# 運行 docker 服務
# 另外一種選擇是通過 scp 將文件傳到指定服務器,完成 docker-compose 啟動
- name: Deploy Docker App
uses: appleboy/ssh-action@master
env:
TZ: Asia/Shanghai
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.HOST_USERNAME }}
key: ${{ secrets.HOST_SSHKEY }}
port: ${{ secrets.PORT }}
script: |
# 獲取 docker-compose 文件
wget https://raw.githubusercontent.com/${{ env.IMAGE_NAME }}/master/docker/docker-compose.yml
ls
cat docker-compose.yml
docker-compose down -v
docker-compose up -d
之上的一些 action 插件都可是使用其他的代替文章來源:http://www.zghlxwxcb.cn/news/detail-799775.html
- https://github.com/aliyun/acr-login
- https://docs.github.com/zh/actions/publishing-packages/publishing-docker-images#publishing-images-to-github-packages
- …
效果展示
示例倉庫:https://github.com/yuluo-yx/uipaas-ci-cd-test文章來源地址http://www.zghlxwxcb.cn/news/detail-799775.html
到了這里,關于Github Actions 推送代碼構建 Docker 鏡像并 push 到倉庫的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!