-
Notifications
You must be signed in to change notification settings - Fork 0
126 lines (110 loc) · 4.24 KB
/
backend-docker.yml
File metadata and controls
126 lines (110 loc) · 4.24 KB
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
name: Backend Docker Build
on:
push:
branches: [main]
paths:
- 'backend/**'
- '.github/workflows/backend-docker.yml'
pull_request:
branches: [main]
paths:
- 'backend/**'
permissions:
contents: write
packages: write
actions: write
env:
IMAGE_NAME: tellmemo-backend
jobs:
build-and-push:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get version
id: version
run: |
# Get the latest backend tag or start at 0.0.0
LATEST_TAG=$(git tag -l "backend-v*" --sort=-v:refname | head -1 | sed 's/backend-v//' || echo "0.0.0")
# Increment patch version
IFS='.' read -ra VER <<< "$LATEST_TAG"
MAJOR=${VER[0]:-0}
MINOR=${VER[1]:-0}
PATCH=${VER[2]:-0}
PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "Building backend version: $NEW_VERSION"
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ./backend
file: ./backend/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ github.ref == 'refs/heads/main' && 'latest' || 'develop' }}
${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Create git tag
uses: actions/github-script@v7
with:
script: |
const tagName = 'refs/tags/backend-v${{ steps.version.outputs.version }}';
try {
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: tagName,
sha: context.sha
});
console.log(`Created tag: ${tagName}`);
} catch (error) {
if (error.status === 422) {
console.log(`Tag ${tagName} already exists, skipping creation`);
} else {
throw error;
}
}
- name: Create GitHub Release
uses: actions/github-script@v7
with:
script: |
const tagName = `backend-v${{ steps.version.outputs.version }}`;
const releaseName = `Backend v${{ steps.version.outputs.version }}`;
const branch = '${{ github.ref }}' === 'refs/heads/main' ? 'main' : 'develop';
const version = '${{ steps.version.outputs.version }}';
const sha = '${{ github.sha }}';
const dockerUsername = '${{ secrets.DOCKER_USERNAME }}';
const imageName = '${{ env.IMAGE_NAME }}';
const commitMessage = `${{ github.event.head_commit.message }}`.replace(/`/g, '\\`').replace(/\$/g, '\\$');
const bodyText = '## Backend Release v' + version + '\n\n' +
'Branch: ' + branch + '\n' +
'Commit: ' + sha + '\n\n' +
'Docker Images:\n' +
'- `' + dockerUsername + '/' + imageName + ':' + version + '`\n' +
'- `' + dockerUsername + '/' + imageName + ':' + (branch === 'main' ? 'latest' : 'develop') + '`\n' +
'- `' + dockerUsername + '/' + imageName + ':' + sha + '`\n\n' +
'Changes:\n' + commitMessage;
await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
name: releaseName,
body: bodyText,
draft: false,
prerelease: branch !== 'main'
});