test: docker and gh actions #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Push Docker Image to GHCR | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| jobs: | |
| docker: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1️⃣ 拉取代码 | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| # 2️⃣ 登录 GHCR | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| # 3️⃣ 设置日期 + short SHA 作为 TAG | |
| - name: Set image tag | |
| id: vars | |
| run: | | |
| DATE_TAG=$(date +'%Y%m%d') | |
| SHORT_SHA=${GITHUB_SHA::7} | |
| echo "IMAGE_TAG=${DATE_TAG}-${SHORT_SHA}" >> $GITHUB_ENV | |
| echo "IMAGE_NAME=ghcr.io/${GITHUB_REPOSITORY}/gitinsight" >> $GITHUB_ENV | |
| # 4️⃣ 构建 Docker 镜像 | |
| - name: Build Docker image | |
| run: | | |
| docker build -t ${IMAGE_NAME}:${IMAGE_TAG} . | |
| # 5️⃣ 推送 Docker 镜像到 GHCR | |
| - name: Push Docker image | |
| run: | | |
| docker push ${IMAGE_NAME}:${IMAGE_TAG} | |
| # 6️⃣ 可选:打印最终镜像 | |
| - name: Print Docker image info | |
| run: | | |
| echo "Docker image pushed: ${IMAGE_NAME}:${IMAGE_TAG}" |