From f3ea482dac1c90762587d6b559cba382099375d7 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 12 Dec 2022 14:43:46 +0800 Subject: [PATCH] chore: support to publish image --- .github/workflows/pull-request.yaml | 46 +++++++++++++++++++++++++++++ .github/workflows/release.yaml | 34 +++++++++++++++++++++ Dockerfile | 15 ++++++++++ Makefile | 2 ++ 4 files changed, 97 insertions(+) create mode 100644 Dockerfile diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 29b5816..ebe4695 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -8,6 +8,10 @@ on: branches: - master +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + jobs: build: name: Build @@ -36,3 +40,45 @@ jobs: with: version: latest args: release --skip-publish --rm-dist + + build-image: + name: Build Image + runs-on: ubuntu-20.04 + if: github.ref != 'refs/heads/master' + steps: + - name: Check out code + uses: actions/checkout@v3.0.0 + - name: Build Image + run: | + make image + image: + name: Publish Image + runs-on: ubuntu-20.04 + if: github.ref == 'refs/heads/master' + steps: + - name: Checkout + uses: actions/checkout@v3.0.0 + - name: Setup Docker buildx + uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@28219f9b04b4f3f62068d7b6ce6ca5b26e35336c + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GH_PUBLISH_SECRETS }} + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index d10838b..7290721 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -5,6 +5,10 @@ on: tags: - '*' +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + jobs: goreleaser: runs-on: ubuntu-20.04 @@ -24,3 +28,33 @@ jobs: args: release --rm-dist env: GITHUB_TOKEN: ${{ secrets.GH_PUBLISH_SECRETS }} + + image: + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v3.0.0 + - name: Setup Docker buildx + uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GH_PUBLISH_SECRETS }} + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0a6d4f0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:1.19 as builder + +WORKDIR /workspace +COPY . . +RUN go mod download +RUN CGO_ENABLE=0 go build -ldflags "-w -s" -o gaw + +FROM alpine:3.10 + +LABEL "repository"="https://github.com/linuxsuren/github-action-workflow" +LABEL "homepage"="https://github.com/linuxsuren/github-action-workflow" + +COPY --from=builder /workspace/gaw /usr/local/bin/gaw + +CMD ["gaw"] \ No newline at end of file diff --git a/Makefile b/Makefile index 45f6742..d9b32e5 100644 --- a/Makefile +++ b/Makefile @@ -4,3 +4,5 @@ copy: build cp bin/gaw /usr/local/bin test-gh: act -W pkg/data/ -j imageTest +image: + docker build .