Skip to content

Commit 9dbbb72

Browse files
Merge pull request #206 from selfdocumentingcode/build/git-version
Use GitVersion to tag images. Create automatic github releases in main. BREAKING CHANGE: Just bumping to 1.0.0
2 parents a8df5c2 + 9772e9c commit 9dbbb72

File tree

6 files changed

+222
-22
lines changed

6 files changed

+222
-22
lines changed

.github/workflows/build.yml

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,48 @@ jobs:
2121
runs-on: ubuntu-latest
2222

2323
permissions:
24-
contents: read
24+
contents: write
2525
packages: write
2626

2727
steps:
28-
- uses: actions/checkout@v4
28+
- name: Checkout
29+
uses: actions/checkout@v4
30+
with:
31+
fetch-depth: 0
2932

3033
- name: Setup .NET Core
3134
uses: actions/setup-dotnet@v4
3235
with:
3336
dotnet-version: 8.0.x
3437

38+
- name: Install GitVersion
39+
uses: gittools/actions/gitversion/setup@v3
40+
with:
41+
versionSpec: '6.0.x'
42+
43+
- name: Execute GitVersion
44+
id: gitversion
45+
uses: gittools/actions/gitversion/execute@v3
46+
with:
47+
useConfigFile: true
48+
49+
- name: Display GitVersion config
50+
uses: gittools/actions/gitversion/command@v3
51+
if: ${{ env.ACTIONS_RUNNER_DEBUG == 'true' }}
52+
with:
53+
arguments: '/showConfig'
54+
55+
- name: Output the FullSemVer variable
56+
uses: gittools/actions/gitversion/[email protected]
57+
if: ${{ env.ACTIONS_RUNNER_DEBUG == 'true' }}
58+
with:
59+
arguments: '/showvariable FullSemVer'
60+
61+
- name: Update the version number in the project files
62+
uses: gittools/actions/gitversion/[email protected]
63+
with:
64+
arguments: '/updateprojectfiles'
65+
3566
- name: Test
3667
run: dotnet test
3768

@@ -48,9 +79,10 @@ jobs:
4879
with:
4980
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
5081
tags: |
51-
# set latest tag for default branch
5282
type=raw,value=latest,enable={{is_default_branch}}
53-
type=ref,event=branch,enable=${{ github.ref != format('refs/heads/{0}', 'main') }}
83+
type=ref,event=branch,enable=${{ github.ref != 'refs/heads/main' }}
84+
type=raw,value=${{ steps.gitversion.outputs.semVer }}
85+
type=sha,format=long
5486
5587
- name: Build and push Docker image
5688
uses: docker/[email protected]

.github/workflows/release-v2.yml

Lines changed: 140 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,73 @@ env:
2323
DB_MIGRATION_SCRIPT: database_migration.sql
2424

2525
jobs:
26-
release:
26+
setup:
2727
runs-on: ubuntu-latest
28+
29+
outputs:
30+
imageTag: ${{ steps.setup-vars.outputs.IMAGE_TAG }}
31+
needsSemver: ${{ steps.setup-vars.outputs.NEEDS_SEMVER }}
32+
repositoryOwnerLC: ${{ steps.setup-vars.outputs.REPOSITORY_OWNER_LC }}
33+
repositoryName: ${{ steps.setup-vars.outputs.REPOSITORY_NAME }}
34+
2835
steps:
29-
- uses: actions/checkout@v4
30-
with:
31-
sparse-checkout: |
32-
docker/${{ env.COMPOSE_FILE_NAME }}
33-
sparse-checkout-cone-mode: false
36+
- name: Checkout
37+
uses: actions/checkout@v4
3438

