From 021fe13b7e1ff4b9fbafa20d80c74220e2fc5644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 31 Oct 2023 05:02:15 +0100 Subject: [PATCH 1/2] ci: added dependabot, build, docker and deploy --- .github/dependabot.yml | 14 +++++++ .github/workflows/dockerci.yml | 40 ++++++++++++++++++++ .github/workflows/dockerflycd.yml | 61 +++++++++++++++++++++++++++++++ .github/workflows/testingci.yml | 23 ++++++++++++ Dockerfile => docker/Dockerfile | 10 ++++- fly.toml | 4 +- 6 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/dockerci.yml create mode 100644 .github/workflows/dockerflycd.yml create mode 100644 .github/workflows/testingci.yml rename Dockerfile => docker/Dockerfile (82%) diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..1094946b --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,14 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + - package-ecosystem: "docker" + directory: "/" + schedule: + interval: "daily" + - package-ecosystem: "npm" + directory: "/" + schedule: + interval: "daily" \ No newline at end of file diff --git a/.github/workflows/dockerci.yml b/.github/workflows/dockerci.yml new file mode 100644 index 00000000..4f371c63 --- /dev/null +++ b/.github/workflows/dockerci.yml @@ -0,0 +1,40 @@ +name: Docker CI + +on: + push: + branches: ['main'] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + permission: + contents: read + packages: read + + steps: + - uses: docker/setup-qemu-action@v3 + - uses: docker/setup-buildx-action@v3 + + - uses: docker/metadata-action@v4 + id: meta + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - uses: docker/build-push-action@v5 + env: + API_URL: 'http://example.org' + PUBLIC_API_URL: 'http://example.org' + with: + context: . + file: ./docker/Dockerfile + push: false + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + platforms: |- + linux/amd64 + linux/arm64 diff --git a/.github/workflows/dockerflycd.yml b/.github/workflows/dockerflycd.yml new file mode 100644 index 00000000..c5a2098c --- /dev/null +++ b/.github/workflows/dockerflycd.yml @@ -0,0 +1,61 @@ +name: Docker Fly CD + +on: + push: + tags: ['v*.*.*'] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + permission: + contents: read + packages: write + + steps: + - uses: docker/setup-qemu-action@v3 + - uses: docker/setup-buildx-action@v3 + + - uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - uses: docker/metadata-action@v4 + id: meta + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - uses: docker/build-push-action@v5 + env: + API_URL: ${{ secrets.API_URL }} + PUBLIC_API_URL: ${{ secrets.PUBLIC_API_URL }} + with: + context: . + file: ./docker/Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + platforms: |- + linux/amd64 + linux/arm64 + + deploy: + needs: build + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@v2 + + - run: flyctl deploy --remote-only + env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} diff --git a/.github/workflows/testingci.yml b/.github/workflows/testingci.yml new file mode 100644 index 00000000..879b76b4 --- /dev/null +++ b/.github/workflows/testingci.yml @@ -0,0 +1,23 @@ +name: Testing CI + +on: + pull_request: + branches: ['main'] + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: lts/* + cache: 'npm' + - run: npm ci + - run: npm run build + env: + API_URL: 'http://example.org' + PUBLIC_API_URL: 'http://example.org' diff --git a/Dockerfile b/docker/Dockerfile similarity index 82% rename from Dockerfile rename to docker/Dockerfile index 448b4ec6..0055bdde 100644 --- a/Dockerfile +++ b/docker/Dockerfile @@ -2,6 +2,7 @@ # Adjust NODE_VERSION as desired ARG NODE_VERSION=18.16.0 +ARG PORT=8080 FROM node:${NODE_VERSION}-slim as base LABEL fly_launch_runtime="NodeJS" @@ -11,7 +12,7 @@ WORKDIR /app # Set production environment ENV NODE_ENV=production - +ENV PORT="${PORT}" # Throw-away build stage to reduce size of final image FROM base as build @@ -21,7 +22,7 @@ RUN apt-get update -qq && \ apt-get install -y python-is-python3 pkg-config build-essential # Install node modules -COPY --link package.json package-lock.json . +COPY --link package.json package-lock.json ./ RUN npm install --production=false # Copy application code @@ -40,5 +41,10 @@ FROM base # Copy built application COPY --from=build /app /app +# Expose port +EXPOSE ${PORT} + # Start the server by default, this can be overwritten at runtime CMD [ "npm", "run", "start" ] + +LABEL org.opencontainers.image.source="https://github.com/tminaorg/prednjica" \ No newline at end of file diff --git a/fly.toml b/fly.toml index 918abd37..e515bb53 100644 --- a/fly.toml +++ b/fly.toml @@ -2,9 +2,7 @@ app = "prednjica" primary_region = "cdg" [build] - -[env] - PORT = "8080" + image = "ghcr.io/tminaorg/prednjica:latest" [http_service] internal_port = 8080 From 3ce2ca45e04c6fdc8a7a160ed2b3379b1e985173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 31 Oct 2023 05:08:04 +0100 Subject: [PATCH 2/2] ci(docker): switch to npm clean install --- docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 0055bdde..18a6f59c 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -23,7 +23,7 @@ RUN apt-get update -qq && \ # Install node modules COPY --link package.json package-lock.json ./ -RUN npm install --production=false +RUN npm ci --include=dev # Copy application code COPY --link . .