-
Notifications
You must be signed in to change notification settings - Fork 0
49 lines (39 loc) · 1.77 KB
/
docker.yaml
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
name: Build image
on:
workflow_call:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- 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: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get short Git commit hash
run: echo "GIT_COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
# Always build and tag the image with the Git commit hash
- name: Build and push Docker image with Git commit hash
run: |
docker build -t ghcr.io/${{ github.repository }}:${{ env.GIT_COMMIT_HASH }} .
docker push ghcr.io/${{ github.repository }}:${{ env.GIT_COMMIT_HASH }}
# Conditional step: Only build and tag with "latest" on the main branch
- name: Build and push Docker image with "latest" tag
if: github.ref == 'refs/heads/main'
run: |
docker build -t ghcr.io/${{ github.repository }}:latest .
docker push ghcr.io/${{ github.repository }}:latest
# Conditional step: Only build and tag with version on tagged pushes
- name: Extract version from tag
if: startsWith(github.ref, 'refs/tags/v') # Only runs if it's a tag push
run: echo "IMAGE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: Build and push Docker image with version tag
if: startsWith(github.ref, 'refs/tags/v') # Only runs if it's a tag push
run: |
docker build -t ghcr.io/${{ github.repository }}:${{ env.IMAGE_VERSION }} .
docker push ghcr.io/${{ github.repository }}:${{ env.IMAGE_VERSION }}