-
Notifications
You must be signed in to change notification settings - Fork 1
106 lines (93 loc) · 3.64 KB
/
docker-image.yml
File metadata and controls
106 lines (93 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
name: Build and Push Docker Image
on:
push:
branches:
- main
tags:
- "v*"
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Load build environment
id: build_env
run: |
BUILD_ENV_FILE="./.env.build"
if [ -f "${BUILD_ENV_FILE}" ]; then
echo "Loading build environment variables from ${BUILD_ENV_FILE}"
while IFS='=' read -r key value; do
if [[ ! -z "$key" && "$key" != "#"* ]]; then
echo "$key=$value" >> $GITHUB_ENV
fi
done < "${BUILD_ENV_FILE}"
echo "✅ Environment variables loaded"
else
echo "Warning: .env.build file not found, using defaults"
fi
# Set defaults if not in .env.build
echo "NEXT_PUBLIC_SITE_URL=${NEXT_PUBLIC_SITE_URL:-https://hackathonweekly.com}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_BUCKET_NAME=${NEXT_PUBLIC_BUCKET_NAME:-hackweek-public-1303088253}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_S3_ENDPOINT=${NEXT_PUBLIC_S3_ENDPOINT:-https://hackweek-public-1303088253.cos.ap-guangzhou.myqcloud.com}" >> $GITHUB_ENV
- name: Display environment variables
run: |
echo "NEXT_PUBLIC_SITE_URL: ${{ env.NEXT_PUBLIC_SITE_URL }}"
echo "NEXT_PUBLIC_BUCKET_NAME: ${{ env.NEXT_PUBLIC_BUCKET_NAME }}"
echo "NEXT_PUBLIC_S3_ENDPOINT: ${{ env.NEXT_PUBLIC_S3_ENDPOINT }}"
- name: Compute build metadata
run: |
BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
GIT_COMMIT=$(git rev-parse --short HEAD)
if git describe --tags --exact-match >/dev/null 2>&1; then
BUILD_VERSION=$(git describe --tags --exact-match)
else
BUILD_VERSION="${GITHUB_REF_NAME}-${GIT_COMMIT}"
fi
echo "BUILD_VERSION=${BUILD_VERSION}" >> $GITHUB_ENV
echo "BUILD_TIME=${BUILD_TIME}" >> $GITHUB_ENV
echo "GIT_COMMIT=${GIT_COMMIT}" >> $GITHUB_ENV
echo "BUILD_VERSION: ${BUILD_VERSION}"
echo "BUILD_TIME: ${BUILD_TIME}"
echo "GIT_COMMIT: ${GIT_COMMIT}"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=sha
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
- name: Build and push to GitHub Container Registry
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
NEXT_PUBLIC_SITE_URL=${{ env.NEXT_PUBLIC_SITE_URL }}
NEXT_PUBLIC_BUCKET_NAME=${{ env.NEXT_PUBLIC_BUCKET_NAME }}
NEXT_PUBLIC_S3_ENDPOINT=${{ env.NEXT_PUBLIC_S3_ENDPOINT }}
BUILD_VERSION=${{ env.BUILD_VERSION }}
BUILD_TIME=${{ env.BUILD_TIME }}
GIT_COMMIT=${{ env.GIT_COMMIT }}
cache-from: type=gha
cache-to: type=gha,mode=max