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 5bd720c
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 16 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
16 changes: 16 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ITS_IMDB_COOKIEATMAIN=zAta|RHiA67JIrBDPaswIym3GyrTlEuQH-u9yrKP3BUNCHgVyE4oNtUzBYVKlhjjzBiM_Z-GSVnH9rKW3Hf7LdbejovoF6SI4ZmgJcTIUXoA4NVcH1Qahwm0KYCyz95o1gsgby-uQwdU6CoS6MFTnjMkLe1puNiv4uFkvo8mOQulJJeutzYedxiUd0ns9w1X_WeVXPTZWjwisPZMw3EOR6-q9xR4kCEWRW7CmWxU1AEDQbT8ns_AJJD34w1nIQUkuLgBQrvJI_pY
ITS_IMDB_COOKIEUBIDMAIN=301-0710501-5367639
ITS_IMDB_EMAIL=user@domain.com
ITS_IMDB_HEADLESS=true
ITS_IMDB_LISTS=ls000000000,ls111111111
ITS_IMDB_PASSWORD=password123
ITS_IMDB_TRACE=false
ITS_SYNC_HISTORY=false
ITS_SYNC_MODE=dry-run
ITS_SYNC_RATINGS=false
ITS_SYNC_TIMEOUT=10m
ITS_SYNC_WATCHLIST=false
ITS_TRAKT_CLIENTID=828832482dea6fffa4453f849fe873de8be54791b9acc01f6923098d0a62972d
ITS_TRAKT_CLIENTSECRET=bdf9bab88c17f3710a6394607e96cd3a21dee6e5ea0e0236e9ed06e425ed8b6f
ITS_TRAKT_EMAIL=user@domain.com
ITS_TRAKT_PASSWORD=password123
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
15 changes: 11 additions & 4 deletions .github/workflows/sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- cron: "0 */12 * * *"
workflow_dispatch:
env:
BROWSER_PATH: ${{ github.workspace }}/chrome-linux64/chrome
ITS_IMDB_AUTH: ${{ secrets.IMDB_AUTH }}
ITS_IMDB_EMAIL: ${{ secrets.IMDB_EMAIL }}
ITS_IMDB_PASSWORD: ${{ secrets.IMDB_PASSWORD }}
Expand All @@ -28,12 +29,18 @@ jobs:
sync:
runs-on: ubuntu-24.04
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 -qq chrome-linux64.zip
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ Follow the relevant section below, based on how you want to use the application.
6. Run the **sync** workflow manually: `Actions` > `Workflows` > `sync` > `Run workflow`
7. From now on, GitHub Actions will automatically trigger the **sync** workflow

## Run the application in a Docker container
1. Install [Docker](https://www.docker.com/get-started)
2. Clone the repository: `git clone git@github.com:cecobask/imdb-trakt-sync.git`
3. Create a [Trakt App](https://trakt.tv/oauth/applications). Use **urn:ietf:wg:oauth:2.0:oob** as redirect uri
4. Configure the application:
- Create `.env` file with the same contents as [.env.example](config.yaml)
- Populate the `.env` file with your secret values
- All secret keys should have `ITS_` prefix
5. Open a terminal window in the repository folder and then:
- Build a Docker image: `make package`
- Run the sync workflow in a Docker container: `make sync-container`

## Run the application locally
1. Install [Git](https://git-scm.com/downloads) and [Go](https://go.dev/doc/install)
2. Clone the repository: `git clone git@github.com:cecobask/imdb-trakt-sync.git`
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 5bd720c

Please sign in to comment.