Skip to content

Added Release Feature (#7) #11

Added Release Feature (#7)

Added Release Feature (#7) #11

name: Build and Push Docker Image
on:
push:
branches:
- master
tags:
- 'v*'
pull_request:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile:
- Dockerfile.ubuntu
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Docker registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Assign environment variables used in subsequent steps
- name: Env IMG_REPO_NAME Assignment
run: echo "IMG_REPO_NAME=$(echo ${{ matrix.dockerfile }} | sed 's/^Dockerfile\.\(.*\)$/\1/' | tr '[:upper:]' '[:lower:]')-wsl" >> "${GITHUB_ENV}"
# TAG_NAME defaults to 'latest' if not a release or manual deployment
- name: Assign TAG_NAME
run: |
echo "TAG_NAME=latest" >> "${GITHUB_ENV}"
if [ "${{ github.event.release.tag_name }}" != "" ]; then
echo "TAG_NAME=${{ github.event.release.tag_name }}" >> "${GITHUB_ENV}"
fi;
if [ "${{ github.event.inputs.version }}" != "" ]; then
echo "TAG_NAME=${{ github.event.inputs.version }}" >> "${GITHUB_ENV}"
fi;
- name: Build Docker image only
if: github.event_name == 'pull_request'
uses: docker/build-push-action@v6
with:
context: Dockerfiles
file: Dockerfiles/${{ matrix.dockerfile }}
push: false
tags: ghcr.io/${{ github.actor }}/${{ env.IMG_REPO_NAME }}:${{ env.TAG_NAME }}
- name: Build and push Docker image
if: github.event_name == 'push'
uses: docker/build-push-action@v6
with:
context: Dockerfiles
file: Dockerfiles/${{ matrix.dockerfile }}
push: true
tags: ghcr.io/${{ github.actor }}/${{ env.IMG_REPO_NAME }}:${{ env.TAG_NAME }}
cache-from: type=registry,ref=ghcr.io/${{ github.actor }}/${{ env.IMG_REPO_NAME }}:${{ env.TAG_NAME }}
cache-to: type=registry,ref=ghcr.io/${{ github.actor }}/${{ env.IMG_REPO_NAME }}:${{ env.TAG_NAME }},mode=max
- name: Save Docker image to tarball
if: github.event_name == 'push'
run: |
docker save ghcr.io/${{ github.actor }}/${{ env.IMG_REPO_NAME }}:${{ env.TAG_NAME }} -o ${{ env.IMG_REPO_NAME }}.tar
- name: Create GitHub release
if: github.event_name == 'push'
uses: actions/create-release@v1
with:
tag_name: ${{ env.TAG_NAME }}
release_name: Release ${{ env.TAG_NAME }}
draft: false
prerelease: false
id: create_release
- name: Upload tar as release asset
if: github.event_name == 'push'
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./${{ env.IMG_REPO_NAME }}.tar
asset_name: ${{ env.IMG_REPO_NAME }}.tar
asset_content_type: application/x-tar