This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and push docker image | |
on: | |
push: | |
paths: | |
- "**.go" | |
- "go.mod" | |
- "go.sum" | |
- "ui/**" | |
- "Dockerfile" | |
workflow_dispatch: | |
jobs: | |
# Job 1: Checkout code and set up common environment | |
setup: | |
runs-on: ubuntu-latest | |
outputs: | |
ref_name: ${{ steps.get_ref.outputs.ref_name }} | |
steps: | |
- name: Checkout code | |
id: get_ref | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Get branch name | |
id: ref | |
run: echo "ref_name=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV | |
# Job 2: Build the Docker image | |
build: | |
needs: setup | |
runs-on: ubuntu-latest | |
steps: | |
- name: Build docker image | |
uses: docker/build-push-action@v6 | |
with: | |
load: true | |
# Job 3: Run Semantic Release | |
semantic-release: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Semantic Release | |
uses: cycjimmy/semantic-release-action@v4 | |
id: release | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# Job 4: Login to Docker registries | |
docker-login: | |
needs: semantic-release | |
if: ${{ github.ref_name == 'main' && needs.semantic-release.outputs.new_release_published == 'true' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Login to Docker registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ vars.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
# Job 5: Push Docker image | |
push-image: | |
needs: [semantic-release, docker-login] | |
if: ${{ github.ref_name == 'main' && needs.semantic-release.outputs.new_release_published == 'true' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Push docker image | |
uses: docker/build-push-action@v6 | |
with: | |
push: true | |
tags: | | |
${{ vars.DOCKERHUB_USERNAME }}/swarm-cd:latest | |
${{ vars.DOCKERHUB_USERNAME }}/swarm-cd:${{ needs.semantic-release.outputs.new_release_version }} | |
ghcr.io/${{ github.repository }}:latest | |
ghcr.io/${{ github.repository }}:${{ needs.semantic-release.outputs.new_release_version }} | |
# Job 6: Update Docker Hub repo description | |
update-dockerhub-description: | |
needs: push-image | |
if: ${{ github.ref_name == 'main' && needs.semantic-release.outputs.new_release_published == 'true' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Update Docker Hub repo description | |
uses: peter-evans/dockerhub-description@v4.0.0 | |
with: | |
username: ${{ vars.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
repository: ${{ vars.DOCKERHUB_USERNAME }}/swarm-cd |