Skip to content

Commit

Permalink
build(container): build and push docker image to ghcr
Browse files Browse the repository at this point in the history
  • Loading branch information
cecobask committed Dec 24, 2024
1 parent b9c76c5 commit abd4f11
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 17 deletions.
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.github
assets
build
.codecov.yaml
.env
.gitignore
.golangci.yaml
.goreleaser.yaml
coverage.out
Dockerfile
Makefile
46 changes: 46 additions & 0 deletions .github/workflows/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: package
on:
push:
tags:
- "*"
permissions:
contents: read
packages: write
attestations: write
id-token: write
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
package:
runs-on: ubuntu-24.04
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
uses: docker/metadata-action@v5
id: meta
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
id: push
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
permissions:
contents: write
jobs:
goreleaser:
release:
runs-on: ubuntu-24.04
steps:
- name: Check out code
Expand Down
19 changes: 14 additions & 5 deletions .github/workflows/sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sync
on:
push:
branches:
- main
- package
schedule:
- cron: "0 */12 * * *"
workflow_dispatch:
Expand All @@ -27,13 +27,22 @@ env:
jobs:
sync:
runs-on: ubuntu-24.04
env:
BROWSER_PATH: ${{ github.workspace }}/chrome-linux64/chrome
steps:
- name: Install Google Chrome
run: |
wget --progress=dot:giga https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1321438/chrome-linux.zip
unzip chrome-linux.zip -d "$HOME/.browser"
- name: Check out code
uses: actions/checkout@v4
- name: Install Google Chrome
run: |
wget --progress=dot:giga https://storage.googleapis.com/chrome-for-testing-public/131.0.6778.204/linux64/chrome-linux64.zip
unzip chrome-linux64.zip
echo ${{ github.workspace }}
if test -f "$BROWSER_PATH"; then
echo "Google Chrome binary stored at $BROWSER_PATH"
else
echo "Google Chrome binary not found at $BROWSER_PATH"
exit 1
fi
- name: Setup Go
uses: actions/setup-go@v5
with:
Expand Down
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM golang:1.23.4-alpine AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
ENV CGO_ENABLED=0
RUN go build -o build/its main.go

FROM ubuntu:24.04
WORKDIR /app
COPY --from=build /app/build ./build
COPY --from=build /app/config.yaml .
ENV DEBIAN_FRONTEND=noninteractive
ENV DEBCONF_NOWARNINGS=yes
RUN apt-get update > /dev/null && \
apt-get install -y --no-install-recommends \
ca-certificates \
libasound2t64 \
libgbm1 \
libgtk-3-0 \
libnss3 \
libxss1 \
libxtst6 \
unzip \
wget > /dev/null && \
rm -rf /var/lib/apt/lists/* && \
wget -q https://storage.googleapis.com/chrome-for-testing-public/131.0.6778.204/linux64/chrome-linux64.zip && \
unzip -qq chrome-linux64.zip && \
rm chrome-linux64.zip
ENV BROWSER_PATH=/app/chrome-linux64/chrome
ENV PATH=$PATH:/app/build
ENTRYPOINT ["its"]
CMD ["sync"]
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
build:
@go build -o build/its main.go

package:
@docker buildx build -t its:dev --platform=linux/amd64 .

configure:
@./build/its configure

sync:
@./build/its sync

sync-container:
@docker run -it --rm --platform=linux/amd64 --env-file=.env its:dev

html-coverage:
@go tool cover -html=coverage.out

Expand Down
23 changes: 12 additions & 11 deletions pkg/client/imdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

const (
envVarKeyBrowserPath = "BROWSER_PATH"
imdbPathBase = "https://www.imdb.com"
imdbPathExports = "/exports"
imdbPathList = "/list/%s"
Expand All @@ -47,17 +48,7 @@ type imdbConfig struct {
}

func NewIMDbClient(ctx context.Context, conf *appconfig.IMDb, logger *slog.Logger) (IMDbClientInterface, error) {
var browserPath string
if os.Getenv("GITHUB_ACTIONS") == "true" {
browserPath = fmt.Sprintf("%s/.browser/chrome-linux/chrome", os.Getenv("HOME"))
} else {
var found bool
browserPath, found = launcher.LookPath()
if !found {
return nil, fmt.Errorf("failure looking up browser path")
}
}
l := launcher.New().Headless(*conf.Headless).Bin(browserPath).
l := launcher.New().Headless(*conf.Headless).Bin(getBrowserPathOrFallback()).
Set("allow-running-insecure-content").
Set("autoplay-policy", "user-gesture-required").
Set("disable-component-update").
Expand Down Expand Up @@ -722,3 +713,13 @@ func setBrowserCookies(browser *rod.Browser, config *appconfig.IMDb) error {
}
return nil
}

func getBrowserPathOrFallback() string {
if browserPath, found := os.LookupEnv(envVarKeyBrowserPath); found {
return browserPath
}
if browserPath, found := launcher.LookPath(); found {
return browserPath
}
return ""
}

0 comments on commit abd4f11

Please sign in to comment.