-
Notifications
You must be signed in to change notification settings - Fork 1
54 lines (45 loc) · 1.5 KB
/
Copy pathdocker.yml
File metadata and controls
54 lines (45 loc) · 1.5 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
name: Docker Image CI
on:
push:
branches:
- main
- dev # Add your dev branch here
- 'feature/**' # Optional: for any feature branch
jobs:
build_and_push_image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Determine Image Tags
id: set_tags
run: |
# Get a sanitized, lowercased version of the branch name (e.g., "dev")
BRANCH_NAME=$(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9-]/_/g' | tr '[:upper:]' '[:lower:]')
# Define the base name for the image
IMAGE_NAME=ghcr.io/${{ github.repository }}
# Conditionally set tags based on the branch
if [[ "${{ github.ref_name }}" == "main" ]]; then
# For the main branch, tag with 'latest'
TAGS="${IMAGE_NAME}:latest"
else
# For all other branches (like 'dev'), tag with the branch name
TAGS="${IMAGE_NAME}:${BRANCH_NAME}"
fi
# Output the tags for use in the next step
echo "TAGS=${TAGS}" >> $GITHUB_OUTPUT
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.set_tags.outputs.TAGS }}