3539
- name: Set up environment variables
40+
id: setup-vars
3641
run: |
42+
BRANCH_NAME_DASH=$(echo "${GITHUB_REF#refs/heads/}" | tr '/' '-')
43+
3744
# Set the image tag based on the branch name. If the branch is main, use "latest".
3845
# If the image tag is provided as an input, use that instead.
3946
if [ -z "$IMAGE_TAG_OVERRIDE" ]; then
4047
if [ "$GITHUB_REF" = "refs/heads/main" ]; then
4148
IMAGE_TAG=latest
4249
else
43-
IMAGE_TAG=$(echo "${GITHUB_REF#refs/heads/}" | tr '/' '-')
50+
IMAGE_TAG="$BRANCH_NAME_DASH"
4451
fi
4552
else
46-
IMAGE_TAG=$IMAGE_TAG_OVERRIDE
53+
IMAGE_TAG="$IMAGE_TAG_OVERRIDE"
54+
fi
55+
echo "IMAGE_TAG=$IMAGE_TAG" >> "$GITHUB_OUTPUT"
56+
57+
# If the image tag is "latest" or the branch name, flag it as needing semver.
58+
if [ "$IMAGE_TAG" = "latest" ]; then
59+
NEEDS_SEMVER=true
60+
elif [ "$IMAGE_TAG" = "$BRANCH_NAME_DASH" ]; then
61+
NEEDS_SEMVER=true
4762
fi
48-
echo "IMAGE_TAG=$IMAGE_TAG" >> "$GITHUB_ENV"
63+
echo "NEEDS_SEMVER: $NEEDS_SEMVER"
64+
echo "NEEDS_SEMVER=$NEEDS_SEMVER" >> "$GITHUB_OUTPUT"
4965
5066
# Set the repository name to lowercase
51-
REPOSITORY_NAME=$(echo $REPOSITORY_OWNER | tr '[:upper:]' '[:lower:]');
52-
echo "REPOSITORY_NAME=$REPOSITORY_NAME" >> "$GITHUB_ENV"
67+
REPOSITORY_OWNER_LC=$(echo $REPOSITORY_OWNER | tr '[:upper:]' '[:lower:]');
68+
echo "REPOSITORY_OWNER_LC=$REPOSITORY_OWNER_LC" >> "$GITHUB_OUTPUT"
69+
70+
# Extract the repository name from the repository env i.e. selfdocumentingcode/kattbot => kattbot
71+
REPOSITORY_NAME=${REPOSITORY##*/}
72+
echo "REPOSITORY_NAME=$REPOSITORY_NAME" >> "$GITHUB_OUTPUT"
5373
env:
5474
IMAGE_TAG_OVERRIDE: ${{ inputs.imageTag }}
5575
REPOSITORY_OWNER: ${{ github.repository_owner }}
76+
REPOSITORY: ${{ github.repository }}
77+
78+
release:
79+
runs-on: ubuntu-latest
80+
permissions:
81+
contents: read
82+
packages: write
83+
needs: setup
84+
env:
85+
IMAGE_TAG: ${{ needs.setup.outputs.imageTag }}
86+
REPOSITORY_OWNER_LC: ${{ needs.setup.outputs.repositoryOwnerLC }}
87+
steps:
88+
- uses: actions/checkout@v4
89+
with:
90+
sparse-checkout: |
91+
${{ env.COMPOSE_FILE_PATH_SRC }}
92+
sparse-checkout-cone-mode: false
5693

5794
- name: Upload compose.yml
5895
uses: appleboy/[email protected]
@@ -84,7 +121,7 @@ jobs:
84121
debug: ${{vars.ACTIONS_RUNNER_DEBUG}}
85122
script_stop: true
86123
script: |
87-
FULL_IMAGE_NAME="$REGISTRY/$REPOSITORY_NAME/$IMAGE_NAME:$IMAGE_TAG"
124+
FULL_IMAGE_NAME="$REGISTRY/$REPOSITORY_OWNER_LC/$IMAGE_NAME:$IMAGE_TAG"
88125
echo "Full image name: $FULL_IMAGE_NAME"
89126
90127
MIGRATIONS_CONTAINER_NAME="$COMPOSE_PROJECT_NAME-migrations"
@@ -128,7 +165,7 @@ jobs:
128165
envs: >-
129166
HOST_GATEWAY_IP,
130167
REGISTRY,
131-
REPOSITORY_NAME,
168+
REPOSITORY_OWNER_LC,
132169
IMAGE_NAME,
133170
IMAGE_TAG,
134171
COMPOSE_PROJECT_NAME,
@@ -141,3 +178,93 @@ jobs:
141178
BOT_TOKEN,
142179
OPENAI_API_KEY,
143180
DB_CONNECTION_STRING
181+
182+
create-release:
183+
runs-on: ubuntu-latest
184+
if: ${{ github.ref == 'refs/heads/main' }}
185+
permissions:
186+
packages: read
187+
contents: read
188+
needs: [setup, release]
189+
env:
190+
IMAGE_TAG: ${{ needs.setup.outputs.imageTag }}
191+
NEEDS_SEMVER: ${{ needs.setup.outputs.needsSemver }}
192+
REPOSITORY_NAME: ${{ needs.setup.outputs.repositoryName }}
193+
steps:
194+
- name: Checkout
195+
uses: actions/checkout@v4
196+
with:
197+
fetch-depth: 0
198+
199+
- name: GitHub Packages Admin
200+
id: package-info
201+
uses: selfdocumentingcode/[email protected]
202+
with:
203+
ghtoken: ${{ github.token }}
204+
operation: listPackageVersions
205+
user: ${{ github.repository_owner }}
206+
package_type: container
207+
package_name: ${{ env.REPOSITORY_NAME }}
208+
include: metadata.container.tags[*] ${{ env.IMAGE_TAG }}
209+
slice: __NONE__ 1 # get the first item only
210+
211+
- name: Get container tags
212+
run: |
213+
echo "IMAGE_TAG: $IMAGE_TAG"
214+
echo "NEEDS_SEMVER: $NEEDS_SEMVER"
215+
216+
TAG_LIST=$(echo "$PACKAGE_INFO" | jq --raw-output '.[0].metadata.container.tags')
217+
echo "TAG_LIST: $TAG_LIST"
218+
219+
SHA_TAG=$(echo "$PACKAGE_INFO" | jq --raw-output '.[0].metadata.container.tags | map(select(startswith("sha"))) | .[0]')
220+
echo "SHA_TAG: $SHA_TAG"
221+
222+
# Exclude "sha-" prefix from SHA_TAG
223+
COMMIT_SHA=${SHA_TAG#"sha-"}
224+
echo "COMMIT_SHA: $COMMIT_SHA"
225+
echo "COMMIT_SHA=$COMMIT_SHA" >> $GITHUB_ENV
226+
227+
if [ "${NEEDS_SEMVER}" == "true" ]; then
228+
SEMVER_TAG=$(echo "$PACKAGE_INFO" | jq --raw-output '.[0].metadata.container.tags | map(select((startswith("sha") | not) and . != "${IMAGE_TAG}")) | .[0]')
229+
else
230+
SEMVER_TAG=${IMAGE_TAG}
231+
fi
232+
echo "SEMVER_TAG: $SEMVER_TAG"
233+
echo "SEMVER_TAG=$SEMVER_TAG" >> $GITHUB_ENV
234+
env:
235+
PACKAGE_INFO: ${{ steps.package-info.outputs.result_json_output }}
236+
237+
- name: Check if commit exists on current branch
238+
id: check_commit
239+
run: |
240+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
241+
echo "BRANCH_NAME: $BRANCH_NAME"
242+
243+
if git branch --contains "$COMMIT_SHA" | grep -q "$BRANCH_NAME"; then
244+
echo "Commit $COMMIT_SHA exists on branch $BRANCH_NAME."
245+
else
246+
echo "Commit $COMMIT_SHA does not exist on $BRANCH_NAME."
247+
exit 1
248+
fi
249+
env:
250+
COMMIT_SHA: ${{ env.COMMIT_SHA }}
251+
252+
- uses: actions/create-github-app-token@v1
253+
id: app-token
254+
with:
255+
app-id: ${{ secrets.GH_APP_ID }}
256+
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
257+
258+
- name: Create Release
259+
run: |
260+
gh release create $SEMVER_TAG \
261+
--target $COMMIT_SHA \
262+
--title $SEMVER_TAG \
263+
--draft \
264+
--repo $REPOSITORY
265+
env:
266+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
267+
REPOSITORY: ${{ github.repository }}
268+
SEMVER_TAG: ${{ env.SEMVER_TAG }}
269+
COMMIT_SHA: ${{ env.COMMIT_SHA }}
270+

GitVersion.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
workflow: GitHubFlow/v1
2+
assembly-informational-format: '{FullSemVer}'
3+
major-version-bump-message: "^(build|chore|ci|docs|feat|feature|fix|bug|bugfix|perf|refactor|revert|style|test)(\\([\\w\\s-,/\\\\]*\\))?(!:|:.*\\n\\n((.+\\n)+\\n)?BREAKING CHANGE:\\s.+)"
4+
minor-version-bump-message: "^(feat|feature)(\\([\\w\\s-,/\\\\]*\\))?(:|/)"
5+
patch-version-bump-message: "^(fix|bug|bugfix|perf)(\\([\\w\\s-,/\\\\]*\\))?(:|/)"
6+
branches:
7+
main:
8+
mode: ContinuousDeployment
9+
increment: Patch
10+
feature:
11+
mode: ManualDeployment
12+
increment: Minor

scripts/kattbot-backup-db.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
pg_dump kattbot > /tmp/kattbot.bak
55
# pg_dump --data-only --exclude-table-data=public.\"__EFMigrationsHistory\" kattbot > /tmp/kattbot.bak
66

7-
# create folder if it doesnt'exit
8-
mkdir -p $HOME/kattbot-db-backups
7+
# create folder if it doesn't exist
8+
mkdir -p "$HOME/kattbot-db-backups"
99

1010
# move backup file to backups folder
11-
mv /tmp/kattbot.bak $HOME/kattbot-db-backups/kattbot-$(date +%Y-%m-%d-%H:%M).bak
11+
mv /tmp/kattbot.bak "$HOME/kattbot-db-backups/kattbot-$(date +%Y-%m-%d-%H:%M).bak"

src/Kattbot/Kattbot.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
77
</PropertyGroup>
88

9+
<PropertyGroup>
10+
<!-- AssemblySemvVer: Major.Minor.Patch.0 -->
11+
<AssemblyVersion>1.2.3.0</AssemblyVersion>
12+
<!-- AssemblySemFileVer: Major.Minor.Patch.0 -->
13+
<FileVersion>1.2.3.0</FileVersion>
14+
<!-- FullSemVer: SemVer + BuildMetaData -->
15+
<InformationalVersion>1.2.3-a.4+5</InformationalVersion>
16+
<!-- SemVer: The semantical version number, including PreReleaseTagWithDash for pre-release version numbers. -->
17+
<Version>1.2.3-a.4</Version>
18+
</PropertyGroup>
19+
920
<ItemGroup>
1021
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.1" />
1122
<PackageReference Include="TiktokenSharp" Version="1.1.5" />

src/Kattbot/Workers/BotWorker.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Threading;
1+
using System.Diagnostics;
2+
using System.Reflection;
3+
using System.Threading;
24
using System.Threading.Tasks;
35
using DSharpPlus;
46
using DSharpPlus.Entities;
@@ -43,7 +45,23 @@ private async Task ConnectToGateway()
4345
{
4446
string commandPrefix = _options.CommandPrefix;
4547

46-
var activity = new DiscordActivity($"\"{commandPrefix}help\" for help", DiscordActivityType.Playing);
48+
string? productVersion =
49+
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
50+
51+
var versionString = "?.?.?";
52+
53+
if (!string.IsNullOrWhiteSpace(productVersion))
54+
{
55+
const int gitFullShaLength = 40;
56+
57+
versionString = productVersion.Length > gitFullShaLength
58+
? productVersion[..^(gitFullShaLength + 1)]
59+
: productVersion;
60+
}
61+
62+
var activity = new DiscordActivity(
63+
$"Use {commandPrefix}help for help (v{versionString})",
64+
DiscordActivityType.Custom);
4765

4866
await _client.ConnectAsync(activity);
4967
}

0 commit comments

Comments
 (0)