-
Notifications
You must be signed in to change notification settings - Fork 0
47 lines (40 loc) · 1.4 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
name: Build and Push Docker image to ghcr.io
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
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
- name: Build Docker image
run: |
docker build -t $IMAGE_NAME:$IMAGE_TAG .
# Step 5: Push the Docker image to GitHub Container Registry
- name: Push Docker image to GHCR
run: |
docker push $IMAGE_NAME:$IMAGE_TAG