Add credential validation step to catch missing secrets early with clear error message. Use direct secret injection (not env vars) for better security. Isolate docker config to /tmp/docker-config.
85 lines
2.4 KiB
YAML
85 lines
2.4 KiB
YAML
name: CI
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
pull_request:
|
|
branches: [main]
|
|
|
|
env:
|
|
REGISTRY: forgejo.riotpiao.com
|
|
IMAGE: forgejo.riotpiao.com/rock/api-gateway
|
|
|
|
jobs:
|
|
test:
|
|
name: Test
|
|
runs-on: golang
|
|
steps:
|
|
- name: Install Node.js for actions runtime
|
|
run: apt-get update && apt-get install -y nodejs
|
|
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Go vet
|
|
run: go vet ./...
|
|
|
|
- name: Go test -race
|
|
run: go test ./... -race
|
|
|
|
- name: Static build (smoke)
|
|
run: CGO_ENABLED=0 go build -trimpath -o gateway ./cmd/gateway
|
|
|
|
build-push:
|
|
name: Build & Push Image
|
|
needs: test
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
runs-on: golang
|
|
steps:
|
|
- name: Install Node.js and Docker
|
|
run: |
|
|
apt-get update
|
|
apt-get install -y --no-install-recommends nodejs docker.io git
|
|
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Get short SHA
|
|
id: sha
|
|
run: |
|
|
SHORT_SHA=$(git rev-parse --short HEAD)
|
|
echo "short_sha=${SHORT_SHA}" >> $GITHUB_OUTPUT
|
|
|
|
- name: Validate registry credentials
|
|
run: |
|
|
if [ -z "${{ secrets.FORGEJO_REGISTRY_USER }}" ] || [ -z "${{ secrets.FORGEJO_REGISTRY_TOKEN }}" ]; then
|
|
echo "❌ ERROR: Registry secrets not configured"
|
|
echo "Set FORGEJO_REGISTRY_USER and FORGEJO_REGISTRY_TOKEN in repo settings"
|
|
exit 1
|
|
fi
|
|
echo "✓ Registry credentials configured"
|
|
|
|
- name: Registry login
|
|
run: |
|
|
echo "${{ secrets.FORGEJO_REGISTRY_TOKEN }}" | docker login "${{ env.REGISTRY }}" \
|
|
--username "${{ secrets.FORGEJO_REGISTRY_USER }}" --password-stdin
|
|
env:
|
|
DOCKER_CONFIG: /tmp/docker-config
|
|
|
|
- name: Build Docker image
|
|
run: |
|
|
docker build --no-cache \
|
|
-t "${IMAGE}:${{ steps.sha.outputs.short_sha }}" \
|
|
-t "${IMAGE}:latest" \
|
|
-f Dockerfile \
|
|
.
|
|
|
|
- name: Push Docker image
|
|
run: |
|
|
docker push "${IMAGE}:${{ steps.sha.outputs.short_sha }}"
|
|
docker push "${IMAGE}:latest"
|
|
echo "✓ Image pushed: ${IMAGE}:${{ steps.sha.outputs.short_sha }}"
|
|
|
|
- name: Prune unused images
|
|
run: docker image prune -a --force 2>&1 | tail -3 || true
|