Skip to content

Commit 2d23d6a

Browse files
authored
ci: update docker-build-push.yml
1 parent ded4238 commit 2d23d6a

File tree

1 file changed

+67
-10
lines changed

1 file changed

+67
-10
lines changed

.github/workflows/docker-build-push.yml

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
steps:
2323
- name: Checkout code
2424
uses: actions/checkout@v4
25+
with:
26+
fetch-depth: 0
2527

2628
- name: Set up QEMU
2729
uses: docker/setup-qemu-action@v3
@@ -41,13 +43,60 @@ jobs:
4143
VERSION=$(node -p "require('./package.json').version")
4244
echo "version=$VERSION" >> $GITHUB_OUTPUT
4345
46+
- name: Determine versioning strategy
47+
id: versioning
48+
run: |
49+
BASE_VERSION="v${{ steps.package-version.outputs.version }}"
50+
51+
# Check if tag already exists
52+
if git rev-parse "$BASE_VERSION" >/dev/null 2>&1; then
53+
# Find the highest build number for this version
54+
BUILD_PATTERN="${BASE_VERSION}-"
55+
HIGHEST_BUILD=0
56+
57+
for tag in $(git tag -l "${BUILD_PATTERN}*"); do
58+
# Extract build number from tag
59+
BUILD_NUM=$(echo $tag | sed "s|${BUILD_PATTERN}||")
60+
if [[ "$BUILD_NUM" =~ ^[0-9]+$ ]] && [ $BUILD_NUM -gt $HIGHEST_BUILD ]; then
61+
HIGHEST_BUILD=$BUILD_NUM
62+
fi
63+
done
64+
65+
# Increment for the new build
66+
NEW_BUILD=$((HIGHEST_BUILD + 1))
67+
VERSION="${BASE_VERSION}-${NEW_BUILD}"
68+
echo "New build for existing version: $VERSION"
69+
else
70+
VERSION="$BASE_VERSION"
71+
echo "New version: $VERSION"
72+
fi
73+
74+
echo "full_version=$VERSION" >> $GITHUB_OUTPUT
75+
echo "is_new_base_version=$([[ "$VERSION" == "$BASE_VERSION" ]] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
76+
4477
- name: Login to GitHub Container Registry
4578
uses: docker/login-action@v3
4679
with:
4780
registry: ghcr.io
4881
username: ${{ github.repository_owner }}
4982
password: ${{ secrets.GITHUB_TOKEN }}
5083

84+
- name: Get previous tag
85+
id: previoustag
86+
uses: actions/github-script@v6
87+
with:
88+
script: |
89+
const { data: tags } = await github.rest.repos.listTags({
90+
owner: context.repo.owner,
91+
repo: context.repo.repo,
92+
per_page: 2
93+
});
94+
if (tags.length > 1) {
95+
return tags[1].name;
96+
}
97+
return '';
98+
result-encoding: string
99+
51100
- name: Build and push
52101
uses: docker/build-push-action@v5
53102
with:
@@ -56,29 +105,37 @@ jobs:
56105
push: true
57106
tags: |
58107
joanroig/admincraft-websocket:latest
59-
joanroig/admincraft-websocket:${{ steps.package-version.outputs.version }}
108+
joanroig/admincraft-websocket:${{ steps.versioning.outputs.full_version }}
60109
ghcr.io/${{ github.repository_owner }}/admincraft-websocket:latest
61-
ghcr.io/${{ github.repository_owner }}/admincraft-websocket:${{ steps.package-version.outputs.version }}
110+
ghcr.io/${{ github.repository_owner }}/admincraft-websocket:${{ steps.versioning.outputs.full_version }}
62111
labels: |
63112
org.opencontainers.image.source=https://github.com/${{ github.repository_owner }}/admincraft-websocket
64113
org.opencontainers.image.description=Admincraft WebSocket server
65-
org.opencontainers.image.version=${{ steps.package-version.outputs.version }}
114+
org.opencontainers.image.version=${{ steps.versioning.outputs.full_version }}
115+
116+
- name: Create Git Tag
117+
run: |
118+
git config user.name github-actions
119+
git config user.email [email protected]
120+
git tag ${{ steps.versioning.outputs.full_version }}
121+
git push origin ${{ steps.versioning.outputs.full_version }}
122+
echo "Created and pushed tag ${{ steps.versioning.outputs.full_version }}"
66123
67124
- name: Create GitHub Release
68125
id: create_release
69126
uses: actions/create-release@v1
70127
env:
71128
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72129
with:
73-
tag_name: v${{ steps.package-version.outputs.version }}
74-
release_name: Release v${{ steps.package-version.outputs.version }}
130+
tag_name: ${{ steps.versioning.outputs.full_version }}
131+
release_name: Release ${{ steps.versioning.outputs.full_version }}
75132
draft: false
76-
prerelease: false
133+
prerelease: ${{ steps.versioning.outputs.is_new_base_version == 'false' }}
77134
body: |
78-
Release v${{ steps.package-version.outputs.version }}
135+
Release ${{ steps.versioning.outputs.full_version }}
79136
80137
Docker images available at:
81-
- Docker Hub: [joanroig/admincraft-websocket:${{ steps.package-version.outputs.version }}](https://hub.docker.com/r/joanroig/admincraft-websocket/tags?name=${{ steps.package-version.outputs.version }})
82-
- GitHub Packages: [ghcr.io/${{ github.repository_owner }}/admincraft-websocket:${{ steps.package-version.outputs.version }}](https://github.com/${{ github.repository_owner }}/admincraft-websocket/pkgs/container/admincraft-websocket)
138+
- Docker Hub: [joanroig/admincraft-websocket:${{ steps.versioning.outputs.full_version }}](https://hub.docker.com/r/joanroig/admincraft-websocket/tags?name=${{ steps.versioning.outputs.full_version }})
139+
- GitHub Packages: [ghcr.io/${{ github.repository_owner }}/admincraft-websocket:${{ steps.versioning.outputs.full_version }}](https://github.com/${{ github.repository_owner }}/admincraft-websocket/pkgs/container/admincraft-websocket)
83140
84-
Full Changelog: https://github.com/${{ github.repository_owner }}/admincraft-websocket/commits/v${{ steps.package-version.outputs.version }}
141+
Full Changelog: https://github.com/${{ github.repository_owner }}/admincraft-websocket/compare/${{ steps.previoustag.outputs.result }}...${{ steps.versioning.outputs.full_version }}

0 commit comments

Comments
 (0)