Build and push #639
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 | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "49 5 * * *" | |
push: | |
branches: ["main"] | |
# Publish semver tags as releases. | |
tags: ["v*.*.*"] | |
pull_request: | |
branches: ["main"] | |
env: | |
# github.repository as <account>/<repo> | |
IMAGE_NAME: cyberchef | |
IMAGES: | | |
docker.io/obebete/cyberchef | |
ghcr.io/obeone/cyberchef | |
GIST_ID: 876a9aed55cffad6054360c99ecf4093 | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
services: | |
registry: | |
image: registry:2 | |
ports: | |
- 5000:5000 | |
permissions: | |
contents: write | |
packages: write | |
# This is used to complete the identity challenge | |
# with sigstore/fulcio when running outside of PRs. | |
id-token: write | |
steps: | |
- name: Install Cosign | |
uses: sigstore/cosign-installer@main | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
# Workaround: https://github.com/docker/build-push-action/issues/461 | |
- name: Setup Docker buildx | |
uses: docker/setup-buildx-action@v2 | |
with: | |
driver-opts: network=host | |
# Login against a Docker registry except on PR | |
# https://github.com/docker/login-action | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
registry: docker.io | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# Find latest tag of repository gchoi/cyberchef | |
- name: Fetch latest release (if not run by a tag push) | |
uses: gregziegan/fetch-latest-release@v2.0.0 | |
with: | |
github_token: ${{ github.token }} | |
repo_path: gchq/CyberChef | |
id: version | |
# If not from a tag push | |
if: github.event_name != 'push' || (github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/')) | |
- name: Use tag name as version if run on a tag push | |
run: | | |
TAG_NAME=$(basename ${GITHUB_REF##*/}) | |
echo ::set-output name=tag_name::${TAG_NAME} | |
if: github.event_name == 'push' || startsWith(github.ref, 'refs/tags/') | |
- name: Parse semver string | |
id: semver_parser | |
uses: booxmedialtd/ws-action-parse-semver@v1 | |
with: | |
input_string: "${{ steps.version.outputs.tag_name }}" | |
- name: Docker metadata | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
images: ${{ env.IMAGES }} | |
tags: | | |
type=raw,value=latest,enable=${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} | |
type=ref,event=branch | |
type=ref,event=pr | |
type=raw,value=${{ steps.semver_parser.outputs.major }},enable=${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} | |
type=raw,value=${{ steps.semver_parser.outputs.major }}.${{ steps.semver_parser.outputs.minor }},enable=${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} | |
type=raw,value=${{ steps.semver_parser.outputs.major }}.${{ steps.semver_parser.outputs.minor }}.${{ steps.semver_parser.outputs.patch }},enable=${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} | |
type=raw,value=v${{ steps.semver_parser.outputs.major }},enable=${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} | |
type=raw,value=v${{ steps.semver_parser.outputs.major }}.${{ steps.semver_parser.outputs.minor }},enable=${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} | |
type=raw,value=v${{ steps.semver_parser.outputs.major }}.${{ steps.semver_parser.outputs.minor }}.${{ steps.semver_parser.outputs.patch }},enable=${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} | |
# Build and push Docker image with Buildx (don't push on PR) | |
# https://github.com/docker/build-push-action | |
- name: Build CyberChef | |
id: build-base | |
uses: docker/build-push-action@v2 | |
with: | |
context: . | |
file: Dockerfile.build | |
build-args: | | |
GIT_TAG=${{ steps.version.outputs.tag_name }} | |
tags: | | |
localhost:5000/${{ env.IMAGE_NAME }}:build | |
labels: ${{ steps.meta.outputs.labels }} | |
push: true | |
cache-to: type=gha,mode=max | |
cache-from: type=gha | |
# Now we build multiarch packages with Dockerfile.build | |
- name: Build and push Docker image | |
id: build-and-push | |
uses: docker/build-push-action@v2 | |
with: | |
context: . | |
push: ${{ github.event_name != 'pull_request' }} | |
platforms: | | |
linux/amd64 | |
linux/arm64 | |
linux/i386 | |
linux/armhf | |
linux/armel | |
build-args: | | |
BASE_IMAGE=localhost:5000/${{ env.IMAGE_NAME }}:build | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
- name: Sign the images with GitHub OIDC Token | |
run: | | |
set -e | |
# In variables, "%%*( )" trim whitespace at beggining and end of variable | |
for image in ${IMAGES}; do | |
yes | cosign sign "${image%%*( )}@${DIGEST%%*( )}" | |
done | |
env: | |
TAGS: ${{ steps.meta.outputs.tags }} | |
DIGEST: ${{ steps.build-and-push.outputs.digest }} | |
COSIGN_EXPERIMENTAL: true | |
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
- name: Find last tag | |
uses: oprypin/find-latest-tag@v1 | |
with: | |
repository: ${{ github.repository }} | |
id: latest_tag | |
- name: Check if tag already exists | |
uses: mukunku/tag-exists-action@v1.0.0 | |
id: checkTag | |
with: | |
tag: ${{ steps.version.outputs.tag_name }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- if: steps.checkTag.outputs.exists == 'false' && github.event_name != 'pull_request' | |
name: Create/update tag ${{ steps.version.outputs.tag_name }} | |
uses: rickstaa/action-create-tag@v1 | |
with: | |
tag: ${{ steps.version.outputs.tag_name }} | |
message: Version ${{ steps.version.outputs.tag_name }} | |
- if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
name: Getting current date | |
id: date | |
run: echo "current_date=$(date +%Y-%m-%d)" >> $GITHUB_OUTPUT | |
- if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
name: Create date badge | |
uses: schneegans/dynamic-badges-action@v1.7.0 | |
with: | |
auth: ${{ secrets.GIST_SECRET }} | |
gistID: ${{ env.GIST_ID }} | |
filename: date.json | |
label: build date | |
message: ${{ steps.date.outputs.current_date }} | |
color: green |