-
Notifications
You must be signed in to change notification settings - Fork 172
182 lines (162 loc) · 6.85 KB
/
containers.yml
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
name: Build and Publish Container Images to Registry
on:
push:
branches: [ master ]
paths: ['src/**/VERSION']
pull_request:
branches: [ master ]
paths: ['src/**/VERSION']
jobs:
services_updated:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.version_services.outputs.matrix }}
steps:
- name: Gets commit SHA for Checkout and VERSION check
id: commit_sha
run: |
# Gets commit SHA for Checkout and VERSION check
if ${{ github.event_name == 'pull_request' }}; then
VERSION_COMMIT_SHA="${{ github.event.pull_request.head.sha }}"
else
VERSION_COMMIT_SHA="${{ github.sha }}"
fi
echo ::set-output name=version_commit_sha::$VERSION_COMMIT_SHA
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ steps.commit_sha.outputs.version_commit_sha }}
- name: Get updated service's VERSION files
id: version_services
run: |
# Gets VERSION files (e.g. src/api/VERSION) that has been updated
# updated_version_file contains the full path to the VERSION file (e.g. src/api/VERSION)
services_prep=()
for updated_version_file in $(git diff-tree --no-commit-id --name-only -r ${{ steps.commit_sha.outputs.version_commit_sha }} ${{ github.event.before }} | grep '\<src/[a-z]*/VERSION\>');
do
service=$(cut -d "/" -f 2 <<< "$updated_version_file")
echo "::group::Processing $service"
folder=${updated_version_file%"/VERSION"}
service_version=$(cat $updated_version_file)
service_platforms=$(cat $folder/PLATFORMS 2>/dev/null || echo "linux/amd64")
services_prep+=("{\"service\":\"$service\", \"version\":\"$service_version\", \"platforms\":\"$service_platforms\", \"version_file_path\":\"$updated_version_file\"}")
echo "::endgroup::"
done
echo "::set-output name=matrix::$(echo ${services_prep[@]} | jq -sc '{"include": . }')"
payload:
needs: services_updated
runs-on: ubuntu-latest
steps:
- name: Service(s) Matrix Info
run: echo ">>> ${{ needs.services_updated.outputs.matrix }} <<<"
build:
needs: services_updated
runs-on: ubuntu-latest
strategy:
matrix: ${{fromJson(needs.services_updated.outputs.matrix)}}
outputs:
service_version: ${{ steps.prepare.outputs.service_version }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx (moby/buildkit)
id: buildx
uses: docker/setup-buildx-action@v1
with:
install: true
- name: Pre-build info
run: |
# Metadata
# Get the service name and prefix it with "mushop"
# E.g. "mushop-api", "mushop-fulfillment"
IMAGE_NAME="mushop-${{ matrix.service }}"
echo "::group::Service Metadata"
echo "SERVICE >>> ${{ matrix.service }}"
echo "VERSION >>> ${{ matrix.version }}"
echo "PLATFORMS >>> ${{ matrix.platforms }}"
echo "AVAILABLE PLATFORMS >>> ${{ steps.buildx.outputs.platforms }}"
echo "IMAGE_NAME >>> ${IMAGE_NAME}"
echo "::endgroup::"
- name: Prepare parameters for build with BuildKit
id: prepare
run: |
# Get variables
echo "::group::Prepare Variables"
VERSION=${{ matrix.version }}
PLATFORMS=${{ matrix.platforms }}
FILE_PATH=${{ matrix.version_file_path }}
FOLDER=${FILE_PATH%"/VERSION"}
SERVICE=${{ matrix.service }}
DOCKER_TAGS=""
NEXT_STEP_TITLE=""
# Get the service name and prefix it with "mushop"
# E.g. "mushop-api", "mushop-fulfillment"
IMAGE_NAME="mushop-$SERVICE"
if ${{ github.event_name == 'pull_request' }}; then
IMAGE_ID=local/$IMAGE_NAME
else
IMAGE_ID=${{ secrets.DOCKER_REGISTRY_URL }}/${{ secrets.DOCKER_REPOSITORY }}/$IMAGE_NAME
fi
echo "::endgroup::"
echo "::group::Prepare Container Tags"
# Strip version for tags semver
SEMREG='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
SEM=`echo $VERSION | sed -e "s#^v##"`
TAGS=$SEM
MAJOR=`echo $SEM | sed -e "s#$SEMREG#\1#"`
MINOR=`echo $SEM | sed -e "s#$SEMREG#\2#"`
PATCH=`echo $SEM | sed -e "s#$SEMREG#\3#"`
SPECIAL=`echo $SEM | sed -e "s#$SEMREG#\4#"`
## add semantic tags
if [ "$MAJOR" != "$SEM" ] && [ -z "$SPECIAL" ]; then
TAGS="$SEM $MAJOR.$MINOR $MAJOR latest"
if [ -n "$SPECIAL" ]; then
TAGS="$MAJOR.$MINOR.$PATCH $TAGS"
fi
fi
for tag in $TAGS; do
DOCKER_TAGS+="$IMAGE_ID:$tag,"
echo "tag >>> ${tag}"
done
echo "::endgroup::"
if ${{ github.event_name == 'push' }}; then
NEXT_STEP_TITLE+="and Publish"
fi
echo ::set-output name=container_tags::$DOCKER_TAGS
echo ::set-output name=service_folder::$FOLDER
echo ::set-output name=build_platforms::$PLATFORMS
echo ::set-output name=service_version::$VERSION
echo ::set-output name=build_title::$NEXT_STEP_TITLE
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- name: Login to Container Registry
if: ${{ github.event_name == 'push' }}
uses: docker/login-action@v1
with:
registry: ${{ secrets.DOCKER_REGISTRY_URL }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build ${{ steps.prepare.outputs.build_title }} - ${{ matrix.service }}
if: success()
uses: docker/build-push-action@v2
with:
context: ${{ steps.prepare.outputs.service_folder }}
platforms: ${{ steps.prepare.outputs.build_platforms }}
push: ${{ github.event_name == 'push' }}
tags: ${{ steps.prepare.outputs.container_tags }}
labels: |
org.opencontainers.image.url='${{ github.event.repository.html_url }}/${{ steps.prepare.outputs.service_folder }}'
org.opencontainers.image.documentation='${{ github.event.repository.html_url }}/${{ steps.prepare.outputs.service_folder }}/README.md'
org.opencontainers.image.source=${{ github.event.repository.clone_url }}
org.opencontainers.image.created=${{ steps.prepare.outputs.created }}
org.opencontainers.image.version=${{ steps.prepare.outputs.service_version }}
org.opencontainers.image.revision=${{ github.sha }}
published_notification:
needs: [services_updated,build]
if: ${{ success() && github.event_name == 'push' }}
runs-on: ubuntu-latest
steps:
- name: Notify that Images have been published
run: echo "Published"