Skip to content

Commit c7ae697

Browse files
committed
Enhance GitHub Actions workflow to build and push Docker images to GitHub Container Registry
1 parent 95c52dc commit c7ae697

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

.github/workflows/docker-build.yml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,38 @@ on:
99
jobs:
1010
build:
1111
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
packages: write # Grant write permission to packages
1215
steps:
1316
- name: Checkout repository
1417
uses: actions/checkout@v4
1518

19+
- name: Log in to GitHub Container Registry
20+
uses: docker/login-action@v3
21+
with:
22+
registry: ghcr.io
23+
username: ${{ github.actor }}
24+
password: ${{ secrets.GITHUB_TOKEN }}
25+
1626
- name: Build and tag the Docker image
1727
run: |
18-
docker build . --file Dockerfile --tag doh-server:latest
19-
# If you want to push to Docker Hub, uncomment the following lines
20-
# and ensure DOCKER_USERNAME and DOCKER_PASSWORD are set as GitHub Secrets.
21-
# echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
22-
# docker tag doh-server:latest your_dockerhub_username/doh-server:latest
23-
# docker push your_dockerhub_username/doh-server:latest
28+
IMAGE_NAME=doh-server
29+
IMAGE_TAG=${{ github.sha }}
30+
# Use lowercase for owner and repo name for GHCR compatibility
31+
REPO_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')
32+
REPO_NAME=$(echo ${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')
33+
GHCR_IMAGE=ghcr.io/$REPO_OWNER/$REPO_NAME/$IMAGE_NAME
34+
35+
docker build . --file Dockerfile --tag $GHCR_IMAGE:latest --tag $GHCR_IMAGE:$IMAGE_TAG
36+
37+
- name: Push the Docker image to GitHub Container Registry
38+
run: |
39+
IMAGE_NAME=doh-server
40+
IMAGE_TAG=${{ github.sha }}
41+
REPO_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')
42+
REPO_NAME=$(echo ${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')
43+
GHCR_IMAGE=ghcr.io/$REPO_OWNER/$REPO_NAME/$IMAGE_NAME
44+
45+
docker push $GHCR_IMAGE:latest
46+
docker push $GHCR_IMAGE:$IMAGE_TAG

0 commit comments

Comments
 (0)