-
-
Notifications
You must be signed in to change notification settings - Fork 1
169 lines (144 loc) · 5.94 KB
/
ci.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
name: Test, Build, Publish, Release
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
name: 🔬 Linting, 📊 PyTest
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- name: Set up .env
run: cp .env.example .env
- name: Install poetry
run: pipx install poetry
- name: View poetry
run: poetry --version
- name: 🐍 Set up Python
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c
with:
python-version: 3.12
cache: "poetry"
- name: Add Poe the Poet plugin
run: poetry self add 'poethepoet[poetry_plugin]'
- name: Install dependencies
run: poetry install --no-root --with dev,lint,test
- name: Python Format (autoflake/isort/black)
run: poetry poe formatters
- name: Python Lint (pylint)
run: poetry poe linters
- name: Python Typecheck (mypy)
run: poetry poe typings
# - name: Run tests
# env:
# ENVIRONMENT: TEST # This disables New Relic initialization
# run: poetry poe tests
build:
name: 🐳 Build and publish docker images
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
# if branch is main, use prod
BUILD_NAME: ${{ github.ref == 'refs/heads/main' && 'prod' }}
# if the event is push and the commit message starts with `bump:`, upload the image
UPLOAD_IMAGE: ${{ github.event_name == 'push' && startsWith(github.event.head_commit.message , 'bump:') }}
needs: test
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# Get the latest tag
- name: Get latest tag
id: latest_tag
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
with:
script: |
const repo = context.repo;
const tags = await github.rest.repos.listTags(repo);
if (tags.data.length > 0) {
const latestTag = tags.data[0].name;
console.log(`Latest tag: ${latestTag}`);
return latestTag;
} else {
console.log('No tags found; using default version 0.0.1');
return '0.0.1';
}
# Set the version from the latest tag or from the build name
# If the build is triggered by a push event and the commit message starts with `bump:`
# then the version is set to the latest tag
# Otherwise, the version is set to the build name
# This is used to tag the Docker image
- name: Set version
run: echo "VERSION=${{ steps.latest_tag.outputs.result || env.BUILD_NAME }}" >> $GITHUB_ENV
- name: Echo GITHUB_ENV
run: |
echo $GITHUB_ENV
# Log into a Docker registry
# The login is performed only if the event is push and the commit message starts with `bump:`
- name: Login into registry ${{ env.REGISTRY }}
if: ${{ env.UPLOAD_IMAGE != 'false' }}
uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# Metadata is extracted only if the event is push and the commit message starts with `bump:`
- name: Extract Docker metadata
if: ${{ env.UPLOAD_IMAGE != 'false' }}
id: meta
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81
with:
tags: |
type=edge,branch=${{ github.ref }}
type=raw,value=${{ env.BUILD_NAME }}
type=ref,event=branch
type=semver,pattern={{version}},value=${{ github.ref == 'refs/heads/main' && env.VERSION || format('{0}-{1}', env.VERSION, github.ref_name) }}
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
labels: |
org.opencontainers.image.description=Slick Telemetry rearwing ${{ env.BUILD_NAME }} image
org.opencontainers.image.version=${{ env.VERSION }}
# Build and upload Docker image to GHCR with Buildx
# The image is uploaded only if the event is push and the commit message starts with `bump:`
- name: Build and push
id: docker_build
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0
with:
context: .
file: ./Dockerfile.${{ env.BUILD_NAME }}
push: ${{ env.UPLOAD_IMAGE }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
release:
# create a release following the logic below:
# 1. branch is `main` AND
# 2. workflow is triggered by a push event AND
# 3. the head commit's commit message does NOT starts with `bump:`
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' && ! startsWith(github.event.head_commit.message , 'bump:') }}
name: ⬆️ Bump version and create changelog with a GitHub release
needs: build
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
fetch-depth: 0
token: ${{ secrets.SVC_PAT }}
- name: Create bump and changelog
uses: commitizen-tools/commitizen-action@1f11eb222996406681d2bfa1eb3d997eca46557c
with:
github_token: ${{ secrets.SVC_PAT }}
changelog_increment_filename: body.md
- name: Release
uses: softprops/action-gh-release@d99959edae48b5ffffd7b00da66dcdb0a33a52ee
with:
body_path: "body.md"
tag_name: ${{ env.REVISION }} # this is the version set in the previous step
token: ${{ secrets.SVC_PAT }}
generate_release_notes: true