Skip to content

Build and Push Docker Image #1082

Build and Push Docker Image

Build and Push Docker Image #1082

Workflow file for this run

name: Build and Push Docker Image
on:
push:
branches:
- main
pull_request:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository code
- name: Checkout repository
uses: actions/checkout@v3
# Step 2: Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# Step 3: Fetch the latest release version from GitHub API
- name: Get latest release version
id: get_version
run: |
VERSION=$(curl -s https://api.github.com/repos/Byte-Nova/Rimworld-Together/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$VERSION" ]; then
echo "Failed to fetch version from GitHub."
exit 1
fi
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "Latest version is $VERSION."
# Step 4: 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 5: Build the Docker image with both the version and latest tags (lowercase repository)
- name: Build Docker image
run: |
LOWERCASE_REPO=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
docker build --build-arg VERSION=$VERSION -t ghcr.io/$LOWERCASE_REPO:$VERSION .
docker tag ghcr.io/$LOWERCASE_REPO:$VERSION ghcr.io/$LOWERCASE_REPO:latest
# Step 6: Push the Docker image to GHCR (both version and latest tags)
- name: Push Docker image
run: |
LOWERCASE_REPO=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
docker push ghcr.io/$LOWERCASE_REPO:$VERSION
docker push ghcr.io/$LOWERCASE_REPO:latest