Skip to content

[pre-commit.ci] pre-commit autoupdate (#13) #29

[pre-commit.ci] pre-commit autoupdate (#13)

[pre-commit.ci] pre-commit autoupdate (#13) #29

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: Set variable based on actor
run: |
if [[ "${{ github.actor }}" == *"bot"* ]]; then
echo "GITHUB_ACTOR=bot" >> $GITHUB_ENV
else
echo "GITHUB_ACTOR=${{ github.actor }}" >> $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
load: true
tags: ghcr.io/${{ env.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
load: true
tags: ghcr.io/${{ env.GITHUB_ACTOR }}/${{ env.IMG_REPO_NAME }}:${{ env.TAG_NAME }}
- name: Save Docker image to tarball
run: |
docker run -t --name wsl_export ghcr.io/${{ env.GITHUB_ACTOR }}/${{ env.IMG_REPO_NAME }}:${{ env.TAG_NAME }} ls
docker export wsl_export > ${{ env.IMG_REPO_NAME }}.tar
- name: Check if release exists
id: check-release
run: |
response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ env.TAG_NAME }}")
if echo "$response" | grep -q '"tag_name":'; then
release_id=$(echo "$response" | jq -r '.id')
echo "RELEASE_EXISTS=true" >> $GITHUB_ENV
echo "RELEASE_ID=$release_id" >> $GITHUB_ENV
else
echo "RELEASE_EXISTS=false" >> $GITHUB_ENV
fi
- name: Delete existing release
if: env.RELEASE_EXISTS == 'true'
run: |
echo "Deleting existing release with ID ${{ env.RELEASE_ID }}"
curl -s -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/${{ env.RELEASE_ID }}"
- name: Create GitHub release
if: github.event_name == 'push'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ env.TAG_NAME }}
release_name: Release ${{ env.IMG_REPO_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
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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