From 8e749d23978406c7e7890e076aba628bf9757c4c Mon Sep 17 00:00:00 2001 From: Ehab Younes Date: Mon, 11 Nov 2024 21:49:01 +0300 Subject: [PATCH] Use Dockerfile as source of truth instead of a GitHub action --- .github/workflows/docker-ci.yaml | 62 +++++++++++++++++++++--------- .github/workflows/ui-tests-ci.yaml | 34 ---------------- Dockerfile | 33 +++++++++++----- 3 files changed, 67 insertions(+), 62 deletions(-) delete mode 100644 .github/workflows/ui-tests-ci.yaml diff --git a/.github/workflows/docker-ci.yaml b/.github/workflows/docker-ci.yaml index 61c1355..5031866 100644 --- a/.github/workflows/docker-ci.yaml +++ b/.github/workflows/docker-ci.yaml @@ -8,38 +8,53 @@ on: - "go.sum" - "ui/**" - "Dockerfile" - workflow_dispatch: - + workflow_dispatch: jobs: - docker: - permissions: - packages: write - contents: write - issues: write # to be able to comment on released issues - pull-requests: write # to be able to comment on released pull requests - id-token: write # to enable use of OIDC for npm provenance + # Job 1: Checkout code and set up common environment + setup: runs-on: ubuntu-latest + outputs: + ref_name: ${{ steps.get_ref.outputs.ref_name }} steps: - - name: Checkout + - name: Checkout code + id: get_ref uses: actions/checkout@v4 with: fetch-depth: 0 + - name: Get branch name + id: ref + run: echo "ref_name=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV + # Job 2: Build the Docker image + build: + needs: setup + runs-on: ubuntu-latest + steps: - name: Build docker image uses: docker/build-push-action@v6 with: load: true + # Job 3: Run Semantic Release + semantic-release: + needs: build + runs-on: ubuntu-latest + steps: - name: Semantic Release uses: cycjimmy/semantic-release-action@v4 id: release env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + # Job 4: Login to Docker registries + docker-login: + needs: semantic-release + if: ${{ github.ref_name == 'main' && needs.semantic-release.outputs.new_release_published == 'true' }} + runs-on: ubuntu-latest + steps: - name: Login to Docker registry uses: docker/login-action@v3 - if: ${{ github.ref_name == 'main' }} with: registry: ghcr.io username: ${{ github.actor }} @@ -47,25 +62,34 @@ jobs: - name: Login to Docker Hub uses: docker/login-action@v3 - if: ${{ github.ref_name == 'main' }} with: username: ${{ vars.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - + + # Job 5: Push Docker image + push-image: + needs: [semantic-release, docker-login] + if: ${{ github.ref_name == 'main' && needs.semantic-release.outputs.new_release_published == 'true' }} + runs-on: ubuntu-latest + steps: - name: Push docker image uses: docker/build-push-action@v6 - if: ${{ github.ref_name == 'main' && steps.release.outputs.new_release_published == 'true' }} with: push: true tags: | ${{ vars.DOCKERHUB_USERNAME }}/swarm-cd:latest - ${{ vars.DOCKERHUB_USERNAME }}/swarm-cd:${{ steps.release.outputs.new_release_version }} + ${{ vars.DOCKERHUB_USERNAME }}/swarm-cd:${{ needs.semantic-release.outputs.new_release_version }} ghcr.io/${{ github.repository }}:latest - ghcr.io/${{ github.repository }}:${{ steps.release.outputs.new_release_version }} + ghcr.io/${{ github.repository }}:${{ needs.semantic-release.outputs.new_release_version }} + # Job 6: Update Docker Hub repo description + update-dockerhub-description: + needs: push-image + if: ${{ github.ref_name == 'main' && needs.semantic-release.outputs.new_release_published == 'true' }} + runs-on: ubuntu-latest + steps: - name: Update Docker Hub repo description uses: peter-evans/dockerhub-description@e98e4d1628a5f3be2be7c231e50981aee98723ae # v4.0.0 - if: ${{ github.ref_name == 'main' && steps.release.outputs.new_release_published == 'true' }} with: username: ${{ vars.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} diff --git a/.github/workflows/ui-tests-ci.yaml b/.github/workflows/ui-tests-ci.yaml deleted file mode 100644 index 1df27c2..0000000 --- a/.github/workflows/ui-tests-ci.yaml +++ /dev/null @@ -1,34 +0,0 @@ -# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs - -name: Run All UI Tests - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -jobs: - build: - - runs-on: ubuntu-latest - - strategy: - matrix: - node-version: [18.x] - - steps: - - uses: actions/checkout@v4 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v4 - with: - cache-dependency-path: ui/package-lock.json - node-version: ${{ matrix.node-version }} - cache: 'npm' - - run: npm ci - working-directory: ui - - run: npm run build - working-directory: ui - - run: npm test - working-directory: ui diff --git a/Dockerfile b/Dockerfile index dd01356..4f288f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,37 @@ -FROM node:22-alpine3.19 AS ui +# Base stage for the front end (used for both build and test) +FROM node:22-alpine3.19 AS frontend-base WORKDIR /ui -COPY ui/package.json ui/package-lock.json . +COPY ui/package.json ui/package-lock.json ./ RUN npm install -COPY ui/ . -RUN npm run build && npm test run +COPY ui/ ./ -FROM golang:1.22.5 AS backend +# Stage 1: Build the front end +FROM frontend-base AS frontend-build +RUN npm run build + +# Stage 2: Test the front end +FROM frontend-base AS frontend-test +RUN npm run build +RUN npm test + +# Stage 3: Build the backend +FROM golang:1.22.5 AS backend-build +WORKDIR /backend COPY go.mod go.sum ./ RUN go mod download COPY cmd/ cmd/ COPY util/ util/ -COPY web/ web/ +COPY web/ web/ COPY swarmcd/ swarmcd/ RUN CGO_ENABLED=0 GOOS=linux go build -o /swarm-cd ./cmd/ -FROM alpine:3.2 +# Stage 4: Test the backend (if applicable) +# FROM backend-build AS backend-test + +# Stage 5: Packaging +FROM alpine:3.2 AS final WORKDIR /app RUN apk add --no-cache ca-certificates && update-ca-certificates -COPY --from=ui /ui/dist/ . -COPY --from=backend /swarm-cd . +COPY --from=frontend-build /ui/dist/ /app/ui/ +COPY --from=backend-build /swarm-cd /app/ CMD ["/app/swarm-cd"] \ No newline at end of file