-
Notifications
You must be signed in to change notification settings - Fork 2
134 lines (120 loc) · 4.6 KB
/
deploy-docker.yaml
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
name: Build and Publish Docker image
on:
workflow_call:
inputs:
packages:
description: Packages to build and publish
required: true
type: string
environment:
description: Environment to build and publish e.g. prod, beta, dev
required: true
type: string
container-registry:
description: Container registry to push the image to
default: ghcr.io
type: string
image-prefix:
description: Image prefix of the built image
type: string
gitops-repository:
description: Target repository for updating deployment declaration
type: string
default: ${{ github.repository }}
gitops-ref:
description: Target ref for updating deployment declaration
type: string
default: master
update-mode:
description: Mode of updating deployment declaration, pr or commit
type: string
default: pr
push:
description: Enable pushing the image to the container registry
type: boolean
default: true
update:
description: Enable updating deployment declaration in the target repository
type: boolean
default: true
secrets:
GH_TOKEN:
description: GitHub token used to checkout target repository and open PR
required: true
jobs:
build-and-publish-docker-image:
name: Build and Publish Docker image
runs-on: ubuntu-latest
strategy:
matrix:
packages: ${{ fromJson(inputs.packages) }}
outputs:
DOCKERFILE_EXISTS: ${{ steps.check-dockerfile.outputs.DOCKERFILE_EXISTS }}
steps:
- name: Checkout with tags
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ matrix.packages.ref }}
- name: Check if Dockerfile exists
id: check-dockerfile
run: |
if [ ! -f apps/${{ matrix.packages.name }}/Dockerfile ]; then
echo "Dockerfile does not exist"
echo "DOCKERFILE_EXISTS=false" >> $GITHUB_OUTPUT
else
echo "Dockerfile exists"
echo "DOCKERFILE_EXISTS=true" >> $GITHUB_OUTPUT
fi
- name: Set up Docker Buildx
if: steps.check-dockerfile.outputs.DOCKERFILE_EXISTS == 'true'
uses: docker/setup-buildx-action@v2
- name: Login to Container Registry
if: steps.check-dockerfile.outputs.DOCKERFILE_EXISTS == 'true'
uses: docker/login-action@v1
with:
registry: ${{ inputs.container-registry }}
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Set frontend env
if: ${{ contains(fromJson('["web", "admin-web"]'), matrix.packages.name) && steps.check-dockerfile.outputs.DOCKERFILE_EXISTS == 'true' }}
env:
APP_NAME: ${{ matrix.packages.name }}
BRANCH: ${{ github.ref_name }}
run: |
if [[ $BRANCH == 'main' && -f "apps/$APP_NAME/.env.prod" ]]; then
mv apps/$APP_NAME/.env.prod apps/$APP_NAME/.env
echo "$APP_NAME env is set to production"
elif [[ $BRANCH == 'beta' && -f "apps/$APP_NAME/.env.beta" ]]; then
mv apps/$APP_NAME/.env.beta apps/$APP_NAME/.env
echo "$APP_NAME env is set to beta"
elif [[ $BRANCH == 'dev' && -f "apps/$APP_NAME/.env.dev" ]]; then
mv apps/$APP_NAME/.env.dev apps/$APP_NAME/.env
echo "$APP_NAME env is set to dev"
else
echo "$APP_NAME env is not set. This could be because branch '$BRANCH' is not in [main, beta, dev] or .env file for the branch is not found"
fi
- name: Build and push Docker image
if: steps.check-dockerfile.outputs.DOCKERFILE_EXISTS == 'true'
uses: docker/build-push-action@v3
with:
context: .
file: apps/${{ matrix.packages.name }}/Dockerfile
tags: ${{ inputs.container-registry }}/${{ inputs.image-prefix }}/${{ matrix.packages.name }}:${{ matrix.packages.imageTag }}
push: ${{ inputs.push }}
cache-from: type=gha
cache-to: type=gha,mode=max
update-gitops:
needs: build-and-publish-docker-image
if: inputs.update
uses: ./.github/workflows/update-gitops.yaml
with:
packages: ${{ inputs.packages }}
environment: ${{ inputs.environment }}
image-prefix: ${{ inputs.image-prefix }}
gitops-repository: ${{ inputs.gitops-repository }}
gitops-ref: ${{ inputs.gitops-ref }}
container-registry: ${{ inputs.container-registry }}
mode: ${{ inputs.update-mode }}
secrets:
GH_TOKEN: ${{ secrets.GH_TOKEN }}