-
Notifications
You must be signed in to change notification settings - Fork 0
56 lines (48 loc) · 1.87 KB
/
build-container.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
name: Build and Publish Docker Containers
on:
push:
branches:
- main
paths:
- 'environments/**/Dockerfile'
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
ENVIRONMENTS_DIR: environments
jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Extract commit SHA
run: echo "SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_ENV
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Find Docker Directories
id: find-docker-dirs
run: |
docker_dirs=$(find $ENVIRONMENTS_DIR -type f -name Dockerfile -exec dirname {} \;)
echo "DOCKER_DIRS<<EOF" >> $GITHUB_ENV
echo "${docker_dirs}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Build and Push Docker Images
run: |
while IFS= read -r dir; do
if [[ -n "$dir" ]]; then
docker_context="./$dir"
image_name=$(basename "$dir") # Extracts the name of the directory
docker build "$docker_context" -t ${{ env.REGISTRY }}/${{ github.repository_owner }}/$image_name:latest
docker push ${{ env.REGISTRY }}/${{ github.repository_owner }}/$image_name:latest
# tag the image with the short SHA
docker tag ${{ env.REGISTRY }}/${{ github.repository_owner }}/$image_name:latest ${{ env.REGISTRY }}/${{ github.repository_owner }}/$image_name:${{ env.SHORT_SHA }}
docker push ${{ env.REGISTRY }}/${{ github.repository_owner }}/$image_name:${{ env.SHORT_SHA }}
fi
done <<< "$DOCKER_DIRS"
env:
DOCKER_BUILDKIT: 1