Skip to content

Commit c5f3e04

Browse files
committed
Add CI/CD pipelines for Go application - Add GitHub Actions workflow for testing - Add workflow for building and publishing Docker image - Create Docker file for Go application - Add CI/CD documentation
1 parent ad78b82 commit c5f3e04

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed

.github/workflows/go-build.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Go Build and Docker Publish
2+
3+
on:
4+
push:
5+
branches: [main, develop]
6+
tags: ["v*"]
7+
pull_request:
8+
branches: [main, develop]
9+
10+
jobs:
11+
build:
12+
name: Build Go App
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
18+
- name: Set up Go
19+
uses: actions/setup-go@v4
20+
with:
21+
go-version: "1.21"
22+
cache: true
23+
24+
- name: Install dependencies
25+
run: go mod download
26+
27+
- name: Build
28+
run: |
29+
mkdir -p build
30+
go build -v -o build/export_trakt ./cmd/export_trakt
31+
32+
- name: Upload build artifact
33+
uses: actions/upload-artifact@v3
34+
with:
35+
name: export-trakt-binary
36+
path: build/export_trakt
37+
38+
docker:
39+
name: Build and Push Docker Image
40+
runs-on: ubuntu-latest
41+
needs: build
42+
if: github.event_name != 'pull_request'
43+
steps:
44+
- name: Checkout code
45+
uses: actions/checkout@v3
46+
47+
- name: Download build artifact
48+
uses: actions/download-artifact@v3
49+
with:
50+
name: export-trakt-binary
51+
path: build
52+
53+
- name: Make binary executable
54+
run: chmod +x build/export_trakt
55+
56+
- name: Set up Docker Buildx
57+
uses: docker/setup-buildx-action@v2
58+
59+
- name: Login to GitHub Container Registry
60+
uses: docker/login-action@v2
61+
with:
62+
registry: ghcr.io
63+
username: ${{ github.repository_owner }}
64+
password: ${{ secrets.GITHUB_TOKEN }}
65+
66+
- name: Docker meta
67+
id: meta
68+
uses: docker/metadata-action@v4
69+
with:
70+
images: ghcr.io/${{ github.repository_owner }}/export_trakt_4_letterboxd
71+
tags: |
72+
type=ref,event=branch
73+
type=semver,pattern={{version}}
74+
type=semver,pattern={{major}}.{{minor}}
75+
type=sha
76+
latest
77+
78+
- name: Build and push Docker image
79+
uses: docker/build-push-action@v4
80+
with:
81+
context: .
82+
file: ./Dockerfile.go
83+
push: true
84+
tags: ${{ steps.meta.outputs.tags }}
85+
labels: ${{ steps.meta.outputs.labels }}
86+
platforms: linux/amd64,linux/arm64

