-
Notifications
You must be signed in to change notification settings - Fork 0
142 lines (119 loc) · 4.92 KB
/
docker-publish.yaml
File metadata and controls
142 lines (119 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: Reusable docker publish
on:
workflow_call:
secrets:
appSecret:
description: GitHub App private key (PEM) for generating a token
required: false
inputs:
appId:
description: GitHub App ID for generating a token
required: false
type: string
appInstallationId:
description: Optional GitHub App installation ID
required: false
type: string
packageName:
description: Name of the package to publish
required: false
type: string
contextLocation:
description: Location of context
required: false
type: string
default: .
jobs:
run:
name: Build and publish docker image
runs-on: ubuntu-latest
steps:
- uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- id: app-token
if: ${{ inputs.appId }}
uses: actions/create-github-app-token@v3
with:
app-id: ${{ inputs.appId }}
private-key: ${{ secrets.appSecret }}
owner: ${{ github.repository_owner }}
- uses: actions/checkout@v6
with:
submodules: 'true'
token: ${{ steps.app-token.outputs.token || github.token }}
- uses: docker/setup-buildx-action@v4
- name: Set tags
id: tags
env:
BRANCH_NAME: ${{ github.ref_name }}
run: |
echo "commitSha=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "date=$(date +%s)" >> "$GITHUB_OUTPUT"
case "${BRANCH_NAME}" in
*master) TAG_PREFIX=""; TAG_SUFFIX="latest" ;;
*rust) TAG_PREFIX=""; TAG_SUFFIX="latest" ;;
*go) TAG_PREFIX=""; TAG_SUFFIX="latest" ;;
*stage) TAG_PREFIX="stage-"; TAG_SUFFIX="stage" ;;
*) TAG_PREFIX=""; TAG_SUFFIX="latest" ;;
esac
echo "tagPrefix=$TAG_PREFIX" >> "$GITHUB_OUTPUT"
echo "tagSuffix=$TAG_SUFFIX" >> "$GITHUB_OUTPUT"
- name: Build and push
if: inputs.packageName
uses: docker/build-push-action@v7
with:
push: true
tags: |
ghcr.io/rees46/${{ inputs.packageName }}:${{ steps.tags.outputs.tagSuffix }}
ghcr.io/rees46/${{ inputs.packageName }}:${{ steps.tags.outputs.tagPrefix }}${{ steps.tags.outputs.commitSha }}-${{ steps.tags.outputs.date }}
cache-from: type=gha,scope=${{ inputs.packageName }}
cache-to: type=gha,mode=max,scope=${{ inputs.packageName }}
context: ${{ inputs.contextLocation }}
- if: ${{ !inputs.packageName }}
uses: crazy-max/ghaction-github-runtime@v4
- name: Build and publish all found docker images
if: ${{ !inputs.packageName }}
env:
CONTEXT_LOCATION: ${{ inputs.contextLocation }}
BUILD_ID: ${{ steps.tags.outputs.date }}
GIT_SHA: ${{ steps.tags.outputs.commitSha }}
TAG_PREFIX: ${{ steps.tags.outputs.tagPrefix }}
TAG_SUFFIX: ${{ steps.tags.outputs.tagSuffix }}
REPOSITORY_OWNER: ${{ github.repository_owner }}
run: |
set -uo pipefail
REPOSITORY_OWNER_CLEAN=$(echo "$REPOSITORY_OWNER" | tr '[:upper:]' '[:lower:]')
echo "🏗️ Начинаем билдить образы, не падаем на первом фейле, держимся до конца..."
FAILED_IMAGES=()
while IFS= read -r -d '' DOCKERFILE; do
REL_PATH="${DOCKERFILE}"
IMAGE_BASENAME="$(basename $(dirname "${REL_PATH}" | tr '_' '-'))"
[[ "${IMAGE_BASENAME}" == "." ]] && IMAGE_BASENAME="$(basename "${CONTEXT_LOCATION}")"
IMAGE_TAG="${TAG_PREFIX}${GIT_SHA}-${BUILD_ID}"
IMAGE="ghcr.io/${REPOSITORY_OWNER_CLEAN}/${IMAGE_BASENAME}:${IMAGE_TAG}"
IMAGE_LATEST="ghcr.io/${REPOSITORY_OWNER_CLEAN}/${IMAGE_BASENAME}:${TAG_SUFFIX}"
echo "📦 ${IMAGE_BASENAME} → ${IMAGE}"
if docker buildx build \
--file "${DOCKERFILE}" \
--tag "${IMAGE}" \
--tag "${IMAGE_LATEST}" \
--cache-from type=gha,scope="${IMAGE_BASENAME}" \
--cache-to type=gha,scope="${IMAGE_BASENAME}" \
--push \
--progress plain \
"${CONTEXT_LOCATION}"; then
echo "✅ Успех: ${IMAGE_BASENAME}"
else
echo "❌ Фейл: ${IMAGE_BASENAME}" >&2
FAILED_IMAGES+=("${IMAGE_BASENAME}")
fi
done < <(find "${CONTEXT_LOCATION}" -name Dockerfile -type f -print0)
if [ "${#FAILED_IMAGES[@]}" -ne 0 ]; then
echo "⚠️ Завалились образы: ${FAILED_IMAGES[*]}"
exit 1
else
echo "🎉 Все образы успешно запушены."
fi