Skip to content

Commit 1dce0be

Browse files
committed
feat: add gh workflows for image publication
1 parent fc4ba54 commit 1dce0be

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Build, Release & Publish to ghcr
2+
3+
permissions:
4+
contents: write
5+
packages: write
6+
actions: write
7+
8+
on:
9+
push:
10+
branches:
11+
- release
12+
- main
13+
14+
jobs:
15+
build_release:
16+
runs-on: self-hosted
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v6
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Fetch all Git tags
25+
run: git fetch --tags
26+
27+
- name: Check for new commits since last tag
28+
if: github.ref == 'refs/heads/release'
29+
id: check_commits
30+
run: |
31+
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
32+
echo "last_tag=$LAST_TAG" >> $GITHUB_OUTPUT
33+
34+
if [ -z "$LAST_TAG" ]; then
35+
echo "no_last_tag=true" >> $GITHUB_OUTPUT
36+
else
37+
NEW_COMMITS=$(git log ${LAST_TAG}..HEAD --oneline)
38+
if [ -z "$NEW_COMMITS" ]; then
39+
echo "no_new_commits=true" >> $GITHUB_OUTPUT
40+
else
41+
echo "no_new_commits=false" >> $GITHUB_OUTPUT
42+
fi
43+
fi
44+
45+
- name: Get next version
46+
if: github.ref == 'refs/heads/release' && steps.check_commits.outputs.no_new_commits != 'true'
47+
id: version
48+
uses: mathieudutour/github-tag-action@v6.2
49+
with:
50+
github_token: ${{ secrets.GITHUB_TOKEN }}
51+
release_branches: release
52+
tag_prefix: "v"
53+
custom_release_rules: |
54+
major:major:Breaking Changes
55+
minor:minor:Features
56+
patch:patch:Bug Fixes or other non-feature changes
57+
58+
- name: Generate changelog
59+
if: github.ref == 'refs/heads/release' && steps.check_commits.outputs.no_new_commits != 'true'
60+
id: changelog
61+
env:
62+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
run: |
64+
PREV_TAG=${{ steps.check_commits.outputs.last_tag }}
65+
CURR_TAG=${{ steps.version.outputs.new_tag }}
66+
67+
git log ${PREV_TAG}..HEAD --pretty=format:"%H|%s" | while IFS="|" read -r COMMIT_HASH MESSAGE; do
68+
AUTHOR_LOGIN=$(gh api repos/${{ github.repository }}/commits/$COMMIT_HASH --jq '.author.login // empty')
69+
AUTHOR_NAME=$(gh api repos/${{ github.repository }}/commits/$COMMIT_HASH --jq '.commit.author.name // empty')
70+
71+
if [[ "$AUTHOR_LOGIN" == *"bot"* ]]; then
72+
echo "- $MESSAGE ([${COMMIT_HASH:0:7}](https://github.com/${{ github.repository }}/commit/$COMMIT_HASH)) by $AUTHOR_NAME" >> RELEASE_NOTES.md
73+
else
74+
if [[ -n "$AUTHOR_LOGIN" ]]; then
75+
echo "- $MESSAGE ([${COMMIT_HASH:0:7}](https://github.com/${{ github.repository }}/commit/$COMMIT_HASH)) by @$AUTHOR_LOGIN" >> RELEASE_NOTES.md
76+
else
77+
echo "- $MESSAGE ([${COMMIT_HASH:0:7}](https://github.com/${{ github.repository }}/commit/$COMMIT_HASH)) by $AUTHOR_NAME" >> RELEASE_NOTES.md
78+
fi
79+
fi
80+
done
81+
82+
- name: Create GitHub Release
83+
if: github.ref == 'refs/heads/release' && steps.check_commits.outputs.no_new_commits != 'true'
84+
id: github_release
85+
uses: softprops/action-gh-release@v2
86+
with:
87+
tag_name: ${{ steps.version.outputs.new_tag }}
88+
name: "MineTracker-Backend | ${{ steps.version.outputs.new_tag }} Release"
89+
body_path: RELEASE_NOTES.md
90+
91+
- name: Set up Docker Buildx
92+
uses: docker/setup-buildx-action@v3
93+
94+
- name: Log in to GitHub Container Registry
95+
uses: docker/login-action@v3
96+
with:
97+
registry: ghcr.io
98+
username: ${{ github.actor }}
99+
password: ${{ secrets.GITHUB_TOKEN }}
100+
101+
- name: Generate Docker tags
102+
id: docker_tags
103+
run: |
104+
SHORT_COMMIT=$(git rev-parse --short HEAD)
105+
106+
if [ "${{ github.ref }}" == "refs/heads/release" ]; then
107+
if [ "${{ steps.check_commits.outputs.no_new_commits }}" != "true" ]; then
108+
VERSION=${{ steps.version.outputs.new_tag }}
109+
echo "tags=ghcr.io/${{ github.repository_owner }}/minetracker-backend:${VERSION}+${SHORT_COMMIT},ghcr.io/${{ github.repository_owner }}/minetracker-backend:latest" >> $GITHUB_OUTPUT
110+
fi
111+
elif [ "${{ github.ref }}" == "refs/heads/main" ]; then
112+
echo "tags=ghcr.io/${{ github.repository_owner }}/minetracker-backend:dev-${SHORT_COMMIT},ghcr.io/${{ github.repository_owner }}/minetracker-backend:dev" >> $GITHUB_OUTPUT
113+
fi
114+
115+
- name: Build and push Docker image
116+
if: steps.docker_tags.outputs.tags != ''
117+
uses: docker/build-push-action@v6
118+
with:
119+
context: .
120+
push: true
121+
tags: ${{ steps.docker_tags.outputs.tags }}
122+
platforms: linux/amd64,linux/arm64

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM golang:1.25.6-alpine AS builder
2+
3+
WORKDIR /app
4+
5+
RUN apk add --no-cache git
6+
7+
COPY go.mod go.sum ./
8+
RUN go mod download
9+
COPY . .
10+
11+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o minetracker .
12+
13+
FROM alpine:latest
14+
15+
WORKDIR /app
16+
RUN apk --no-cache add ca-certificates
17+
COPY --from=builder /app/minetracker .
18+
19+
RUN mkdir -p /app/data
20+
21+
CMD ["./minetracker"]

0 commit comments

Comments
 (0)