Skip to content

Commit e9add6f

Browse files
committed
test release docker container
1 parent 043457a commit e9add6f

3 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Build and Push Docker Image
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
# tags: [ 'v*' ]
7+
pull_request:
8+
branches: [ main ]
9+
10+
env:
11+
REGISTRY: ghcr.io
12+
IMAGE_NAME: ${{ github.repository }}
13+
14+
jobs:
15+
build-and-push:
16+
runs-on: ubuntu-latest
17+
permissions:
18+
contents: read
19+
packages: write
20+
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v4
24+
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v3
27+
with:
28+
version: latest
29+
30+
- name: Log in to GitHub Container Registry
31+
uses: docker/login-action@v3
32+
with:
33+
registry: ${{ env.REGISTRY }}
34+
username: ${{ github.actor }}
35+
password: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Extract metadata for Docker
38+
id: meta
39+
uses: docker/metadata-action@v5
40+
with:
41+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
42+
tags: |
43+
type=ref,event=branch
44+
type=ref,event=pr
45+
type=semver,pattern={{version}}
46+
type=semver,pattern={{major}}.{{minor}}
47+
type=semver,pattern={{major}}
48+
type=sha,prefix=
49+
50+
- name: Build and push Docker image
51+
uses: docker/build-push-action@v5
52+
with:
53+
context: .
54+
file: ./Dockerfile
55+
platforms: linux/amd64,linux/arm64
56+
push: ${{ github.event_name != 'pull_request' }}
57+
tags: ${{ steps.meta.outputs.tags }}
58+
labels: ${{ steps.meta.outputs.labels }}
59+
cache-from: type=gha
60+
cache-to: type=gha,mode=max
61+
62+
- name: Run Trivy vulnerability scanner
63+
uses: aquasecurity/trivy-action@master
64+
with:
65+
image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
66+
format: 'sarif'
67+
output: 'trivy-results.sarif'
68+
69+
- name: Upload Trivy scan results
70+
uses: github/codeql-action/upload-sarif@v2
71+
with:
72+
sarif_file: 'trivy-results.sarif'
73+
if: always()
74+
75+
# Separate job for testing the Docker image
76+
test-docker:
77+
runs-on: ubuntu-latest
78+
needs: build-and-push
79+
if: github.event_name != 'pull_request'
80+
81+
steps:
82+
- name: Test Docker image
83+
run: |
84+
docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest --help

DOCKER_README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Docker Setup and GitHub Packages Release
2+
3+
## Docker Build
4+
5+
Build the Docker image:
6+
7+
```bash
8+
docker build -t xcstrings-translator .
9+
```
10+
11+
## Running the Container
12+
13+
Run the container with your xcstrings files:
14+
15+
```bash
16+
# Mount a directory containing your xcstrings files
17+
docker run -v /path/to/your/files:/workspace -w /workspace xcstrings-translator [command] [options]
18+
19+
# Example:
20+
docker run -v $(pwd):/workspace -w /workspace xcstrings-translator google --api-key "your-key" --input "Localizable.xcstrings" --target-languages "zh-Hans"
21+
```
22+
23+
## GitHub Packages Release
24+
25+
The Docker image is automatically built and pushed to GitHub Container Registry when you push to the main branch or create a tag.
26+
27+
### Manual Release Process
28+
29+
1. **Build and tag the image:**
30+
```bash
31+
# Get the current version from cmd/root.go or create a tag
32+
VERSION=$(grep 'Version = ' cmd/root.go | cut -d'"' -f2)
33+
docker build -t ghcr.io/fdddf/xcstrings-translator:${VERSION} .
34+
docker tag ghcr.io/fdddf/xcstrings-translator:${VERSION} ghcr.io/fdddf/xcstrings-translator:latest
35+
```
36+
37+
2. **Login to GitHub Container Registry:**
38+
```bash
39+
echo $GITHUB_TOKEN | docker login ghcr.io -u USERNAME --password-stdin
40+
```
41+
42+
3. **Push the image:**
43+
```bash
44+
docker push ghcr.io/fdddf/xcstrings-translator:${VERSION}
45+
docker push ghcr.io/fdddf/xcstrings-translator:latest
46+
```
47+
48+
### Automated Release with GitHub Actions
49+
50+
The workflow in `.github/workflows/docker-release.yml` automatically:
51+
- Builds the Docker image on pushes to main branch
52+
- Tags images with branch names, PR numbers, semantic versions, and commit SHAs
53+
- Pushes to GitHub Container Registry
54+
- Runs security scanning with Trivy
55+
56+
#### Workflow Triggers:
57+
- Push to `main` or `master` branches
58+
- Push of version tags (`v*`)
59+
- Pull requests to `main`
60+
61+
### Using the Released Image
62+
63+
Pull and run the latest image:
64+
65+
```bash
66+
docker pull ghcr.io/fdddf/xcstrings-translator:latest
67+
docker run ghcr.io/fdddf/xcstrings-translator:latest --help
68+
```
69+
70+
## Multi-platform Support
71+
72+
The GitHub Actions workflow builds for both `linux/amd64` and `linux/arm64` architectures.
73+
74+
## Security Features
75+
76+
- Runs as non-root user (UID 65532)
77+
- Minimal Alpine base image
78+
- Health checks built-in
79+
- Built with security scanning via Trivy

Dockerfile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Multi-stage build for xcstrings-translator
2+
FROM golang:1.25-alpine AS builder
3+
4+
# Install build dependencies
5+
RUN apk add --no-cache \
6+
git \
7+
gcc \
8+
musl-dev
9+
10+
# Set working directory
11+
WORKDIR /app
12+
13+
# Copy go mod files
14+
COPY go.mod go.sum ./
15+
RUN go mod download
16+
17+
# Copy source code
18+
COPY . .
19+
20+
# Build the application
21+
RUN go build -o bin/xcstrings-translator .
22+
23+
# Final stage - minimal Alpine image
24+
FROM alpine:latest
25+
26+
# Install runtime dependencies
27+
RUN apk --no-cache add \
28+
ca-certificates \
29+
tzdata
30+
31+
# Create non-root user
32+
RUN addgroup -g 65532 nonroot && \
33+
adduser -D -u 65532 -G nonroot nonroot
34+
35+
# Copy the binary from builder stage
36+
COPY --from=builder /app/bin/xcstrings-translator /usr/local/bin/xcstrings-translator
37+
38+
# Create app directory and set permissions
39+
RUN mkdir -p /app && \
40+
chown -R nonroot:nonroot /app
41+
42+
WORKDIR /app
43+
44+
# Make binary executable
45+
RUN chmod +x /usr/local/bin/xcstrings-translator
46+
47+
# Switch to non-root user
48+
USER nonroot:nonroot
49+
50+
# Health check
51+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
52+
CMD xcstrings-translator --help || exit 1
53+
54+
# Default command
55+
ENTRYPOINT ["xcstrings-translator"]
56+
CMD ["--help"]

0 commit comments

Comments
 (0)