-
Notifications
You must be signed in to change notification settings - Fork 0
51 lines (44 loc) · 1.5 KB
/
build.yml
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
name: Build and Push Docker image to GHCR
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on:
group: self-hosted
#labels:
# - Linux
permissions:
packages: write
contents: read
steps:
# Step 1: Checkout the repository
- name: Checkout repository
uses: actions/checkout@v3
# Step 2: Log in to GitHub Container Registry (GHCR)
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Step 3: Define dynamic image name and tag based on repo name
- name: Set dynamic image name and tag
id: vars
run: |
REPO_NAME=$(basename "${{ github.repository }}") # Extract the repository name
IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/$REPO_NAME"
GIT_SHA="${{ github.sha }}"
TAG="main-${GIT_SHA:0:7}" # Example: Use first 7 characters of commit SHA
echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV
echo "IMAGE_TAG=$TAG" >> $GITHUB_ENV
# Step 4: Build the Docker image with both tags
- name: Build Docker image
run: |
docker build -t $IMAGE_NAME:$IMAGE_TAG -t $IMAGE_NAME:latest .
# Step 5: Push both Docker image tags to GHCR
- name: Push Docker image to GHCR
run: |
docker push $IMAGE_NAME:$IMAGE_TAG
docker push $IMAGE_NAME:latest