Add GitHub Actions workflow for building, testing, and scanning Go anโฆ #2
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 Images | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
workflow_dispatch: | |
permissions: | |
contents: read | |
packages: write | |
jobs: | |
build-backend: | |
name: ๐ ๏ธ Build and Push | |
runs-on: ubuntu-latest | |
steps: | |
- name: ๐ Checkout Code | |
uses: actions/checkout@v4 | |
# Get version from version.yaml | |
- name: ๐ Get version | |
run: | | |
echo "VERSION=$(cat version.yaml | sed -n 's/version: //p')" >> $GITHUB_ENV | |
echo $VERSION | |
# Set build tags based on branch | |
- name: ๐๏ธ Set Build tags | |
run: | | |
if [ $GITHUB_REF = 'refs/heads/main' ]; then | |
echo "IMAGE_TAGS=$VERSION" >> $GITHUB_ENV | |
else | |
echo "IMAGE_TAGS=rc-$VERSION-$GITHUB_RUN_NUMBER" >> $GITHUB_ENV | |
fi | |
- name: ๐ ๏ธ Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
# Login to GitHub Container Registry | |
- name: ๐ Login to GitHub Container Registry | |
uses: docker/#-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.TOKEN }} | |
# Login to Docker Hub | |
- name: ๐ Login to Docker Hub | |
if: github.ref == 'refs/heads/main' | |
uses: docker/#-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: ๐ฆ Build and Push Image to GHCR (PR) | |
if: github.event_name == 'pull_request' | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ghcr.io/kek-sec/goshort:${{ env.IMAGE_TAGS }} | |
annotations: | | |
org.opencontainers.image.description=goshort, url shortener | |
build-args: | | |
VERSION=${{ env.VERSION }} | |
labels: | | |
org.opencontainers.image.source=https://github.com/kek-sec/goshort | |
cache-from: type=registry,ref=ghcr.io/kek-sec/goshort:latest | |
cache-to: type=registry,ref=ghcr.io/kek-sec/goshort:latest | |
- name: ๐ฆ Build and Push Image to GHCR and Docker Hub (Main) | |
if: github.ref == 'refs/heads/main' | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: | | |
ghcr.io/kek-sec/goshort:${{ env.IMAGE_TAGS }} | |
petrakisg/goshort:${{ env.IMAGE_TAGS }} | |
annotations: | | |
org.opencontainers.image.description=goshort, a secure one-time secret sharing service | |
build-args: | | |
VERSION=${{ env.VERSION }} | |
labels: | | |
org.opencontainers.image.source=https://github.com/kek-sec/goshort | |
cache-from: type=registry,ref=ghcr.io/kek-sec/goshort:latest | |
cache-to: type=registry,ref=ghcr.io/kek-sec/goshort:latest |