.github/workflows/go-tests.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Go Tests
2+
3+
on:
4+
push:
5+
branches: [main, feature/*, develop]
6+
pull_request:
7+
branches: [main, develop]
8+
9+
jobs:
10+
test:
11+
name: Run Go Tests
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: "1.21"
21+
cache: true
22+
23+
- name: Install dependencies
24+
run: go mod download
25+
26+
- name: Run tests
27+
run: go test -v ./... -coverprofile=coverage.out
28+
29+
- name: Generate coverage report
30+
run: go tool cover -html=coverage.out -o coverage.html
31+
32+
- name: Upload coverage report
33+
uses: actions/upload-artifact@v3
34+
with:
35+
name: coverage-report
36+
path: coverage.html
37+
38+
- name: Check test coverage
39+
run: |
40+
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | tr -d '%')
41+
echo "Total coverage: $COVERAGE%"
42+
if (( $(echo "$COVERAGE < 70" | bc -l) )); then
43+
echo "Code coverage is below 70%. Please add more tests."
44+
exit 1
45+
fi

Dockerfile.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
FROM alpine:3.18
2+
3+
LABEL maintainer="Johan Devlaminck <info@johandevlaminck.com>"
4+
LABEL org.opencontainers.image.source=https://github.com/JohanDevl/Export_Trakt_4_Letterboxd
5+
LABEL org.opencontainers.image.description="Export your Trakt.tv history to Letterboxd format"
6+
LABEL org.opencontainers.image.licenses=MIT
7+
8+
# Create app directories
9+
RUN mkdir -p /app/config /app/logs /app/locales /app/exports
10+
11+
# Set working directory
12+
WORKDIR /app
13+
14+
# Copy pre-built binary
15+
COPY build/export_trakt /app/
16+
17+
# Copy translation files
18+
COPY locales/ /app/locales/
19+
20+
# Default config file
21+
COPY config/config.toml /app/config/
22+
23+
# Ensure the binary is executable
24+
RUN chmod +x /app/export_trakt
25+
26+
# Set environment variables
27+
ENV CONFIG_PATH=/app/config/config.toml
28+
ENV GO_ENV=production
29+
30+
# Volumes
31+
VOLUME ["/app/config", "/app/logs", "/app/exports"]
32+
33+
# Run the application
34+
ENTRYPOINT ["/app/export_trakt", "--config", "/app/config/config.toml"]

docs/CI_CD.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# CI/CD Pipeline Documentation
2+
3+
This project uses GitHub Actions for continuous integration and continuous deployment. The CI/CD pipeline automates testing, building, and deployment of the application.
4+
5+
## Pipeline Components
6+
7+
### 1. Go Tests Workflow
8+
9+
The tests workflow runs all unit and integration tests for the Go application.
10+
11+
**File**: `.github/workflows/go-tests.yml`
12+
13+
**Triggered by**:
14+
15+
- Push to main, develop, and feature/\* branches
16+
- Pull requests to main and develop branches
17+
18+
**Steps**:
19+
20+
1. Check out the code
21+
2. Set up Go environment
22+
3. Install dependencies
23+
4. Run all tests with coverage tracking
24+
5. Generate a coverage report
25+
6. Upload the coverage report as an artifact
26+
7. Verify the code coverage meets minimum threshold (70%)
27+
28+
**Usage**:
29+
30+
```bash
31+
# To run tests locally with coverage reporting
32+
go test -v ./... -coverprofile=coverage.out
33+
go tool cover -html=coverage.out -o coverage.html
34+
```
35+
36+
### 2. Go Build and Docker Publish Workflow
37+
38+
This workflow builds the Go application and creates a Docker image.
39+
40+
**File**: `.github/workflows/go-build.yml`
41+
42+
**Triggered by**:
43+
44+
- Push to main and develop branches
45+
- Push of version tags (v\*)
46+
- Pull requests to main and develop branches
47+
48+
**Jobs**:
49+
50+
1. **Build**: Compiles the Go application
51+
52+
- Produces executable binary
53+
- Uploads the binary as an artifact
54+
55+
2. **Docker** (runs only on push, not PR):
56+
- Builds a Docker image using Dockerfile.go
57+
- Tags the image according to branch/tag
58+
- Pushes to GitHub Container Registry
59+
60+
**Docker Image Tags**:
61+
62+
- `latest` - Always points to the most recent build from main
63+
- `vX.Y.Z` - For version releases
64+
- `vX.Y` - Major.Minor version
65+
- `develop` - For builds from the develop branch
66+
- `sha-XXXXXXX` - Git commit SHA
67+
68+
## Docker Images
69+
70+
### Go Application (Dockerfile.go)
71+
72+
The Go application uses a minimal Alpine-based image for runtime.
73+
74+
- Base Image: `alpine:3.18`
75+
- Image URL: `ghcr.io/johandevl/export_trakt_4_letterboxd`
76+
77+
**Volumes**:
78+
79+
- `/app/config` - Configuration files
80+
- `/app/logs` - Log files
81+
- `/app/exports` - Export output files
82+
83+
**Customization**:
84+
The application can be configured by mounting a custom config file:
85+
86+
```bash
87+
docker run -v /path/to/config.toml:/app/config/config.toml ghcr.io/johandevl/export_trakt_4_letterboxd
88+
```
89+
90+
## Quality Standards
91+
92+
The CI pipeline enforces the following quality standards:
93+
94+
1. **Test Coverage**: Minimum 70% code coverage required
95+
2. **Passing Tests**: All tests must pass
96+
3. **Build Verification**: Application must build successfully
97+
98+
## Troubleshooting
99+
100+
### Common Issues
101+
102+
1. **Failed Tests**:
103+
104+
- Check the test logs to identify which tests failed
105+
- Run tests locally to debug the issues
106+
107+
2. **Coverage Below Threshold**:
108+
109+
- Add more tests to cover untested code
110+
- Run coverage report locally to identify uncovered areas
111+
112+
3. **Docker Build Failures**:
113+
- Verify the Dockerfile.go is valid
114+
- Check if all required files are available in the repository
115+
116+
### Viewing Artifacts
117+
118+
1. Go to the GitHub Actions tab in the repository
119+
2. Select the workflow run
120+
3. Scroll down to the "Artifacts" section
121+
4. Download the artifact (coverage report or binary)

0 commit comments

Comments
 (0)