Skip to content

Commit

Permalink
use reusable workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Divkix authored Mar 1, 2023
1 parent 3ef4034 commit 36d2b54
Showing 1 changed file with 23 additions and 110 deletions.
133 changes: 23 additions & 110 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,136 +3,49 @@ name: Release
on:
push:
tags:
- '*'
- "*"
workflow_dispatch:
inputs:
tag:
description: 'Tag to release'
description: "Tag to release"
required: true

jobs:
# only run this step when the tag is not already created
create-tag:
name: Create new tag
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Create and Push Tag
if: startsWith(github.ref, 'refs/tags/') != 'true' && github.event.inputs.tag != ''
run: |-
git config --global user.email "[email protected]"
git config --global user.name "Divanshu Chauhan"
tag=${{ github.event.inputs.tag }} # if triggered by workflow_dispatch
git tag -f -a -m "$tag" "$tag"
git push -f origin "$tag"
if: startsWith(github.ref, 'refs/tags/') != 'true' && github.event.inputs.tag != ''
uses: divkix/reusable-workflows/.github/workflows/create-tag.yml@main
with:
tag: ${{ inputs.tag }}

# fetch some info from repo such as latest tag and name in lowercase
get-repo-info:
name: Get Repo Info
runs-on: ubuntu-latest
needs: create-tag
steps:
- name: Checkout current repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get Latest Tag
id: get_tag
run: |-
tag="$(git tag | sort -V | tail -1)"
echo "::set-output name=tag::$(git tag | sort -V | tail -1)"
- name: Lowercase required vars
id: lowercase
run: |-
echo "::set-output name=project_name::$(echo ${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')"
echo "::set-output name=repo_name::$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')"
outputs:
tag: ${{ steps.get_tag.outputs.tag }}
project_name: ${{ steps.lowercase.outputs.project_name }}
repo_name: ${{ steps.lowercase.outputs.repo_name }}
uses: divkix/reusable-workflows/.github/workflows/get-repo-info.yml@main

# build the docker image and push it to docker hub and ghcr
docker-build:
name: Build and Push Docker Images
needs: get-repo-info
runs-on: ubuntu-latest
steps:
- name: Checkout current repo
uses: actions/checkout@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set ENV Variables
id: vars
run: |
echo "BUILD_DATE=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
echo "GIT_SHA=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_ENV
echo "GIT_REF=$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: |-
ghcr.io/${{ needs.get-repo-info.outputs.repo_name }}:latest
ghcr.io/${{ needs.get-repo-info.outputs.repo_name }}:${{ needs.get-repo-info.outputs.tag }}
${{ needs.get-repo-info.outputs.repo_name }}:latest
${{ needs.get-repo-info.outputs.repo_name }}:${{ needs.get-repo-info.outputs.tag }}
labels: |-
org.opencontainers.image.authors=${{ github.repository_owner }}
org.opencontainers.image.created=${{ env.BUILD_DATE }}
org.opencontainers.image.description=Created from commit ${{ env.GIT_SHA }} and ref ${{ env.GIT_REF }}
org.opencontainers.image.ref.name=${{ env.GIT_REF }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.source=https://github.com/${{ needs.get-repo-info.outputs.repo_name }}
org.opencontainers.image.version=${{ needs.get-repo-info.outputs.tag }}
# update description of the docker image on docker hub
update-dockerhub-description:
name: Update DockerHub Description
needs: docker-build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
uses: divkix/reusable-workflows/.github/workflows/docker-build-publish.yml@main
with:
repo_name: ${{ needs.get-repo-info.outputs.repo_name }}
tag: ${{ needs.get-repo-info.outputs.tag }}
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}

# create a release on github
release:
name: Release
runs-on: ubuntu-latest
needs:
- docker-build
- get-repo-info
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Release
uses: softprops/action-gh-release@v1
with:
name: ${{ needs.get-repo-info.outputs.tag }}
tag_name: ${{ needs.get-repo-info.outputs.tag }}
body: |-
${{ github.event.repository.name }} ${{ needs.get-repo-info.outputs.tag }}
uses: divkix/reusable-workflows/.github/workflows/create-release.yml@main
with:
tag: ${{ needs.get-repo-info.outputs.tag }}
body: |-
${{ github.event.repository.name }} ${{ needs.get-repo-info.outputs.tag }}
Docker Images:
`ghcr.io/${{ github.repository_owner }}/${{ needs.get-repo-info.outputs.repo_name }}:${{ needs.get-repo-info.outputs.tag }}`
`docker.io/${{ github.repository_owner }}/${{ needs.get-repo-info.outputs.repo_name }}:${{ needs.get-repo-info.outputs.tag }}`
Docker Images:
`ghcr.io/${{ github.repository_owner }}/${{ needs.get-repo-info.outputs.repo_name }}:${{ needs.get-repo-info.outputs.tag }}`
`docker.io/${{ github.repository_owner }}/${{ needs.get-repo-info.outputs.repo_name }}:${{ needs.get-repo-info.outputs.tag }}`

0 comments on commit 36d2b54

Please sign in to comment.