-
Notifications
You must be signed in to change notification settings - Fork 2
150 lines (126 loc) · 4.58 KB
/
container-build.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
name: Build & Push Container
on:
workflow_dispatch:
permissions:
contents: write
packages: write
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
merge-branches-with-no-ff:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: merge into test
if: github.ref_name == 'test'
uses: ./.github/workflows/actions/merge-ff-only
with:
target-ref: "test"
base-ref: "main"
- name: merge into production
if: github.ref_name == 'production'
uses: ./.github/workflows/actions/merge-ff-only
with:
target-ref: "production"
base-ref: "test"
container-prerequisites:
runs-on: ubuntu-latest
needs: merge-branches-with-no-ff
outputs:
version: ${{ steps.extract-version.outputs.version }}
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Get version tag of HEAD if any
id: extract-version
run: |
git fetch --prune --tags
echo "version=`git tag -l --points-at=HEAD | sed -En 's/^v(.*)/\1/p'`" >> $GITHUB_OUTPUT
# If the current commit has no version tag, an image needs to be
# built, tagged and pushed to the registry
# If the build runs on test but test has already been built
# with the latest tag, nothing happens
container-build-and-push-image:
runs-on: ubuntu-latest
needs: container-prerequisites
if: needs.container-prerequisites.outputs.version == ''
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
# Make sure we actually checkout the HEAD with the previously
# merged commits from main, instead of the latest commit of the
# release branch at the time this workflow has been started
# (before merge)
- name: Checkout latest commit
run: |
git fetch --prune --tags
git checkout HEAD
git log -n 1
- name: Read .nvmrc
run: echo "NODE_VERSION=$(cat .nvmrc)" >> $GITHUB_OUTPUT
id: nvm
- name: Use Node.js ${{ steps.nvm.outputs.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ steps.nvm.outputs.NODE_VERSION }}
cache: "npm"
- name: Install dependencies 🔧
run: npm ci
- name: Build application 🏭
run: |
npm run build
mv dist evento-portal
mkdir dist
mv evento-portal dist
- name: Bump Git version tag and push it
id: tag
uses: anothrNick/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INITIAL_VERSION: 1.0.0
WITH_V: true
- name: Login to Docker registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Determine environment and version tags based on Git & Docker registry
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ steps.tag.outputs.new_tag }}-${{ github.ref_name }}
type=semver,pattern={{version}},value=${{ steps.tag.outputs.new_tag }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
# If the current commit has a version tag, the existing image should
# be reused by retagging and pushing it
container-retag-and-push-image:
runs-on: ubuntu-latest
needs: container-prerequisites
if: needs.container-prerequisites.outputs.version != ''
steps:
- name: Login to Docker registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Retag existing image & push
env:
VERSION_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.container-prerequisites.outputs.version }}
BRANCH_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}
BRANCH_VERSION_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:v${{ needs.container-prerequisites.outputs.version }}-${{ github.ref_name }}
run: |
docker pull ${{ env.VERSION_IMAGE }}
docker tag ${{ env.VERSION_IMAGE }} ${{ env.BRANCH_VERSION_IMAGE }}
docker push ${{ env.BRANCH_VERSION_IMAGE }}