Skip to content

Commit d2a000d

Browse files
added docker-build (#7)
1 parent 89628f1 commit d2a000d

File tree

4 files changed

+230
-0
lines changed

4 files changed

+230
-0
lines changed

.dockerignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Include any files or directories that you don't want to be copied to your
2+
# container here (e.g., local build artifacts, temporary files, etc.).
3+
#
4+
# For more help, visit the .dockerignore file reference guide at
5+
# https://docs.docker.com/go/build-context-dockerignore/
6+
7+
**/.DS_Store
8+
**/.classpath
9+
**/.dockerignore
10+
**/.env
11+
**/.git
12+
**/.gitignore
13+
**/.project
14+
**/.settings
15+
**/.toolstarget
16+
**/.vs
17+
**/.vscode
18+
**/*.*proj.user
19+
**/*.dbmdl
20+
**/*.jfm
21+
**/bin
22+
**/charts
23+
**/docker-compose*
24+
**/compose.y*ml
25+
**/Dockerfile*
26+
**/node_modules
27+
**/npm-debug.log
28+
**/obj
29+
**/secrets.dev.yaml
30+
**/values.dev.yaml
31+
LICENSE
32+
README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Docker Image Build on tag or release
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
release:
8+
types: [published]
9+
10+
env:
11+
ORG: opentelekomcloud
12+
PROJECT: status-dashboard-v3-api
13+
14+
jobs:
15+
push_if_tag:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- name: Docker meta
22+
id: meta
23+
uses: docker/metadata-action@v5
24+
with:
25+
images: |
26+
"${{ secrets.REGISTRY }}/${{ env.ORG }}/${{ env.PROJECT }}"
27+
tags: |
28+
type=schedule
29+
type=ref,event=branch
30+
type=ref,event=pr
31+
type=semver,pattern={{version}}
32+
type=semver,pattern={{major}}.{{minor}}
33+
type=semver,pattern={{major}}
34+
type=sha
35+
36+
- name: Set up Docker Buildx
37+
uses: docker/setup-buildx-action@v3
38+
39+
- name: Login to Container Registry
40+
uses: docker/login-action@v3
41+
with:
42+
registry: ${{ secrets.REGISTRY }}
43+
username: ${{ secrets.REGISTRY_USER }}
44+
password: ${{ secrets.REGISTRY_PASSWORD }}
45+
46+
- name: Build and push
47+
uses: docker/build-push-action@v5
48+
with:
49+
context: .
50+
tags: ${{ steps.meta.outputs.tags }}
51+
labels: ${{ steps.meta.outputs.labels }}
52+
push: true
53+
build-args: |
54+
BASE_URL=${{ secrets.BASE_URL }}
55+
AUTH_TOKEN=${{ secrets.AUTH_TOKEN }}

.github/workflows/docker-build.yaml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Docker Image Build
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- closed
8+
- edited
9+
- reopened
10+
- synchronize
11+
12+
env:
13+
ORG: stackmon
14+
PROJECT: status-dashboard-v3
15+
16+
jobs:
17+
18+
build:
19+
if: github.event.pull_request.merged == false
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- name: Docker meta
26+
id: meta
27+
uses: docker/metadata-action@v5
28+
with:
29+
images: |
30+
"${{ secrets.REGISTRY }}/${{ env.ORG }}/${{ env.PROJECT }}"
31+
tags: |
32+
type=schedule
33+
type=ref,event=branch
34+
type=ref,event=pr
35+
type=semver,pattern={{version}}
36+
type=semver,pattern={{major}}.{{minor}}
37+
type=semver,pattern={{major}}
38+
type=sha
39+
40+
- name: Set up Docker Buildx
41+
uses: docker/setup-buildx-action@v3
42+
43+
- name: Build and push
44+
uses: docker/build-push-action@v5
45+
with:
46+
context: .
47+
tags: ${{ steps.meta.outputs.tags }}
48+
labels: ${{ steps.meta.outputs.labels }}
49+
push: false
50+
51+
push_if_merged:
52+
if: github.event.pull_request.merged == true
53+
runs-on: ubuntu-latest
54+
55+
steps:
56+
- uses: actions/checkout@v4
57+
58+
- name: Docker meta
59+
id: meta
60+
uses: docker/metadata-action@v5
61+
with:
62+
images: |
63+
"${{ secrets.REGISTRY }}/${{ env.ORG }}/${{ env.PROJECT }}"
64+
tags: |
65+
type=schedule
66+
type=ref,event=branch
67+
type=ref,event=pr
68+
type=semver,pattern={{version}}
69+
type=semver,pattern={{major}}.{{minor}}
70+
type=semver,pattern={{major}}
71+
type=sha
72+
73+
- name: Set up Docker Buildx
74+
uses: docker/setup-buildx-action@v3
75+
76+
- name: Login to Container Registry
77+
uses: docker/login-action@v3
78+
with:
79+
registry: ${{ secrets.REGISTRY }}
80+
username: ${{ secrets.REGISTRY_USER }}
81+
password: ${{ secrets.REGISTRY_PASSWORD }}
82+
83+
- name: Build and push
84+
uses: docker/build-push-action@v5
85+
with:
86+
context: .
87+
tags: ${{ steps.meta.outputs.tags }}
88+
labels: ${{ steps.meta.outputs.labels }}
89+
push: true

Dockerfile

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# syntax=docker/dockerfile:1
2+
3+
# Create a stage for building the application.
4+
ARG GO_VERSION=1.22
5+
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS build
6+
WORKDIR /usr/src/app
7+
8+
# Cache dependencies
9+
RUN --mount=type=cache,target=/go/pkg/mod/ \
10+
--mount=type=bind,source=go.sum,target=go.sum \
11+
--mount=type=bind,source=go.mod,target=go.mod \
12+
go mod download -x
13+
14+
ARG TARGETARCH
15+
16+
# Build the application
17+
RUN --mount=type=cache,target=/go/pkg/mod/ \
18+
--mount=type=bind,target=. \
19+
CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /usr/local/bin/app cmd/main.go
20+
21+
# Stage 2: Create a minimal production image
22+
FROM alpine:3.20 AS final
23+
24+
# Install necessary packages
25+
RUN --mount=type=cache,target=/var/cache/apk \
26+
apk --update add \
27+
ca-certificates \
28+
tzdata \
29+
&& \
30+
update-ca-certificates
31+
32+
# Create a non-privileged user
33+
ARG UID=1001
34+
RUN adduser \
35+
--disabled-password \
36+
--gecos "" \
37+
--home "/nonexistent" \
38+
--shell "/sbin/nologin" \
39+
--no-create-home \
40+
--uid "${UID}" \
41+
appuser
42+
43+
USER appuser
44+
45+
WORKDIR /usr/src/app
46+
47+
# Copy the binary from the build stage
48+
COPY --from=build --chown=appuser:appuser /usr/local/bin/app .
49+
50+
# Expose the port
51+
EXPOSE 8000
52+
53+
# Define the command to run
54+
ENTRYPOINT [ "/usr/src/app/app" ]

0 commit comments

Comments
 (0)