feat: CI/CD - Conventional Commits e Semantic Release
This commit is contained in:
211
.gitea/workflows/ci-and-cd.yml
Normal file
211
.gitea/workflows/ci-and-cd.yml
Normal file
@ -0,0 +1,211 @@
|
||||
# .gitea/workflows/ci-and-cd.yml
|
||||
name: CI & CD
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
lint_commits:
|
||||
name: Lint Commits
|
||||
runs-on: [self-hosted]
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Install Dependencies
|
||||
env:
|
||||
NODE_ENV: development
|
||||
NPM_CONFIG_PRODUCTION: 'false'
|
||||
run: npm ci --no-audit
|
||||
|
||||
- name: Lint Commit Messages
|
||||
# Se qualquer mensagem não corresponder ao Conventional Commits, este step falha
|
||||
run: npx commitlint --from=origin/master --to=HEAD
|
||||
|
||||
release:
|
||||
name: Release
|
||||
needs: lint_commits # só roda se lint_commits passar
|
||||
runs-on: [self-hosted]
|
||||
outputs:
|
||||
sha_short: ${{ steps.commit_short.outputs.sha_short }}
|
||||
is_tagged: ${{ steps.check_tag.outputs.is_tagged }}
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
|
||||
- name: Commit Short Hash
|
||||
id: commit_short
|
||||
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: Install Dependencies
|
||||
run: npm ci --no-audit
|
||||
|
||||
- name: Run Semantic Release
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
GITEA_URL: https://git.dejodigital.com.br
|
||||
run: npx semantic-release
|
||||
|
||||
- name: Check if Release Tag Exists
|
||||
id: check_tag
|
||||
run: |
|
||||
if git tag --points-at HEAD | grep -q '^v'; then
|
||||
echo "is_tagged=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "is_tagged=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
build_and_push:
|
||||
name: Docker | Build and Push
|
||||
needs: release
|
||||
if: ${{ needs.release.outputs.is_tagged == 'true' }} # só roda se semantic-release criou tag
|
||||
runs-on: [self-hosted]
|
||||
env:
|
||||
DEJO_NODE_AWS_REGION: us-east-1
|
||||
AWS_ECR_REPOSITORY: dev-dejo/dejo-node
|
||||
DISABLE_DISCORD_NOTIFY: true
|
||||
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
|
||||
sha_short: ${{ needs.release.outputs.sha_short }}
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Discord | Notify Start
|
||||
if: ${{ env.DISABLE_DISCORD_NOTIFY != 'true' }}
|
||||
run: |
|
||||
TAG=${GITHUB_REF#refs/tags/}
|
||||
curl -X POST -H "Content-Type: application/json" \
|
||||
-d "{\"content\": \":arrow_forward: Deploy da versão **${TAG}** iniciado...\"}" \
|
||||
"${DISCORD_WEBHOOK}"
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Copy Env
|
||||
run: cp infrastructure/.env.example infrastructure/.env
|
||||
|
||||
- name: Cache Docker layers
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: /tmp/.buildx-cache
|
||||
key: ${{ runner.os }}-buildx-${{ github.sha }}
|
||||
restore-keys: ${{ runner.os }}-buildx
|
||||
|
||||
- name: Docker Login to AWS ECR
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
registry: ${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}
|
||||
username: ${{ secrets.DEJO_NODE_AWS_ACCESS_KEY }}
|
||||
password: ${{ secrets.DEJO_NODE_AWS_SECRET_KEY }}
|
||||
|
||||
- name: Build and Push Backend
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: infrastructure
|
||||
file: infrastructure/Dockerfile
|
||||
push: true
|
||||
tags: |
|
||||
${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}/${{ env.AWS_ECR_REPOSITORY }}:latest
|
||||
${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}/${{ env.AWS_ECR_REPOSITORY }}:${{ env.sha_short }}
|
||||
cache-from: type=local,src=/tmp/.buildx-cache
|
||||
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
|
||||
|
||||
- name: Moving Cache
|
||||
run: |
|
||||
rm -rf /tmp/.buildx-cache
|
||||
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
|
||||
|
||||
kustomize_apply:
|
||||
name: Kubernetes | Kustomize Apply
|
||||
needs: build_and_push
|
||||
runs-on: [self-hosted]
|
||||
env:
|
||||
DEJO_NODE_AWS_REGION: us-east-1
|
||||
KUBE_NAMESPACE: dejo-node
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Configure AWS Credentials
|
||||
uses: aws-actions/configure-aws-credentials@v4
|
||||
with:
|
||||
aws-access-key-id: ${{ secrets.DEJO_NODE_AWS_ACCESS_KEY }}
|
||||
aws-secret-access-key: ${{ secrets.DEJO_NODE_AWS_SECRET_KEY }}
|
||||
aws-region: ${{ env.DEJO_NODE_AWS_REGION }}
|
||||
|
||||
- name: Apply Kustomize
|
||||
run: |
|
||||
echo "${{ secrets.DEJO_NODE_KUBE_CONFIG_DATA_DEV }}" | base64 -d > kubeconfig
|
||||
export KUBECONFIG=$PWD/kubeconfig
|
||||
kubectl apply -k infrastructure/kubernetes/dev -n "${KUBE_NAMESPACE}"
|
||||
|
||||
deploy_backend:
|
||||
name: Kubernetes | Deploy App
|
||||
needs: kustomize_apply
|
||||
runs-on: [self-hosted]
|
||||
env:
|
||||
DEJO_NODE_AWS_REGION: us-east-1
|
||||
AWS_ECR_REPOSITORY: dev-dejo/dejo-node
|
||||
KUBE_NAMESPACE: dejo-node
|
||||
KUBE_DEPLOY_NAME: api-app
|
||||
DISABLE_DISCORD_NOTIFY: true
|
||||
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
|
||||
sha_short: ${{ needs.release.outputs.sha_short }}
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Configure AWS Credentials
|
||||
uses: aws-actions/configure-aws-credentials@v4
|
||||
with:
|
||||
aws-access-key-id: ${{ secrets.DEJO_NODE_AWS_ACCESS_KEY }}
|
||||
aws-secret-access-key: ${{ secrets.DEJO_NODE_AWS_SECRET_KEY }}
|
||||
aws-region: ${{ env.DEJO_NODE_AWS_REGION }}
|
||||
|
||||
- name: Deploy API
|
||||
run: |
|
||||
echo "${{ secrets.DEJO_NODE_KUBE_CONFIG_DATA_DEV }}" | base64 -d > kubeconfig
|
||||
export KUBECONFIG=$PWD/kubeconfig
|
||||
kubectl set image deployment/${{ env.KUBE_DEPLOY_NAME }} \
|
||||
${{ env.KUBE_DEPLOY_NAME }}="${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}/${{ env.AWS_ECR_REPOSITORY }}:${{ env.sha_short }}" \
|
||||
--record -n "${KUBE_NAMESPACE}"
|
||||
|
||||
- name: Verify Rollout
|
||||
run: |
|
||||
echo "${{ secrets.DEJO_NODE_KUBE_CONFIG_DATA_DEV }}" | base64 -d > kubeconfig
|
||||
export KUBECONFIG=$PWD/kubeconfig
|
||||
kubectl rollout status deployment/${{ env.KUBE_DEPLOY_NAME }} -n "${KUBE_NAMESPACE}"
|
||||
|
||||
- name: Discord | Notify Error
|
||||
if: ${{ failure() && env.DISABLE_DISCORD_NOTIFY != 'true' }}
|
||||
run: |
|
||||
curl -X POST -H "Content-Type: application/json" \
|
||||
-d '{"content": ":x: Erro durante o deploy! Veja logs."}' \
|
||||
"${{ env.DISCORD_WEBHOOK }}"
|
||||
exit 1
|
||||
|
||||
- name: Discord | Notify Success
|
||||
if: ${{ success() && env.DISABLE_DISCORD_NOTIFY != 'true' }}
|
||||
run: |
|
||||
curl -X POST -H "Content-Type: application/json" \
|
||||
-d '{"content": ":white_check_mark: Deploy concluído com sucesso! :rocket:"}' \
|
||||
"${{ env.DISCORD_WEBHOOK }}"
|
||||
|
||||
@ -1,168 +0,0 @@
|
||||
name: Development | CD
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- feature/ci-cd
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}
|
||||
|
||||
env:
|
||||
DEJO_NODE_AWS_REGION: us-east-1
|
||||
AWS_ECR_REPOSITORY: dev-dejo/dejo-node
|
||||
KUBE_NAMESPACE: dejo-node
|
||||
KUBE_DEPLOY_NAME: api-app
|
||||
DISABLE_DISCORD_NOTIFY: true
|
||||
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
|
||||
|
||||
jobs:
|
||||
build_and_push:
|
||||
name: Docker | Build and Push
|
||||
runs-on: [self-hosted]
|
||||
steps:
|
||||
- name: Checkout Branch
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Discord | Notify Início
|
||||
if: ${{ always() && env.DISABLE_DISCORD_NOTIFY != 'true' }}
|
||||
run: |
|
||||
curl -X POST -H "Content-Type: application/json" \
|
||||
-d '{"content": ":arrow_forward: Iniciando deploy no ambiente development..."}' \
|
||||
"${DISCORD_WEBHOOK}"
|
||||
|
||||
- name: Commit Short Hash
|
||||
id: vars
|
||||
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Copy Env
|
||||
run: cp infrastructure/.env.example infrastructure/.env
|
||||
|
||||
- name: Cache Docker layers
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: /tmp/.buildx-cache
|
||||
key: ${{ runner.os }}-multi-buildx-${{ github.sha }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-multi-buildx
|
||||
|
||||
- name: Docker Login to AWS ECR
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
registry: ${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}
|
||||
username: ${{ secrets.DEJO_NODE_AWS_ACCESS_KEY }}
|
||||
password: ${{ secrets.DEJO_NODE_AWS_SECRET_KEY }}
|
||||
|
||||
- name: Build and Push Backend
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
builder: ${{ steps.buildx.outputs.name }}
|
||||
file: ./Dockerfile
|
||||
push: true
|
||||
tags: |
|
||||
${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}/${{ env.AWS_ECR_REPOSITORY }}:latest
|
||||
${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}/${{ env.AWS_ECR_REPOSITORY }}:${{ env.sha_short }}
|
||||
cache-from: type=local,src=/tmp/.buildx-cache
|
||||
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
|
||||
|
||||
- name: Moving Cache
|
||||
run: |
|
||||
rm -rf /tmp/.buildx-cache
|
||||
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
|
||||
|
||||
kustomize_apply:
|
||||
name: Kubernetes | Kustomize Apply
|
||||
runs-on: [self-hosted]
|
||||
needs: build_and_push
|
||||
steps:
|
||||
- name: Checkout Branch
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Configure AWS Credentials
|
||||
uses: aws-actions/configure-aws-credentials@v4
|
||||
with:
|
||||
aws-access-key-id: ${{ secrets.DEJO_NODE_AWS_ACCESS_KEY }}
|
||||
aws-secret-access-key: ${{ secrets.DEJO_NODE_AWS_SECRET_KEY }}
|
||||
aws-region: ${{ env.DEJO_NODE_AWS_REGION }}
|
||||
|
||||
- name: Debug | Mostrar estrutura após checkout
|
||||
run: |
|
||||
echo "PWD = $(pwd)"
|
||||
ls -R .
|
||||
|
||||
- name: Kubernetes | Apply Kustomize
|
||||
env:
|
||||
KUBE_CONFIG_DATA: ${{ secrets.DEJO_NODE_KUBE_CONFIG_DATA_DEV }}
|
||||
KUBE_NAMESPACE: ${{ env.KUBE_NAMESPACE }}
|
||||
run: |
|
||||
# Decodifica e grava o kubeconfig
|
||||
echo "${KUBE_CONFIG_DATA}" | base64 -d > kubeconfig
|
||||
export KUBECONFIG=$PWD/kubeconfig
|
||||
|
||||
# Aplica todos os manifests gerados pelo Kustomize
|
||||
kubectl apply -k infrastructure/kubernetes/dev -n "${KUBE_NAMESPACE}"
|
||||
|
||||
deploy_backend:
|
||||
name: 'Kubernetes | Deploy App'
|
||||
needs: kustomize_apply
|
||||
runs-on: [self-hosted]
|
||||
steps:
|
||||
- name: Checkout Branch
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Configure AWS Credentials
|
||||
uses: aws-actions/configure-aws-credentials@v4
|
||||
with:
|
||||
aws-access-key-id: ${{ secrets.DEJO_NODE_AWS_ACCESS_KEY }}
|
||||
aws-secret-access-key: ${{ secrets.DEJO_NODE_AWS_SECRET_KEY }}
|
||||
aws-region: ${{ env.DEJO_NODE_AWS_REGION }}
|
||||
|
||||
- name: Commit Short Hash
|
||||
id: vars
|
||||
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
|
||||
|
||||
- name: Kubernetes | Deploy API
|
||||
env:
|
||||
KUBE_CONFIG_DATA: ${{ secrets.DEJO_NODE_KUBE_CONFIG_DATA_DEV }}
|
||||
KUBE_NAMESPACE: ${{ env.KUBE_NAMESPACE }}
|
||||
RELEASE_IMAGE: ${{ secrets.DEV_DEJO_AWS_ECR_REGISTRY }}/${{ env.AWS_ECR_REPOSITORY }}:${{ env.sha_short }}
|
||||
run: |
|
||||
# Decodifica e grava o kubeconfig
|
||||
echo "${KUBE_CONFIG_DATA}" | base64 -d > kubeconfig
|
||||
export KUBECONFIG=$PWD/kubeconfig
|
||||
|
||||
# Atualiza a imagem no Deployment
|
||||
kubectl set image deployment/${{ env.KUBE_DEPLOY_NAME }} \
|
||||
${{ env.KUBE_DEPLOY_NAME }}="${RELEASE_IMAGE}" --record -n "${KUBE_NAMESPACE}"
|
||||
|
||||
- name: Run | Verify Kubernetes deployment
|
||||
env:
|
||||
KUBE_CONFIG_DATA: ${{ secrets.DEJO_NODE_KUBE_CONFIG_DATA_DEV }}
|
||||
KUBE_NAMESPACE: ${{ env.KUBE_NAMESPACE }}
|
||||
run: |
|
||||
# Decodifica e grava o kubeconfig
|
||||
echo "${KUBE_CONFIG_DATA}" | base64 -d > kubeconfig
|
||||
export KUBECONFIG=$PWD/kubeconfig
|
||||
|
||||
# Aguardar rollout
|
||||
kubectl rollout status deployment/${{ env.KUBE_DEPLOY_NAME }} -n "${KUBE_NAMESPACE}"
|
||||
|
||||
- name: Discord | Notify Error
|
||||
if: ${{ failure() && env.DISABLE_DISCORD_NOTIFY != 'true' }}
|
||||
run: |
|
||||
curl -X POST -H "Content-Type: application/json" \
|
||||
-d '{"content": ":x: Erro durante o deploy! Veja detalhes nos logs do pipeline."}' \
|
||||
"${DISCORD_WEBHOOK}"
|
||||
exit 1
|
||||
|
||||
- name: Discord | Notify Success
|
||||
if: ${{ success() && env.DISABLE_DISCORD_NOTIFY != 'true' }}
|
||||
run: |
|
||||
curl -X POST -H "Content-Type: application/json" \
|
||||
-d '{"content": ":white_check_mark: Deploy concluído com sucesso! :rocket:"}' \
|
||||
"${DISCORD_WEBHOOK}"
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,3 +18,4 @@ terraform.rc
|
||||
*/**/builds
|
||||
*/**/sealedsecrets_result
|
||||
*.zip
|
||||
/node_modules/
|
||||
|
||||
35
.releaserc
Normal file
35
.releaserc
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"branches": ["master"],
|
||||
"plugins": [
|
||||
[
|
||||
"@semantic-release/commit-analyzer",
|
||||
{
|
||||
"preset": "conventionalcommits",
|
||||
"releaseRules": [
|
||||
{ "type": "feat", "release": "minor" },
|
||||
{ "type": "fix", "release": "patch" },
|
||||
{ "type": "revert", "release": "major" }
|
||||
]
|
||||
}
|
||||
],
|
||||
"@semantic-release/release-notes-generator",
|
||||
[
|
||||
"@semantic-release/changelog",
|
||||
{ "changelogFile": "CHANGELOG.md" }
|
||||
],
|
||||
[
|
||||
"@semantic-release/git",
|
||||
{
|
||||
"assets": ["CHANGELOG.md"],
|
||||
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
||||
}
|
||||
],
|
||||
[
|
||||
"@saithodev/semantic-release-gitea",
|
||||
{
|
||||
"giteaUrl": "https://git.dejodigital.com.br",
|
||||
"assets": ["CHANGELOG.md"]
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
5
commitlint.config.js
Normal file
5
commitlint.config.js
Normal file
@ -0,0 +1,5 @@
|
||||
// commitlint.config.js
|
||||
module.exports = {
|
||||
extends: ['@commitlint/config-conventional']
|
||||
};
|
||||
|
||||
40
infrastructure/hooks/commit-msg
Executable file
40
infrastructure/hooks/commit-msg
Executable file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Path to the commit message file (provided by Git).
|
||||
COMMIT_MSG_FILE=$1
|
||||
|
||||
# Read the commit message from the file.
|
||||
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
|
||||
|
||||
CONVENTIONAL_COMMIT_REGEX='^(feat|fix|docs|style|refactor|test|chore|build|ci|perf|revert)(\([a-zA-Z0-9_.-]+\))?(!)?:\s.*$'
|
||||
|
||||
# Check if the commit message matches the regex
|
||||
if ! [[ $COMMIT_MSG =~ $CONVENTIONAL_COMMIT_REGEX ]]; then
|
||||
echo "ERRO: A mensagem de commit não segue o formato do Conventional Commits."
|
||||
echo
|
||||
echo "O formato correto da mensagem de commit é obrigatório:"
|
||||
echo " <tipo>(<escopo opcional>): <descrição>"
|
||||
echo
|
||||
echo "Os tipos válidos são:"
|
||||
echo " feat: Uma nova funcionalidade."
|
||||
echo " fix: Correção de um bug."
|
||||
echo " docs: Alterações na documentação."
|
||||
echo " style: Alterações de estilo de código (formatação, ponto-e-vírgula ausente, etc.)."
|
||||
echo " refactor: Refatoração de código (nem corrige bug nem adiciona funcionalidade)."
|
||||
echo " test: Adicionar ou atualizar testes."
|
||||
echo " chore: Tarefas rotineiras como atualização de dependências ou ferramentas de build."
|
||||
echo " build: Alterações que afetam o sistema de build ou dependências externas."
|
||||
echo " ci: Alterações nos arquivos de configuração de CI ou scripts."
|
||||
echo " perf: Melhorias de desempenho."
|
||||
echo " revert: Reverter um commit anterior."
|
||||
echo
|
||||
echo "Exemplos:"
|
||||
echo " feat(auth): adicionar funcionalidade de login"
|
||||
echo " fix(api)!: resolver problema de timeout"
|
||||
echo " docs(readme): atualizar instruções de instalação"
|
||||
echo
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
||||
8919
package-lock.json
generated
Normal file
8919
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
package.json
Normal file
16
package.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"scripts": {
|
||||
"prepare": "git config core.hooksPath infrastructure/hooks"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@saithodev/semantic-release-gitea": "^1.0.0",
|
||||
"@semantic-release/changelog": "^6.0.0",
|
||||
"@semantic-release/commit-analyzer": "^9.0.0",
|
||||
"@semantic-release/git": "^10.0.0",
|
||||
"@semantic-release/release-notes-generator": "^10.0.0",
|
||||
"semantic-release": "^24.2.5",
|
||||
"@commitlint/cli": "^19.8.1",
|
||||
"@commitlint/config-conventional": "^19.8.1"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user