Update main.yml #24
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
#CI | |
name: Build and Publish Docker Image to DockerHub | |
on: | |
push: | |
branches: ["main"] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: DockerHub Login | |
uses: docker/[email protected] | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Set Version, Build Number, and Repository Name | |
id: set_version | |
run: | | |
VERSION="prod-1.0" | |
REPO_NAME="${{ github.repository }}" # Fetch the full repository name (owner/repo) | |
REPO_NAME_ONLY=$(echo $REPO_NAME | cut -d '/' -f 2) # Extract just the repo name (after /) | |
BUILD_NUMBER=${{ github.run_number }} | |
# Check if the version already exists on Docker Hub | |
if docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${REPO_NAME_ONLY}:$VERSION 2>/dev/null; then | |
# Extract the current major and minor version | |
MAJOR=$(echo $VERSION | cut -d '.' -f 1) | |
MINOR=$(echo $VERSION | cut -d '.' -f 2) | |
PATCH=$(echo $VERSION | cut -d '.' -f 3) | |
# Increment the patch version | |
PATCH=$((PATCH + 1)) | |
VERSION="$MAJOR.$MINOR.$PATCH" | |
fi | |
# Combine version and build number | |
TAG="$REPO_NAME_ONLY$VERSION_build.$BUILD_NUMBER" | |
TAG="${REPO_NAME_ONLY}-$VERSION-build.$BUILD_NUMBER" | |
# Export variables | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
echo "BUILD_NUMBER=$BUILD_NUMBER" >> $GITHUB_ENV | |
echo "TAG=$TAG" >> $GITHUB_ENV | |
- name: Build the Docker image | |
run: | | |
docker build . \ | |
--file Dockerfile \ | |
--tag ${{ secrets.DOCKERHUB_USERNAME }}/karos-repo:${{ env.TAG }} | |
- name: Docker Push | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/karos-repo:${{ env.TAG }} |