本文作者:丁辉
GithubAction
构建镜像
GITHUB设置 secrets
网址为:仓库地址/settings/secrets/actions
定时任务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| on: schedule: - cron: "0 0 * * *"
on: repository_dispatch: types: - StartAction
on: push: branches: - master
on: push: branches: - master tags: - v*
|
构建 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
| name: Build-Images
jobs: run-docker-command: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Run Docker Command run: | docker run --name mysql \ -e MYSQL_ROOT_PASSWORD=${{ secrets.PASSWORD }} \ ${{ secrets.IMAGES }} ---
jobs: Build-Images-One: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Login to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_HUB_USERNAME }} password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Build and push image uses: docker/build-push-action@v3 with: context: ./ file: ./Dockerfile push: true tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_STASH }}:${{ secrets.TAG }} Build--Images-Two: needs: Build-Images-One runs-on: ubuntu-latest steps: - name: Check Out uses: actions/checkout@v3 - name: Login to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_HUB_USERNAME }} password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2
- name: Build and push id: docker_build uses: docker/build-push-action@v3 with: context: ./demo/ file: ./demo/Dockerfile push: true tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_STASH }}:${{ secrets.TAG }}
|
接口触发构建
1
| curl -X POST https://api.github.com/repos/用户/仓库名/dispatches -H "Accept: application/vnd.github.everest-preview+json" -H "Authorization: token $YOUR_API_TOKEN" --data '{"event_type": "StartAction"}'
|
构建多架构镜像
官方Demo
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
| name: ci
on: push: branches: - "main"
jobs: docker: runs-on: ubuntu-latest steps: - name: Login to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Checkout uses: actions/checkout@v3 - name: Set up QEMU uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Build and push uses: docker/build-push-action@v4 with: context: . platforms: linux/amd64,linux/arm64 push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/nginx:latest
|