-
Notifications
You must be signed in to change notification settings - Fork 0
145 lines (128 loc) · 5.37 KB
/
release.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
name: Build, Test, Release, and Publish
run-name: Release and Publish Docker image
on:
push:
branches:
- main
jobs:
test-app:
name: Test App
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
build-docker-prod:
name: Build Docker Image
runs-on: ubuntu-latest
needs: test-app
outputs:
version: ${{ steps.get_version.outputs.version }}
repo_owner: ${{ steps.convert_repo_owner.outputs.repo_owner }}
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Convert repository owner to lowercase
id: convert_repo_owner
run: |
repo_owner=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
echo "repo_owner=$repo_owner" >> $GITHUB_ENV
echo "::set-output name=repo_owner::$repo_owner"
- name: Get version from package.json
id: get_version
run: |
version=$(jq -r .version package.json)
echo "version=$version" >> $GITHUB_ENV
echo "::set-output name=version::$version"
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- name: Build and push Docker image with version tag
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
ghcr.io/${{ steps.convert_repo_owner.outputs.repo_owner }}/patrigma:v${{ steps.get_version.outputs.version }}
ghcr.io/${{ steps.convert_repo_owner.outputs.repo_owner }}/patrigma:latest
labels: "version=${{ steps.get_version.outputs.version }},commit=${{ github.sha }}"
create_release:
name: Create Release
runs-on: ubuntu-latest
needs: build-docker-prod
outputs:
version: ${{ needs.build-docker-prod.outputs.version }}
steps:
- name: Check if release exists
id: check_release
run: |
RELEASE_URL="https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ needs.build-docker-prod.outputs.version }}"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" $RELEASE_URL)
if [ "$RESPONSE" == "200" ]; then
echo "Release already exists."
echo "exists=true" >> $GITHUB_ENV
else
echo "Release does not exist."
echo "exists=false" >> $GITHUB_ENV
fi
- name: Delete existing release if it exists
if: env.exists == 'true'
run: |
RELEASE_ID=$(curl -s -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ needs.build-docker-prod.outputs.version }}" | jq -r '.id')
curl -s -X DELETE -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases/$RELEASE_ID"
- name: Delete existing tag if it exists
if: env.exists == 'true'
run: |
TAG_URL="https://api.github.com/repos/${{ github.repository }}/git/refs/tags/v${{ needs.build-docker-prod.outputs.version }}"
curl -s -X DELETE -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" $TAG_URL
- name: Create GitHub Release
if: env.exists == 'false' || env.exists == 'true'
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
with:
tag_name: "v${{ needs.build-docker-prod.outputs.version }}"
release_name: "Release v${{ needs.build-docker-prod.outputs.version }}"
draft: false
prerelease: false
body: "Docker image for this release: https://ghcr.io/${{ needs.build-docker-prod.outputs.repo_owner }}/patrigma:v${{ needs.build-docker-prod.outputs.version }}"
notify:
runs-on: ubuntu-latest
needs: create_release
steps:
- name: Notify Discord on success
if: success()
uses: containrrr/shoutrrr-action@v1
with:
url: ${{ secrets.NOTIFICATION_URL }}
title: "Build Succeeded for Release v${{ needs.create_release.outputs.version }}"
message: |
The build for release v${{ needs.create_release.outputs.version }} was successful.
Commit message: ${{ github.event.head_commit.message }}
Commit SHA: ${{ github.sha }}
The Docker image has been published successfully.
- name: Notify Discord on failure
if: failure()
uses: containrrr/shoutrrr-action@v1
with:
url: ${{ secrets.NOTIFICATION_URL }}
title: "Build Failed for Release v${{ needs.create_release.outputs.version }}"
message: |
The build for release v${{ needs.create_release.outputs.version }} has failed.
Commit message: ${{ github.event.head_commit.message }}
Commit SHA: ${{ github.sha }}
Check the build logs for details.