-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreusable-php-docker-publish.yaml
More file actions
139 lines (109 loc) · 4.94 KB
/
reusable-php-docker-publish.yaml
File metadata and controls
139 lines (109 loc) · 4.94 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
name: Reusable PHP docker publish
on:
workflow_call:
env:
SEARCH_PATH: packages
jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: changed_files
uses: tj-actions/changed-files@v46.0.5
- name: Check if any services need deploy
id: changed_packages
env:
CHANGED_FILES: ${{ steps.changed_files.outputs.all_changed_files }}
run: |
echo "CHANGED_FILES: $CHANGED_FILES"
packages=$(echo "$CHANGED_FILES" | tr ' ' '\n' | grep "^${SEARCH_PATH}/" | awk -F '/' '{print $2}' | uniq | tr '\n' ' ')
packages=" $packages "
echo "packages=${packages}" >> "$GITHUB_OUTPUT"
echo "Found packages: $packages"
- 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" ;;
*stage) TAG_PREFIX="stage-"; TAG_SUFFIX="stage" ;;
*) echo "❌ Unsupported branch: ${BRANCH_NAME}" >&2; exit 1 ;;
esac
echo "tagPrefix=$TAG_PREFIX" >> "$GITHUB_OUTPUT"
echo "tagSuffix=$TAG_SUFFIX" >> "$GITHUB_OUTPUT"
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/setup-buildx-action@v3
- name: Build and push core image
uses: docker/build-push-action@v6
with:
file: packages/core/Dockerfile
push: true
tags: |
ghcr.io/rees46/core:${{ steps.tags.outputs.tagSuffix }}
ghcr.io/rees46/core:${{ steps.tags.outputs.tagPrefix }}${{ steps.tags.outputs.commitSha }}-${{ steps.tags.outputs.date }}
cache-from: type=gha,scope=core
cache-to: type=gha,mode=max,scope=core
- name: Build and push all docker images
if: contains(steps.changed_packages.outputs.packages, ' core ')
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
SOURCE_PATH: packages
run: |
MATCHED_FILES=$(find $SOURCE_PATH -name "Dockerfile")
echo "Trying to build packages: $MATCHED_FILES"
for package in $MATCHED_FILES; do
package_dir=$(dirname "$package")
PACKAGE_NAME=$(basename "$package_dir")
if [ "$PACKAGE_NAME" = "core" ]; then
echo "⏭️ Skipping build for core package"
continue
fi
IMAGE_NAME=ghcr.io/$REPOSITORY_OWNER/$PACKAGE_NAME:${{ steps.tags.outputs.tagPrefix }}$(git rev-parse --short HEAD)-$(date +%s)
IMAGE_NAME_LATEST=ghcr.io/$REPOSITORY_OWNER/$PACKAGE_NAME:${{ steps.tags.outputs.tagSuffix }}
IMAGE_VERSION=${{ steps.tags.outputs.tagPrefix }}$(git rev-parse --short HEAD)-$(date +%s)
echo "📦 ${IMAGE_NAME} → ${IMAGE_NAME_LATEST}"
docker buildx build . \
--file "${package}" \
--tag "${IMAGE_NAME}" \
--tag "${IMAGE_NAME_LATEST}" \
--build-arg APP_BUILD_VERSION="${IMAGE_VERSION}" \
--cache-from type=gha,scope="${PACKAGE_NAME}" \
--cache-to type=gha,scope="${PACKAGE_NAME}" \
--push \
--progress plain \
echo "✅ pushed ${IMAGE_NAME} + ${IMAGE_NAME_LATEST}"
done
- name: Build and push changed docker images
if: ${{ !contains(steps.changed_packages.outputs.packages, ' core ') }}
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
CHANGED_FILES: ${{ steps.changed_packages.outputs.packages }}
run: |
echo "Trying to build packages: $CHANGED_FILES"
for package in $CHANGED_FILES; do
if [ "$package" = "core" ]; then
echo "⏭️ Skipping build for core package"
continue
fi
IMAGE_NAME=ghcr.io/$REPOSITORY_OWNER/$package:${{ steps.tags.outputs.tagPrefix }}$(git rev-parse --short HEAD)-$(date +%s)
IMAGE_NAME_LATEST=ghcr.io/$REPOSITORY_OWNER/$package:${{ steps.tags.outputs.tagSuffix }}
IMAGE_VERSION=${{ steps.tags.outputs.tagPrefix }}$(git rev-parse --short HEAD)-$(date +%s)
echo "📦 ${IMAGE_NAME} → ${IMAGE_NAME_LATEST}"
docker buildx build . \
--file "$SEARCH_PATH/${package}/Dockerfile" \
--tag "${IMAGE_NAME}" \
--tag "${IMAGE_NAME_LATEST}" \
--build-arg APP_BUILD_VERSION="${IMAGE_VERSION}" \
--cache-from type=gha,scope="${package}" \
--cache-to type=gha,scope="${package}" \
--push \
--progress plain \
echo "✅ pushed ${IMAGE_NAME} + ${IMAGE_NAME_LATEST}"
done