forked from RyanCNP/Backend-SaneaSP
-
Notifications
You must be signed in to change notification settings - Fork 0
182 lines (149 loc) · 6.13 KB
/
develop-actions.yml
File metadata and controls
182 lines (149 loc) · 6.13 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
name: CI / CD - develop (preview)
on:
push:
branches:
- develop
jobs:
pipeline:
runs-on: ubuntu-latest
steps:
# --- Checkout do código ---
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
# --- Validar mensagem do commit ---
- name: Validar mensagem do commit
run: |
MSG="$(git log -1 --pretty=%s)"
echo "Mensagem detectada: $MSG"
if [[ ! "$MSG" =~ ^(feat|feat!|chore|fix|refactor|docs)(\(.+\))?:|^Merge|^Revert ]]; then
echo "❌ Mensagem de commit inválida!"
echo "Use um dos formatos: feat:, feat!:, chore:, fix:, refactor:, docs:"
exit 1
fi
echo "✔ Mensagem válida."
# --- Instalação de dependências ---
- name: Dependências
run: npm install --force
# --- Instala semver globalmente ---
- name: Semver
run: npm install -g semver
# --- Calcular nova versão com sufixo preview ---
- name: Calcular nova versão Preview
id: version
run: |
git fetch --tags --force --prune
LAST_TAG=$(git tag --list 'v*' --sort=-v:refname | head -n1 || echo "v0.0.0")
CURRENT_VERSION="${LAST_TAG#v}"
NEW_VERSION=$(semver -i patch "$CURRENT_VERSION")
# Número do preview (quantas tags preview já existem para essa versão base)
COUNT=$(git tag --list "v${NEW_VERSION}-preview.*" | wc -l | xargs)
PREVIEW_VERSION="v${NEW_VERSION}-preview.$((COUNT+1))"
echo "NEW_VERSION=$PREVIEW_VERSION" >> $GITHUB_ENV
echo "new_version=$PREVIEW_VERSION" >> $GITHUB_OUTPUT
# --- Criar Release Preview ---
- name: Criar Release Preview
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.version.outputs.new_version }}
name: "Preview ${{ steps.version.outputs.new_version }}"
draft: false
prerelease: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# --- Atualizar e commitar package.json ---
- name: Atualizar versão no package.json
id: update_package
run: |
# Versão atual
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "📦 Versão atual no package.json: $CURRENT_VERSION"
# Incrementa patch
NEW_VERSION=$(npx semver -i patch "$CURRENT_VERSION")
echo "📦 Nova versão: $NEW_VERSION"
# Atualiza package.json com a nova versão
node -e "
const fs = require('fs');
const pkg = require('./package.json');
pkg.version = '$NEW_VERSION';
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2));
"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Commit da nova versão
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Ajusta o remote
git remote set-url origin https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }}.git
git add package.json
git commit -m "chore(version): bump to ${{ steps.update_package.outputs.new_version }}" || echo "Sem alterações para commitar"
git push origin develop
# ---- Imagem Docker -----
- name: Login Docker Hub
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Docker Image Back-DEV
- name: Extrai metadatas para a Imagem Back-Dev
id: metaDev
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: saneasp/front-back-dev
tags: |
type=raw,value=${{ steps.version.outputs.new_version }}
type=raw,value=latest
- name: Build e publicação da Imagem Back-Dev
id: pushDev
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
with:
context: .
file: ./Dockerfile.dev
push: true
tags: ${{ steps.metaDev.outputs.tags }}
labels: ${{ steps.metaDev.outputs.labels }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# --- Enviar e-mail em caso de falha ---
notify_failure:
name: Notificar Falha
needs: pipeline
runs-on: ubuntu-latest
if: failure()
steps:
# --- Checkout do código ---
- name: Checkout code
uses: actions/checkout@v3
# --- Capturar informações ---
- name: Capturar informações
id: info
run: |
echo "data=$(date '+%d/%m/%Y %H:%M:%S')" >> $GITHUB_OUTPUT
echo "commit=${GITHUB_SHA}" >> $GITHUB_OUTPUT
echo "branch=${GITHUB_REF_NAME}" >> $GITHUB_OUTPUT
echo "url_logs=https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" >> $GITHUB_OUTPUT
- name: Carregar e processar template
id: template
run: |
html=$(cat ./.github/email-templates/failure-pipeline.html)
html="${html//\{\{branch\}\}/${{ steps.info.outputs.branch }}}"
html="${html//\{\{data\}\}/${{ steps.info.outputs.data }}}"
html="${html//\{\{commit\}\}/${{ steps.info.outputs.commit }}}"
html="${html//\{\{url_logs\}\}/${{ steps.info.outputs.url_logs }}}"
echo "html<<EOF" >> $GITHUB_OUTPUT
echo "$html" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Enviar e-mail aos responsáveis
uses: dawidd6/action-send-mail@v3
with:
server_address: ${{ secrets.MAIL_HOST }}
server_port: ${{ secrets.MAIL_PORT }}
username: ${{ secrets.MAIL_USER }}
password: ${{ secrets.MAIL_PASS }}
subject: "🚨 Falha no Pipeline - ${{ github.ref_name }}"
to: "${{ secrets.TEAM_EMAILS }}"
from: "CI/CD SaneaSP <no-reply@saneasp.com>"
html_body: ${{ steps.template.outputs.html }}