Skip to content

Commit

Permalink
Use Dockerfile as source of truth instead of a GitHub action
Browse files Browse the repository at this point in the history
  • Loading branch information
EhabY committed Nov 11, 2024
1 parent e5fef7e commit 7b14875
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 61 deletions.
66 changes: 48 additions & 18 deletions .github/workflows/docker-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,94 @@ 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
checkout:
runs-on: ubuntu-latest
outputs:
ref_name: ${{ steps.get_ref.outputs.ref_name }}
steps:
- name: Checkout
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

build:
needs: checkout
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Build docker image
uses: docker/build-push-action@v6
with:
load: true

semantic-release:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v4
id: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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 }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to Docker Hub
uses: docker/login-action@v3
if: ${{ github.ref_name == 'main' }}
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}


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: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- 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 }}
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 }}
Expand Down
34 changes: 0 additions & 34 deletions .github/workflows/ui-tests-ci.yaml

This file was deleted.

33 changes: 24 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit 7b14875

Please sign in to comment.