Skip to content

Commit

Permalink
feat: services CD and Docker steps in CI (#10)
Browse files Browse the repository at this point in the history
Several changes to enable Docker builds for all services:
- Added a web-client build stage to the Dockerfile
- Added dummy `package.json` files to services without web-clients (I
don't like this workaround but it's fine for now)
- Updated ignore files
- Added Docker steps to the services CI workflow
- Added a CD workflow for services
  • Loading branch information
jgkawell authored Jun 25, 2024
1 parent 0bf5168 commit f6f6ee0
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**/node_modules
**/dist
.vscode
**/.vscode
.git
.gitignore
Dockerfile
.dockerignore
119 changes: 119 additions & 0 deletions .github/workflows/services-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: services - CD

on:
push:
branches:
- main
paths:
- services/**

env:
REGISTRY: ${{ vars.REGISTRY || 'ghcr.io' }}
TARGET_PLATFORMS: linux/amd64,linux/arm64

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
changed:
name: Prepare
uses: ./.github/workflows/changes.yml
with:
path: services
dir_names_max_depth: 2

build-and-test:
name: Build-Test (${{ matrix.service-path }})
# needs: [changed]
runs-on: ubuntu-latest
# if: needs.changed.outputs.any_changed == 'true'
strategy:
matrix:
go-version: ['1.21.x']
# service-path: ${{ fromJSON(needs.changed.outputs.matrix) }}
service-path: [ 'examples/crud', 'examples/echo']
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 18
cache-dependency-path: services/${{ matrix.service-path }}/web-client/package-lock.json

- name: Install Node dependencies
run: |
cd services/${{ matrix.service-path }}/web-client &&
npm i -D @swc/cli @swc/core &&
npm install
- name: Build web-client
run: |
cd services/${{ matrix.service-path }}/web-client &&
npm run build
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache-dependency-path: services/${{ matrix.service-path }}/go.sum

- name: Test
working-directory: services/${{ matrix.service-path }}
run: go test ./...

- name: Build
working-directory: services/${{ matrix.service-path }}
run: go build main.go

docker-image:
name: Docker Image (${{ matrix.service-path }})
needs: [changed]
runs-on: ubuntu-latest
if: needs.changed.outputs.any_changed == 'true'
strategy:
matrix:
service-path: ${{ fromJSON(needs.changed.outputs.matrix) }}
steps:
- uses: actions/checkout@v4

- name: Extract service info
id: info
run: |
echo "domain=$(dirname ${{ matrix.service-path }})" >> $GITHUB_OUTPUT
echo "service=$(basename ${{ matrix.service-path }})" >> $GITHUB_OUTPUT
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME || (env.REGISTRY == 'ghcr.io' && github.actor) }}
password: ${{ secrets.DOCKER_PASSWORD || (env.REGISTRY == 'ghcr.io' && secrets.GITHUB_TOKEN) }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}-${{ steps.info.outputs.domain}}-${{ steps.info.outputs.service }}
tags: |
type=sha
type=edge,branch=main
type=schedule,pattern={{date 'YYYYMMDD-hhmmss' tz='America/Chicago'}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ env.TARGET_PLATFORMS }}
build-args: |
DOMAIN=${{ steps.info.outputs.domain}}
SERVICE=${{ steps.info.outputs.service}}
55 changes: 55 additions & 0 deletions .github/workflows/services-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
paths:
- services/**

env:
REGISTRY: ${{ vars.REGISTRY || 'ghcr.io' }}
TARGET_PLATFORMS: linux/amd64,linux/arm64

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
Expand Down Expand Up @@ -63,3 +67,54 @@ jobs:
- name: Build
working-directory: services/${{ matrix.service-path }}
run: go build main.go

docker-image:
name: Docker Image (${{ matrix.service-path }})
needs: [changed]
runs-on: ubuntu-latest
if: needs.changed.outputs.any_changed == 'true'
strategy:
matrix:
service-path: ${{ fromJSON(needs.changed.outputs.matrix) }}
steps:
- uses: actions/checkout@v4

- name: Extract service info
id: info
run: |
echo "domain=$(dirname ${{ matrix.service-path }})" >> $GITHUB_OUTPUT
echo "service=$(basename ${{ matrix.service-path }})" >> $GITHUB_OUTPUT
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME || (env.REGISTRY == 'ghcr.io' && github.actor) }}
password: ${{ secrets.DOCKER_PASSWORD || (env.REGISTRY == 'ghcr.io' && secrets.GITHUB_TOKEN) }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}-${{ steps.info.outputs.domain}}-${{ steps.info.outputs.service }}
tags: |
type=sha
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ env.TARGET_PLATFORMS }}
build-args: |
DOMAIN=${{ steps.info.outputs.domain}}
SERVICE=${{ steps.info.outputs.service}}
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
services/blueprint/web-client/target
pipelines/secrets

node_1
node_2
node_3

.DS_Store
node_modules/

__debug_*

target/
node_modules
dist
target
main
40 changes: 28 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
ARG GO_VERSION=1.21.3
ARG ALPINE_VERSION=3.18

# This layer builds the binary
FROM golang:${GO_VERSION}-alpine AS builder
# Build web client (if needed)
FROM node:18 AS web-client-builder
WORKDIR /web
RUN npm i -D @swc/cli @swc/core
RUN rm package*.json

# Install build dependencies
RUN apk add --no-cache git upx
ARG DOMAIN
ARG SERVICE

# Install node modules
COPY ./services/${DOMAIN}/${SERVICE}/web-client/package*.json .
RUN npm install

# Project setup
# Build dist
COPY ./services/${DOMAIN}/${SERVICE}/web-client .
RUN npm run build

# Make sure dist directory exists even if there's no client to build
RUN mkdir -p ./dist

# Build final binary
FROM golang:${GO_VERSION}-alpine AS go-builder
WORKDIR /app
RUN apk add --no-cache git upx

# Build arguments
ARG DOMAIN
ARG SERVICE

Expand All @@ -20,16 +35,17 @@ COPY ./services/${DOMAIN}/${SERVICE}/go.sum ./go.sum

# Fetch go modules
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
go mod download
RUN --mount=type=cache,target=/go/pkg/mod \
go mod verify
go mod verify

# Copy over code
COPY ./services/${DOMAIN}/${SERVICE} .
COPY --from=web-client-builder /web/dist ./web-client/dist

# Run tests
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
go test -race -test.v ./...
go test -test.v ./...

# Build the binary
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
Expand All @@ -38,14 +54,14 @@ RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache
# Compress the binary
RUN upx main

# This layer runs the binary
# Final image
FROM alpine:${ALPINE_VERSION} AS runner

# Install dependencies
RUN apk add -U --no-cache ca-certificates

# Copy the binary from builder
COPY --from=builder /app/main /etc/main
# Copy the binary from go-builder
COPY --from=go-builder /app/main /etc/main

# Set context to run main
WORKDIR /etc
Expand Down
6 changes: 6 additions & 0 deletions services/examples/crud/web-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "placeholder",
"scripts": {
"build": ""
}
}
6 changes: 6 additions & 0 deletions services/examples/echo/web-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "placeholder",
"scripts": {
"build": ""
}
}

0 comments on commit f6f6ee0

Please sign in to comment.