-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (148 loc) · 5.35 KB
/
build-test-release.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: Build test and release
on:
push:
branches:
- main
paths-ignore:
- 'charts/**'
- 'docs/**'
- '.github/**'
- 'README.md'
pull_request:
branches:
- main
paths-ignore:
- 'charts/**'
- 'docs/**'
- '.github/**'
- 'README.md'
workflow_dispatch:
jobs:
prepare:
name: Preparing build context
runs-on: ubuntu-latest
outputs:
SANITISED_REPOSITORY_NAME: ${{ steps.get_env.outputs.SANITISED_REPOSITORY_NAME }}
DOCKER_IMAGE: ${{ steps.get_env.outputs.DOCKER_IMAGE }}
GITHUB_COMMIT_NUMBER: ${{ steps.get_env.outputs.GITHUB_COMMIT_NUMBER }}
GITHUB_SHA: ${{ steps.get_env.outputs.GITHUB_SHA }}
DOCKER_IMAGE_SEMVER: ${{ steps.semver.outputs.semantic_version }}
CHECK_IF_MASTER_BRANCH: ${{ steps.get_env.outputs.CHECK_FOR_MASTER_BRANCH }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
fetch-depth: '0'
- name: Configure git for private modules
uses: extractions/netrc@v1
with:
machine: github.com
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- name: Set environment variables
id: get_env
run: |
TMP_SANITISED_REPOSITORY_NAME=$(echo ${{ github.event.repository.name }} | sed -e 's|\.|-|g')
CHECK_FOR_MASTER_BRANCH="false"
echo "SANITISED_REPOSITORY_NAME=$TMP_SANITISED_REPOSITORY_NAME" >> $GITHUB_OUTPUT
echo "DOCKER_IMAGE=ghcr.io/${{ github.repository_owner }}/$TMP_SANITISED_REPOSITORY_NAME" >> $GITHUB_OUTPUT
echo "GITHUB_COMMIT_NUMBER=$(git rev-list --count HEAD)" >> $GITHUB_OUTPUT
echo "GITHUB_SHA=$(echo ${GITHUB_SHA::8})" >> $GITHUB_OUTPUT
if [[ ${{ github.ref }} == 'refs/heads/master' || ${{ github.ref }} == 'refs/heads/main' ]]; then
CHECK_FOR_MASTER_BRANCH="true"
fi
echo "CHECK_FOR_MASTER_BRANCH=$CHECK_FOR_MASTER_BRANCH" >> $GITHUB_OUTPUT
- name: Establish semver
id: semver
uses: lukaszraczylo/semver-generator@v1
with:
config_file: semver.yaml
repository_local: true
github_username: ${{ github.actor }}
github_token: ${{ secrets.GHCR_TOKEN }}
- name: Semver check
run: |
echo "Semantic version detected: ${{ steps.semver.outputs.semantic_version }}"
test:
name: "Unit testing"
needs: [prepare]
runs-on: ubuntu-20.04
# container: github/super-linter:v4
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.21
- name: Configure git for private modules
uses: extractions/netrc@v1
with:
machine: github.com
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Run unit tests
run: |
make test
build-docker:
name: "Building docker image"
needs: [ prepare, test ]
runs-on: ubuntu-20.04
if: needs.prepare.outputs.CHECK_IF_MASTER_BRANCH == 'true'
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GHCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Build and push
if: github.event_name != 'pull_request' && ${{ needs.prepare.outputs.CHECK_FOR_MASTER_BRANCH == 'true' }}
run: |
make docker-buildx IMG=${{ needs.prepare.outputs.DOCKER_IMAGE }}:${{ needs.prepare.outputs.DOCKER_IMAGE_SEMVER }} IMG_SECONDARY_TAG=${{ needs.prepare.outputs.DOCKER_IMAGE }}:latest
release:
name: Create Release
runs-on: ubuntu-latest
needs: [ prepare, build-docker ]
if: needs.prepare.outputs.CHECK_IF_MASTER_BRANCH == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get list of the commits since last release
run: |
echo "$(git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%h %s")" > .release_notes
- name: Create Release
id: create_release
if: needs.prepare.outputs.CHECK_IF_MASTER_BRANCH == 'true'
uses: softprops/action-gh-release@v1
with:
name: v${{ needs.prepare.outputs.DOCKER_IMAGE_SEMVER }}
body_path: .release_notes
token: ${{ secrets.GHCR_TOKEN }}
tag_name: v${{ needs.prepare.outputs.DOCKER_IMAGE_SEMVER }}
prerelease: ${{ github.ref != 'refs/heads/master' && github.ref != 'refs/heads/main' }}