diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a10bb0f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +* +!handler/ +!storage/ +!go.mod +!go.sum +!main.go diff --git a/.env.example b/.env.example index 559a4c7..12f0f27 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,6 @@ BOT_TOKEN=123456789:abcdefghijklmnopqrstuvxwyz -MYSQL_HOST=127.0.0.1 +MYSQL_HOST=127.0.0.1 # Use 'db' for Docker MYSQL_PORT=3306 MYSQL_USER=myuser MYSQL_PASSWORD=mypassword -MYSQL_DB=mydb \ No newline at end of file +MYSQL_DB=mydb diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fe2c6df..bc61ebd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,8 +13,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - GOOS: [ windows, linux, darwin ] - GOARCH: [ amd64, 386, arm, arm64 ] + GOOS: [windows, linux, darwin] + GOARCH: [amd64, 386, arm, arm64] exclude: - GOOS: windows GOARCH: arm @@ -25,7 +25,7 @@ jobs: steps: - name: Checkout repo - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Cache uses: actions/cache@v3 @@ -59,3 +59,35 @@ jobs: path: dist/* retention-days: 90 + docker: + name: docker + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + + steps: + - uses: actions/checkout@v4 + + - name: Build image + run: docker build . --file Dockerfile --tag $NAME --label "runnumber=${GITHUB_RUN_ID}" + + - name: Log in to registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin + + - name: Push image + run: | + IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$NAME + + # This changes all uppercase characters to lowercase. + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + # This strips the git ref prefix from the version. + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + # This strips the "v" prefix from the tag name. + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + # This uses the Docker `latest` tag convention. + [ "$VERSION" == "master" ] && VERSION=latest + echo IMAGE_ID=$IMAGE_ID + echo VERSION=$VERSION + docker tag $NAME $IMAGE_ID:$VERSION + docker push $IMAGE_ID:$VERSION diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e1f1ffb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.21 AS build-stage +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download +COPY . ./ +RUN CGO_ENABLED=0 GOOS=linux go build -o /tagesschau-eilbot + +FROM gcr.io/distroless/base-debian12 AS release-stage +WORKDIR /app +COPY --from=build-stage /tagesschau-eilbot /app/tagesschau-eilbot +USER nonroot:nonroot +ENTRYPOINT ["/app/tagesschau-eilbot"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..8ddde29 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,34 @@ +version: '3.8' + +name: tagesschau-eilbot +services: + bot: + # build: . + pull_policy: always + image: ghcr.io/brawl345/tagesschau-eilbot:latest + restart: always + env_file: .env + + db: + image: mariadb:latest + restart: always + environment: + MYSQL_USER: ${MYSQL_USER} + MYSQL_PASSWORD: ${MYSQL_PASSWORD} + MYSQL_DATABASE: ${MYSQL_DB} + MYSQL_RANDOM_ROOT_PASSWORD: true + ports: + - '33060:3306' + volumes: + - 'tagesschau-eilbot-db:/var/lib/mysql' + healthcheck: + test: ['CMD', 'healthcheck.sh', '--connect', '--innodb_initialized'] + start_period: 1m + start_interval: 10s + interval: 1m + timeout: 5s + retries: 3 + +volumes: + tagesschau-eilbot-db: + name: tagesschau-eilbot-db diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index ce9392a..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -python-telegram-bot -redis -requests \ No newline at end of file