Skip to content

Commit 761295e

Browse files
authored
CI/CD pipleine defined - 1st version
1 parent b248561 commit 761295e

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

.github/workflows/publish.yml

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
name: Publishing
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
workflow_dispatch:
8+
inputs:
9+
source_branch:
10+
description: 'Source branch'
11+
required: true
12+
default: 'master'
13+
14+
permissions:
15+
contents: write
16+
packages: write
17+
18+
jobs:
19+
publish:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v3
24+
with:
25+
ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.source_branch || github.ref }}
26+
submodules: recursive
27+
28+
- name: Check if should be published
29+
if: ${{ github.event_name != 'workflow_dispatch' && !contains(github.event.head_commit.message, '[pub]') }}
30+
run: |
31+
echo "[pub] string is missing in commit message, skipping ..."
32+
exit 0
33+
34+
- name: Publish
35+
id: pub
36+
if: ${{ github.event_name == 'workflow_dispatch' || contains(github.event.head_commit.message, '[pub]') }}
37+
run: |
38+
CGO_ENABLED=0 \
39+
go build -trimpath -ldflags="-s -w" -o main
40+
if ldd main 2>&1 | grep -vq "not a dynamic executable"; then
41+
echo "ERROR: binary is dynamically linked"
42+
ldd main || true
43+
exit 1
44+
fi
45+
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \
46+
go build -trimpath -ldflags="-s -w" -o main.exe
47+
echo "exists=true" >> $GITHUB_OUTPUT
48+
49+
- name: Create tag
50+
id: newtag
51+
if: steps.pub.outputs.exists == 'true'
52+
run: |
53+
echo "::group::Version overview"
54+
VERSION=$(awk '/^## /{print $2; exit}' CHANGELOG.md)
55+
BODY=$(awk '/^## /{if (seen++) exit} seen' CHANGELOG.md | tail -n +2)
56+
echo "version=$VERSION" >> $GITHUB_OUTPUT
57+
echo "body<<EOF" >> $GITHUB_OUTPUT
58+
echo "$BODY" >> $GITHUB_OUTPUT
59+
echo "EOF" >> $GITHUB_OUTPUT
60+
echo "::endgroup::"
61+
62+
TAG="$VERSION"
63+
64+
echo "::group::New tag created"
65+
echo "tag=$TAG" >> $GITHUB_OUTPUT
66+
git tag $TAG
67+
git push origin $TAG || true
68+
echo "::endgroup::"
69+
70+
- name: Create release
71+
id: crelease
72+
if: steps.pub.outputs.exists == 'true'
73+
uses: actions/create-release@v1
74+
with:
75+
tag_name: ${{ steps.newtag.outputs.version }}
76+
release_name: ${{ steps.newtag.outputs.version }}
77+
body: ${{ steps.newtag.outputs.body }}
78+
env:
79+
GITHUB_TOKEN: ${{ secrets._TOKEN }}
80+
81+
- name: Upload asset
82+
if: steps.pub.outputs.exists == 'true'
83+
uses: actions/upload-release-asset@v1
84+
env:
85+
GITHUB_TOKEN: ${{ secrets._TOKEN }}
86+
with:
87+
upload_url: ${{ steps.crelease.outputs.upload_url }}
88+
asset_path: ./main
89+
asset_name: main
90+
asset_content_type: application/octet-stream
91+
- name: Upload asset Win
92+
if: steps.pub.outputs.exists == 'true'
93+
uses: actions/upload-release-asset@v1
94+
env:
95+
GITHUB_TOKEN: ${{ secrets._TOKEN }}
96+
with:
97+
upload_url: ${{ steps.crelease.outputs.upload_url }}
98+
asset_path: ./main.exe
99+
asset_name: main.exe
100+
asset_content_type: application/octet-stream
101+
102+
- name: Set up Docker Buildx
103+
if: steps.pub.outputs.exists == 'true'
104+
uses: docker/setup-buildx-action@v3
105+
106+
- name: Log in to GitHub Container Registry
107+
if: steps.pub.outputs.exists == 'true'
108+
uses: docker/login-action@v3
109+
with:
110+
registry: ghcr.io
111+
username: ${{ github.actor }}
112+
password: ${{ secrets.GITHUB_TOKEN }}
113+
114+
- name: Normalize repository owner
115+
if: steps.pub.outputs.exists == 'true'
116+
id: normalize
117+
run: |
118+
echo "owner_lc=${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_OUTPUT"
119+
120+
- name: Build and push Docker image
121+
if: steps.pub.outputs.exists == 'true'
122+
uses: docker/build-push-action@v5
123+
with:
124+
context: .
125+
push: true
126+
platforms: linux/amd64
127+
tags: |
128+
ghcr.io/${{ steps.normalize.outputs.owner_lc }}/MiniHTTPServer:latest
129+
ghcr.io/${{ steps.normalize.outputs.owner_lc }}/MiniHTTPServer:${{ steps.newtag.outputs.version }}
130+
build-args: |
131+
PACKAGE_TAG=${{ steps.newtag.outputs.version }}

0 commit comments

Comments
 (0)