Skip to content

Commit

Permalink
Add FileTree-API
Browse files Browse the repository at this point in the history
  • Loading branch information
carry0987 committed Mar 19, 2024
1 parent c4a7fff commit de04dd7
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
121 changes: 121 additions & 0 deletions .github/workflows/go-filetree-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
name: FileTree-API Docker Build and Push

on:
push:
tags:
- 'go-filetree-api-*'

jobs:
build-and-push:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
platform: linux/amd64
- os: buildjet-2vcpu-ubuntu-2204-arm
platform: linux/arm64
outputs:
version: ${{ env.version }}
latest: ${{ env.IS_LATEST }}
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Check out Sharp-API repo
uses: actions/checkout@v4
with:
repository: 'carry0987/FileTree-API'
token: ${{ secrets.GH_CLONE_TOKEN }}
path: 'Go/FileTree-API/source'

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Prepare the Docker tags list
id: prep
run: |
FULL_VERSION=$(echo ${{ github.ref }} | sed -e 's,.*/go-filetree-api-,,')
VERSION=${FULL_VERSION%-latest}
TAGS="${{ secrets.DOCKER_USERNAME }}/filetree-api:${VERSION}"
IS_LATEST=false
if [[ ${{ matrix.platform }} == "linux/amd64" ]]; then
SUFFIX="-amd64"
elif [[ ${{ matrix.platform }} == "linux/arm64" ]]; then
SUFFIX="-arm64"
fi
TAGS="${{ secrets.DOCKER_USERNAME }}/filetree-api:${VERSION}${SUFFIX}"
if [[ "$FULL_VERSION" == *"-latest" ]]; then
TAGS="$TAGS,${{ secrets.DOCKER_USERNAME }}/filetree-api:latest${SUFFIX}"
IS_LATEST=true
fi
echo "TAGS=$TAGS" >> $GITHUB_ENV
echo "PROGRAM_VERSION=${VERSION%}" >> $GITHUB_ENV
echo "version=${VERSION%}" >> $GITHUB_ENV
echo "IS_LATEST=$IS_LATEST" >> $GITHUB_ENV
echo "PROGRAM_VERSION=${VERSION%}"
echo "Building with tags: $TAGS"
- name: Start multi-arch build and push
uses: docker/build-push-action@v5
with:
context: Go/FileTree-API
file: Go/FileTree-API/Dockerfile
build-args: PROGRAM_VERSION=${{ env.PROGRAM_VERSION }}
push: true
tags: ${{ env.TAGS }}
platforms: ${{ matrix.platform }}
provenance: false

- name: Logout from DockerHub
run: docker logout

manifest:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Set image prefix
run: echo "IMAGE_PREFIX=${{ secrets.DOCKER_USERNAME }}/filetree-api:${{ needs.build-and-push.outputs.version }}" >> $GITHUB_ENV

- name: Create and push manifest images
uses: Noelware/docker-manifest-action@master
with:
inputs: ${{ secrets.DOCKER_USERNAME }}/filetree-api:${{ needs.build-and-push.outputs.version }}
images: ${{ env.IMAGE_PREFIX }}-amd64,${{ env.IMAGE_PREFIX }}-arm64
push: true
amend: true

- name: Check outputs for latest
run: echo "Latest ${{ needs.build-and-push.outputs.latest }}"

- name: Set image prefix for latest
if: ${{ needs.build-and-push.outputs.latest == 'true' }}
run: echo "IMAGE_PREFIX=${{ secrets.DOCKER_USERNAME }}/filetree-api:latest" >> $GITHUB_ENV

- name: Create and push manifest images for latest
if: ${{ needs.build-and-push.outputs.latest == 'true' }}
uses: Noelware/docker-manifest-action@master
with:
inputs: ${{ env.IMAGE_PREFIX }}
images: ${{ env.IMAGE_PREFIX }}-amd64,${{ env.IMAGE_PREFIX }}-arm64
push: true
amend: true

- name: Logout from DockerHub
run: docker logout
26 changes: 26 additions & 0 deletions Go/FileTree-API/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Build stage
FROM golang:1.21 AS builder

# Set the working directory
WORKDIR /app

# Copy the code from the GitHub Actions "Go/FileTree-API/source" directory
COPY source ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o filetree cmd/server/main.go

# Production stage
FROM --platform=$TARGETPLATFORM alpine:latest

# Set the working directory
WORKDIR /root/

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/filetree .

# Set the entry point
CMD ["./filetree"]
2 changes: 2 additions & 0 deletions Go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Docker-Go
This is a Docker image list for Go, suitable for various Web application developments.
7 changes: 6 additions & 1 deletion JS/Sharp-API/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Stage 1: Build stage
# Build stage
FROM node:21-alpine AS builder

# Set the working directory
WORKDIR /usr/src/app

# Copy the code from the GitHub Actions "JS/Sharp-API/source" directory
Expand All @@ -14,11 +16,14 @@ RUN npm prune --production && npm cache clean --force

# Production stage
FROM --platform=$TARGETPLATFORM node:21-alpine

# Set the working directory
WORKDIR /usr/src/app

# Copy built node modules and compiled code
COPY --from=builder /usr/src/app/node_modules ./node_modules
COPY --from=builder /usr/src/app/dist ./dist
COPY --from=builder /usr/src/app/package.json ./package.json

# Set the entry point
CMD ["npm", "run", "start"]

0 comments on commit de04dd7

Please sign in to comment.