-
Notifications
You must be signed in to change notification settings - Fork 0
177 lines (152 loc) · 5.27 KB
/
main.yml
File metadata and controls
177 lines (152 loc) · 5.27 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
name: CI/CD Pipeline
on:
push:
branches:
- main
- '*' # Executa em todas as branches
pull_request:
branches:
- main
workflow_dispatch: # Permite o acionamento manual
env:
DOCKER_USER: rejanegallotti
DOCKER_IMAGE_NAME: ${{ vars.DOCKER_USER || 'rejanegallotti' }}/ms-saudacoes-aleatorias
DOCKER_IMAGE_TAG: 1.0.${{ github.run_id }}
SERVICE_NAME: saudacoes-aleatorias
jobs:
# Job para rodar linters em paralelo
lint:
name: Lint Code
runs-on: ubuntu-latest
# Executa este job apenas em branches que não são a 'main'
if: github.ref_name != 'main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23.4'
- name: Go Fmt & Vet
run: |
echo "Linting code with go fmt and go vet..."
go fmt $(go list ./...)
go vet $(go list ./...)
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v7
with:
version: v2.2.2 # Use uma versão específica para builds consistentes
args: --verbose --timeout=5m --build-tags=tools
install-mode: binary
only-new-issues: false
skip-cache: false
env: # <--- ADICIONE ESTA SEÇÃO
GOFLAGS: "-buildvcs=false" # <--- ESTA LINHA ESPECIFICAMENTE
# Job para rodar os testes
test:
name: Run Go Tests
runs-on: ubuntu-latest
needs: lint # Executa após o job de lint
if: github.ref_name != 'main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 'v1.22'
- name: Install gotestsum
run: go install gotest.tools/gotestsum@latest
- name: Run tests and generate report
run: |
echo "Running tests and generating report..."
# CGO_ENABLED=1 é necessário se houver código C, mas requer gcc.
# A imagem ubuntu-latest já tem o gcc.
CGO_ENABLED=1 gotestsum --junitfile report.xml --format testname
env:
GOFLAGS: "-buildvcs=false" # ← ADICIONADO também para os testes
- name: Upload test results
uses: actions/upload-artifact@v4
if: always() # Sempre executa este passo, mesmo que o anterior falhe
with:
name: test-reports
path: report.xml
# Job para build e push da imagem Docker
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
# Executa apenas quando há um push na branch 'main'
if: github.event_name == 'push' && github.ref_name == 'main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
# Essencial para builds multi-plataforma (ex: ARM em runners x86)
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Cria um builder do Docker que suporta builds multi-plataforma
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ env.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASS }} # Segredo configurado no GitHub
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG }}
${{ env.DOCKER_IMAGE_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
# Job para fazer o deploy da aplicação
deploy:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: build-and-push # Depende do sucesso do build
if: github.event_name == 'push' && github.ref_name == 'main'
environment: staging # Define um ambiente (opcional, bom para proteção)
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Terraform
uses: hashicorp/setup-terraform@v3
- name: Terraform Init
id: init
run: terraform -chdir=infra init
env:
KOYEB_TOKEN: ${{ secrets.KOYEB_TOKEN }}
- name: Terraform Validate
id: validate
run: terraform -chdir=infra validate
- name: Terraform Apply
id: apply
run: terraform -chdir=infra apply -auto-approve
env:
KOYEB_TOKEN: ${{ secrets.KOYEB_TOKEN }}
TF_VAR_docker_image_name: ${{ env.DOCKER_IMAGE_NAME }}
TF_VAR_docker_image_tag: ${{ env.DOCKER_IMAGE_TAG }}
# Job para destruir a infraestrutura manualmente
destroy:
name: Destroy Staging
runs-on: ubuntu-latest
needs: deploy
# Executa apenas quando acionado manualmente (workflow_dispatch)
if: github.event_name == 'workflow_dispatch'
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Terraform
uses: hashicorp/setup-terraform@v3
- name: Terraform Init
run: terraform -chdir=infra init
env:
KOYEB_TOKEN: ${{ secrets.KOYEB_TOKEN }}
- name: Terraform Destroy
run: terraform -chdir=infra destroy -auto-approve
env:
KOYEB_TOKEN: ${{ secrets.KOYEB_TOKEN }}