Skip to content

Commit ea9c32b

Browse files
authored
ci : fix docker build number and tag name (#9638)
* ci : fix docker build number and tag name * fine-grant permissions
1 parent 1e43630 commit ea9c32b

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

.dockerignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
*.o
22
*.a
33
.cache/
4-
.git/
4+
# Do not ignore .git directory, otherwise the reported build number will always be 0
55
.github/
66
.gitignore
77
.vs/

.github/workflows/docker.yml

+38-20
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ on:
1515
branches:
1616
- master
1717
paths: ['.github/workflows/docker.yml', '.devops/*.Dockerfile', '**/CMakeLists.txt', '**/Makefile', '**/*.h', '**/*.hpp', '**/*.c', '**/*.cpp', '**/*.cu', '**/*.cuh', '**/*.swift', '**/*.m', '**/*.metal']
18+
workflow_dispatch: # allows manual triggering, useful for debugging
1819

1920
concurrency:
2021
group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}
2122
cancel-in-progress: true
2223

24+
# Fine-grant permission
25+
# https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token
26+
permissions:
27+
packages: write
28+
2329
jobs:
2430
push_to_registry:
2531
name: Push Docker image to Docker Hub
@@ -46,6 +52,8 @@ jobs:
4652
steps:
4753
- name: Check out the repo
4854
uses: actions/checkout@v4
55+
with:
56+
fetch-depth: 0 # preserve git history, so we can determine the build number
4957

5058
- name: Set up QEMU
5159
uses: docker/setup-qemu-action@v2
@@ -60,6 +68,34 @@ jobs:
6068
username: ${{ github.repository_owner }}
6169
password: ${{ secrets.GITHUB_TOKEN }}
6270

71+
- name: Determine tag name
72+
id: tag
73+
shell: bash
74+
run: |
75+
BUILD_NUMBER="$(git rev-list --count HEAD)"
76+
SHORT_HASH="$(git rev-parse --short=7 HEAD)"
77+
REPO_OWNER="${GITHUB_REPOSITORY_OWNER@L}" # to lower case
78+
REPO_NAME="${{ github.event.repository.name }}"
79+
80+
# determine tag name postfix (build number, commit hash)
81+
if [[ "${{ env.GITHUB_BRANCH_NAME }}" == "master" ]]; then
82+
TAG_POSTFIX="b${BUILD_NUMBER}"
83+
else
84+
SAFE_NAME=$(echo "${{ env.GITHUB_BRANCH_NAME }}" | tr '/' '-')
85+
TAG_POSTFIX="${SAFE_NAME}-${SHORT_HASH}"
86+
fi
87+
88+
# list all tags possible
89+
TAGS=""
90+
TAGS="${TAGS}ghcr.io/${REPO_OWNER}/${REPO_NAME}:${{ matrix.config.tag }},"
91+
TAGS="${TAGS}ghcr.io/${REPO_OWNER}/${REPO_NAME}:${{ matrix.config.tag }}-${TAG_POSTFIX}"
92+
93+
echo "output_tags=$TAGS" >> $GITHUB_OUTPUT
94+
echo "output_tags=$TAGS" # print out for debugging
95+
env:
96+
GITHUB_BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
97+
GITHUB_REPOSITORY_OWNER: '${{ github.repository_owner }}'
98+
6399
# https://github.com/jlumbroso/free-disk-space/tree/54081f138730dfa15788a46383842cd2f914a1be#example
64100
- name: Free Disk Space (Ubuntu)
65101
uses: jlumbroso/free-disk-space@main
@@ -77,31 +113,13 @@ jobs:
77113
docker-images: true
78114
swap-storage: true
79115

80-
- name: Determine tag name
81-
id: tag
82-
shell: bash
83-
run: |
84-
BUILD_NUMBER="$(git rev-list --count HEAD)"
85-
SHORT_HASH="$(git rev-parse --short=7 HEAD)"
86-
if [[ "${{ env.BRANCH_NAME }}" == "master" ]]; then
87-
echo "name=b${BUILD_NUMBER}" >> $GITHUB_OUTPUT
88-
else
89-
SAFE_NAME=$(echo "${{ env.BRANCH_NAME }}" | tr '/' '-')
90-
echo "name=${SAFE_NAME}-b${BUILD_NUMBER}-${SHORT_HASH}" >> $GITHUB_OUTPUT
91-
fi
92-
93-
- name: Downcase github.repository_owner
94-
run: |
95-
echo "repository_owner_lowercase=${GITHUB_REPOSITORY_OWNER@L}" >> $GITHUB_ENV
96-
env:
97-
GITHUB_REPOSITORY_OWNER: '${{ github.repository_owner }}'
98-
99116
- name: Build and push Docker image (tagged + versioned)
100117
if: github.event_name == 'push'
101118
uses: docker/build-push-action@v6
102119
with:
103120
context: .
104121
push: true
105122
platforms: ${{ matrix.config.platforms }}
106-
tags: "ghcr.io/${{ env.repository_owner_lowercase }}/llama.cpp:${{ matrix.config.tag }}-${{ env.COMMIT_SHA }},ghcr.io/${{ env.repository_owner_lowercase }}/llama.cpp:${{ matrix.config.tag }},ghcr.io/${{ env.repository_owner_lowercase }}/llama.cpp:${{ matrix.config.tag }}-${{ steps.tag.outputs.name }}"
123+
# tag list is generated from step above
124+
tags: ${{ steps.tag.outputs.output_tags }}
107125
file: ${{ matrix.config.dockerfile }}

0 commit comments

Comments
 (0)