init project #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |