.github/workflows/docker.yml: use metadata action, run on PRs
Docker Build and Deploy / Build and push docker images (push) Has been cancelled Details
Linting / flake8 (push) Has been cancelled Details

This commit is contained in:
classabbyamp 2023-12-10 15:10:22 -05:00 committed by classabbyamp
parent 86da8d135a
commit 444687bd12
1 changed files with 36 additions and 49 deletions

View File

@ -3,6 +3,10 @@
name: Docker Build and Deploy
on:
workflow_dispatch:
pull_request:
branches:
- master
push:
# Publish `master` as Docker `dev` image.
branches:
@ -11,29 +15,43 @@ on:
tags:
- v*
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
docker:
name: Build and push docker images
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Checkout
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
uses: classabbyamp/treeless-checkout-action@v1
- name: Write ref to file
if: ${{ github.event_name != 'pull_request' }}
run: git rev-list -n 1 $GITHUB_REF > ./git_commit
- name: Build image
id: build_image
run: |
IMAGE_ID=${GITHUB_REPOSITORY,,}
IMAGE_NAME=${IMAGE_ID#*/}
echo "image_id=$IMAGE_ID" >> $GITHUB_ENV
echo "image_name=$IMAGE_NAME" >> $GITHUB_ENV
docker build . --file Dockerfile -t $IMAGE_NAME
- name: Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/${{ github.repository }}
tags: |
type=sha,prefix=
type=raw,value=dev,enable={{is_default_branch}}
type=raw,event=tag,value=latest,enable={{is_default_branch}}
type=ref,event=tag,prefix=,enable={{is_default_branch}},priority=1000
flavor: latest=false
labels: |
org.opencontainers.image.authors=classabbyamp and 0x5c
org.opencontainers.image.url=https://github.com/miaowware/qrm2
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.vendor=miaowware
org.opencontainers.image.title=qrm2
org.opencontainers.image.description=Discord bot with ham radio functions
- name: Login to Github Container Registry
uses: docker/login-action@v1
@ -42,41 +60,10 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Tag image
id: tag_image
run: |
IMAGE_NAME=${{ env.image_name }}
IMAGE_ID=ghcr.io/${{ env.image_id }}
echo IMAGE_ID=$IMAGE_ID
echo "image_id=$IMAGE_ID" >> $GITHUB_ENV
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# if version is master, set version to dev
[[ "$VERSION" == "master" ]] && VERSION=dev
echo VERSION=$VERSION
echo "version=$VERSION" >> $GITHUB_ENV
# tag dev or x.x.x
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
# tag latest if not a dev release
[[ "$VERSION" != "dev" ]] && docker tag $IMAGE_NAME $IMAGE_ID:latest || true
- name: Push images to registry
run: |
VERSION=${{ env.version }}
IMAGE_ID=${{ env.image_id }}
[[ "$VERSION" != "dev" ]] && docker push $IMAGE_ID:latest || true
docker push $IMAGE_ID:$VERSION
- name: Deploy official images
id: deploy_images
uses: satak/webrequest-action@v1
- name: Build and push
uses: docker/build-push-action@v5
with:
url: ${{ secrets.DEPLOY_URL }}
method: POST
headers: '{"Authentication": "Token ${{ secrets.DEPLOY_TOKEN }}"}'
payload: '{"version": "${{ env.version }}"}'
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}