Skip to content

Commit 5a21225

Browse files
committed
Merge pull request #3 from LinuxSuRen/image
chore: support to publish image
2 parents 80d1baa + f3ea482 commit 5a21225

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

.github/workflows/pull-request.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ on:
88
branches:
99
- master
1010

11+
env:
12+
REGISTRY: ghcr.io
13+
IMAGE_NAME: ${{ github.repository }}
14+
1115
jobs:
1216
build:
1317
name: Build
@@ -36,3 +40,45 @@ jobs:
3640
with:
3741
version: latest
3842
args: release --skip-publish --rm-dist
43+
44+
build-image:
45+
name: Build Image
46+
runs-on: ubuntu-20.04
47+
if: github.ref != 'refs/heads/master'
48+
steps:
49+
- name: Check out code
50+
uses: actions/checkout@v3.0.0
51+
- name: Build Image
52+
run: |
53+
make image
54+
image:
55+
name: Publish Image
56+
runs-on: ubuntu-20.04
57+
if: github.ref == 'refs/heads/master'
58+
steps:
59+
- name: Checkout
60+
uses: actions/checkout@v3.0.0
61+
- name: Setup Docker buildx
62+
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
63+
- name: Log into registry ${{ env.REGISTRY }}
64+
if: github.event_name != 'pull_request'
65+
uses: docker/login-action@v2.1.0
66+
with:
67+
registry: ${{ env.REGISTRY }}
68+
username: ${{ github.actor }}
69+
password: ${{ secrets.GH_PUBLISH_SECRETS }}
70+
- name: Extract Docker metadata
71+
id: meta
72+
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
73+
with:
74+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
75+
- name: Build and push Docker image
76+
id: build-and-push
77+
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
78+
with:
79+
context: .
80+
push: ${{ github.event_name != 'pull_request' }}
81+
tags: ${{ steps.meta.outputs.tags }}
82+
labels: ${{ steps.meta.outputs.labels }}
83+
cache-from: type=gha
84+
cache-to: type=gha,mode=max

.github/workflows/release.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ on:
55
tags:
66
- '*'
77

8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
812
jobs:
913
goreleaser:
1014
runs-on: ubuntu-20.04
@@ -24,3 +28,33 @@ jobs:
2428
args: release --rm-dist
2529
env:
2630
GITHUB_TOKEN: ${{ secrets.GH_PUBLISH_SECRETS }}
31+
32+
image:
33+
runs-on: ubuntu-20.04
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v3.0.0
37+
- name: Setup Docker buildx
38+
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
39+
- name: Log into registry ${{ env.REGISTRY }}
40+
if: github.event_name != 'pull_request'
41+
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
42+
with:
43+
registry: ${{ env.REGISTRY }}
44+
username: ${{ github.actor }}
45+
password: ${{ secrets.GH_PUBLISH_SECRETS }}
46+
- name: Extract Docker metadata
47+
id: meta
48+
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
49+
with:
50+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
51+
- name: Build and push Docker image
52+
id: build-and-push
53+
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
54+
with:
55+
context: .
56+
push: ${{ github.event_name != 'pull_request' }}
57+
tags: ${{ steps.meta.outputs.tags }}
58+
labels: ${{ steps.meta.outputs.labels }}
59+
cache-from: type=gha
60+
cache-to: type=gha,mode=max

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.19 as builder
2+
3+
WORKDIR /workspace
4+
COPY . .
5+
RUN go mod download
6+
RUN CGO_ENABLE=0 go build -ldflags "-w -s" -o gaw
7+
8+
FROM ubuntu:kinetic
9+
10+
LABEL "repository"="https://github.com/linuxsuren/github-action-workflow"
11+
LABEL "homepage"="https://github.com/linuxsuren/github-action-workflow"
12+
13+
COPY --from=builder /workspace/gaw /usr/local/bin/gaw
14+
15+
CMD ["gaw"]

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ copy: build
44
cp bin/gaw /usr/local/bin
55
test-gh:
66
act -W pkg/data/ -j imageTest
7+
image:
8+
docker build .

0 commit comments

Comments
 (0)