Skip to content

Commit

Permalink
👷 parameterize workflow ref
Browse files Browse the repository at this point in the history
  • Loading branch information
oasaph committed Jul 20, 2024
1 parent e3c49f3 commit e20eef0
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ on:
tag:
description: "Tag for the release"
required: true
ref:
description: "Branch to pull from"
required: false

push:
branches-ignore:
- "main"
tags:
- "v*.*.*"
paths-ignore:
- ./github

env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
IMAGE_TAG: ${{ github.ref_name }}-${{ github.sha }}
# IMAGE_TAG: ${{ github.ref_name }}-${{ github.sha }}
jobs:
build-and-push:
outputs:
image_tag: ${{ steps.set_image_tag.outputs.IMAGE_TAG }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -32,13 +39,22 @@ jobs:
with:
username: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
password: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
- name: Determine ref
id: set_ref
run: |
if [[ ${{ github.event.inputs.ref }} ]]; then
echo "ref=${{ github.event.inputs.ref }}" >> $GITHUB_OUTPUT
else
echo "ref=${{ github.ref_name }}" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Determine IMAGE_TAG
id: set_image_tag
run: |
if [[ ${{ github.event.inputs.tag }} ]]; then
echo "::set-output name=tag::${{ github.event.inputs.tag }}"
echo "IMAGE_TAG=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
else
echo "::set-output name=tag::${{ github.ref_name }}-${{ github.sha }}"
echo "IMAGE_TAG=${{ steps.set_ref.outputs.ref }}-${{ github.sha }}" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Build and push Docker image
Expand All @@ -48,6 +64,7 @@ jobs:
file: ./Dockerfile
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.set_image_tag.outputs.tag }}

create-release:
needs: build-and-push
runs-on: ubuntu-latest
Expand All @@ -60,8 +77,8 @@ jobs:
sudo apt-get install -y docker.io
- name: Load Docker image
run: |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
docker save ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} > image.tar
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }}
docker save ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }} > image.tar
- name: Upload Docker image as artifact
uses: actions/upload-artifact@v4
with:
Expand All @@ -77,13 +94,13 @@ jobs:
release_name: Release ${{ github.sha }}
draft: false
prerelease: false
body: "Release of ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}"
body: "Release of ${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }}"
- name: Upload Docker image to GitHub Release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./image.tar
asset_name: docker-image-${{ env.IMAGE_TAG }}.tar
asset_name: docker-image-${{ needs.build-and-push.outputs.image_tag }}.tar
asset_content_type: application/octet-stream

0 comments on commit e20eef0

Please sign in to comment.