Publish Docker Image #174
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: Publish Docker Image | |
on: | |
workflow_dispatch: | |
inputs: | |
version: | |
description: "Serverless Version" | |
default: "latest" | |
required: false | |
schedule: | |
- cron: '0 0 * * 1' # At 00:00 on Monday. | |
env: | |
DOCKER_CLI_EXPERIMENTAL: enabled | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
timeout-minutes: 240 | |
permissions: | |
packages: write | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: 'main' | |
fetch-depth: 0 | |
- name: Setup Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: lts/* | |
- name: Install Dependencies | |
run: | | |
sudo apt-get install jq | |
- name: Get Latest Serverless Version | |
id: sls | |
run: | | |
required_version="${{ github.event.inputs.version }}" | |
version="$(npm view serverless version)" | |
if [[ -n "$required_version" ]]; then | |
echo "Required version: $required_version" | |
echo "version=$required_version" >> "$GITHUB_OUTPUT" | |
else | |
echo "Computed version: $version" | |
echo "version=$version" >> "$GITHUB_OUTPUT" | |
fi | |
- name: Get Latest Published Docker Version | |
id: docker | |
run: | | |
image_repository="$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]' | sed 's/docker-//')" | |
echo "$image_repository" | |
echo "image_repository=$image_repository" >> "$GITHUB_OUTPUT" | |
latest_published_tag="$(curl -L -s "https://hub.docker.com/v2/repositories/$image_repository/tags/?page_size=2&page=1&ordering=last_updated" | jq '.results | .[1] | .name' | sed 's/"//g')" | |
echo "$latest_published_tag" | |
echo "latest_published_tag=$latest_published_tag" >> "$GITHUB_OUTPUT" | |
- name: Docker metadata | |
if: ${{ steps.docker.outputs.latest_published_tag != steps.sls.outputs.version }} | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: | | |
${{ steps.docker.outputs.image_repository }} | |
ghcr.io/${{ steps.docker.outputs.image_repository }} | |
# public.ecr.aws/${{ steps.docker.outputs.image_repository }} | |
labels: | | |
org.opencontainers.image.authors=${{ github.repository_owner }} | |
tags: | | |
type=raw,value=${{ steps.sls.outputs.version }} | |
flavor: | | |
latest=true | |
- name: Set up QEMU | |
if: ${{ steps.docker.outputs.latest_published_tag != steps.sls.outputs.version }} | |
id: qemu | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
if: ${{ steps.docker.outputs.latest_published_tag != steps.sls.outputs.version }} | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
with: | |
install: true | |
driver-opts: image=moby/buildkit:master | |
- name: Login to DockerHub | |
if: ${{ steps.docker.outputs.latest_published_tag != steps.sls.outputs.version }} | |
id: login-dockerhub | |
uses: docker/login-action@v3 | |
with: | |
logout: false | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ github.token }} | |
# - name: Login to Public ECR | |
# uses: docker/login-action@v3 | |
# with: | |
# registry: public.ecr.aws | |
# username: ${{ secrets.AWS_ECR_ACCESS_KEY_ID }} | |
# password: ${{ secrets.AWS_ECR_SECRET_ACCESS_KEY }} | |
# env: | |
# AWS_REGION: us-east-1 | |
- name: Build and push Docker images | |
if: ${{ steps.docker.outputs.latest_published_tag != steps.sls.outputs.version }} | |
id: build-and-push | |
uses: docker/build-push-action@v5 | |
with: | |
push: ${{ github.event_name != 'pull_request' }} | |
context: . | |
# platforms: linux/amd64,linux/arm64/v8,linux/arm/v7,linux/arm/v6,linux/ppc64le,linux/s390x | |
platforms: linux/amd64,linux/arm64 | |
build-args: SERVERLESS_VERSION=${{ steps.sls.outputs.version }} | |
labels: ${{ steps.meta.outputs.labels }} | |
tags: ${{ steps.meta.outputs.tags }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |