Skip to content

Commit

Permalink
Refactored Docker workflow
Browse files Browse the repository at this point in the history
Significant changes have been made to the Docker build and push workflow. The process now triggers on push events to the main branch, instead of manually via workflow dispatch. Removed steps related to Go setup, dependencies installation, and artifact upload. Introduced QEMU setup, Docker Buildx setup, Docker layers caching, and login actions for both DockerHub and ghcr.io. Also added conditional build and push actions for ghcr.io based on a new environment variable USE_GHCR.
  • Loading branch information
leokwsw committed Mar 29, 2024
1 parent 1c30e0b commit 0ad037c
Showing 1 changed file with 45 additions and 104 deletions.
149 changes: 45 additions & 104 deletions .github/workflows/docker-push.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
name: Docker build and push

on:
# push:
# branches:
# - main
workflow_dispatch:
inputs:
branch:
description: "Branch to build from"
required: false
default: 'main'
image_tag:
description: "Image tag"
required: false
default: 'latest'
push:
branches:
- main

env:
PLATFORMS: ${{ vars.PLATFORMS || 'linux/amd64,linux/arm64' }}
Expand All @@ -30,100 +20,51 @@ jobs:
with:
ref: ${{ github.event.inputs.branch }}

- name: Set current datetime as env variable
env:
TZ: 'Asia/HongKong' # タイムゾーン指定
run: echo "CURRENT_DATETIME=$(date +'%Y-%m-%d-%H%M%S')" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

# region setup and build
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21.x'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Install dependencies
run: go get .
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build
run: go build -v .
# endregion
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}

# region upload artifact
- uses: actions/upload-artifact@v4
with:
name: go-chatgpt-api-${{ env.CURRENT_DATETIME }}
path: go-chatgpt-api
# endregion
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
platforms: ${{ env.PLATFORMS }}
push: true
tags: ${{ github.actor }}/go-chatgpt-api:${{ github.event.inputs.image_tag }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max

# region Release
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GT_TOKEN }}
with:
tag_name: ${{ env.CURRENT_DATETIME }}
release_name: Release ${{ env.CURRENT_DATETIME }}
draft: false
prerelease: false

- name: Upload Release Asset
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GT_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: ./go-chatgpt-api
asset_name: go-chatgpt-api
asset_content_type: application/x-mach-binary
# endregion
- name: Log into ghcr
uses: docker/login-action@v2
if: ${{ vars.USE_GHCR == '1' }}
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GT_TOKEN }}

# - name: Set up QEMU
# uses: docker/setup-qemu-action@v2
#
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v2
#
# - name: Cache Docker layers
# uses: actions/cache@v2
# with:
# path: /tmp/.buildx-cache
# key: ${{ runner.os }}-buildx-${{ github.sha }}
# restore-keys: |
# ${{ runner.os }}-buildx-
#
# - name: Login to DockerHub
# uses: docker/login-action@v2
# with:
# username: ${{ github.actor }}
# password: ${{ secrets.DOCKER_HUB_TOKEN }}
#
# - name: Build and push
# uses: docker/build-push-action@v4
# with:
# context: .
# platforms: ${{ env.PLATFORMS }}
# push: true
# tags: ${{ github.actor }}/go-chatgpt-api:${{ github.event.inputs.image_tag }}
# cache-from: type=local,src=/tmp/.buildx-cache
# cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
#
# - name: Log into ghcr
# uses: docker/login-action@v2
# if: ${{ vars.USE_GHCR == '1' }}
# with:
# registry: ghcr.io
# username: ${{ github.actor }}
# password: ${{ secrets.GITHUB_TOKEN }}
#
# - name: Build and push to ghcr
# uses: docker/build-push-action@v4
# if: ${{ vars.USE_GHCR == '1' }}
# with:
# context: .
# platforms: ${{ env.PLATFORMS }}
# push: true
# tags: ghcr.io/${{ github.actor }}/go-chatgpt-api:${{ github.event.inputs.image_tag }}
# cache-from: type=local,src=/tmp/.buildx-cache
# cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
- name: Build and push to ghcr
uses: docker/build-push-action@v4
if: ${{ vars.USE_GHCR == '1' }}
with:
context: .
platforms: ${{ env.PLATFORMS }}
push: true
tags: ghcr.io/${{ github.actor }}/go-chatgpt-api:${{ github.event.inputs.image_tag }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max

0 comments on commit 0ad037c

Please sign in to comment.