ci(update-readme): fix sed syntax issues #104
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: Docker build and publish | |
on: | |
push: | |
paths-ignore: | |
- "*.md" | |
- "**/*.md" | |
# Publish `main` as Docker `edge` image. | |
branches: | |
- main | |
- develop | |
# Run tests for any PRs. | |
pull_request: | |
env: | |
IMAGE_NAME: ps3netsrv | |
IMAGE_TITLE: ps3netsrv | |
IMAGE_DESCRIPTION: Docker container for ps3netsrv | |
IMAGE_LICENSE: gpl-3.0-or-later | |
jobs: | |
build-matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
releases: ${{ steps.set-matrix.outputs.releases }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Get latest five ps3netsrv versions | |
id: set-matrix | |
run: | | |
version_commit_map=$(jq -cr --arg gitref ${{ github.ref }} -f .github/ps3netsrv-filter.jq .github/ps3netsrv-history.json) | |
echo "releases=${version_commit_map}" >> "$GITHUB_OUTPUT" | |
echo "version_commit_map=${version_commit_map}" | |
# Run tests build | |
test-build: | |
needs: build-matrix | |
strategy: | |
matrix: | |
release: ${{ fromJSON(needs.build-matrix.outputs.releases) }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set up Docker Buildx | |
id: buildx_test | |
uses: docker/setup-buildx-action@v3 | |
- name: Cache Docker layers | |
uses: actions/cache@v4 | |
with: | |
path: /tmp/.test-buildx-cache | |
key: ${{ runner.os }}-test-buildx-${{ matrix.release.version }}-${{ github.ref }} | |
restore-keys: | | |
${{ runner.os }}-test-buildx-${{ matrix.release.version }}-${{ github.ref }} | |
${{ runner.os }}-test-buildx-${{ github.ref }} | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Test tag splitting | |
env: | |
PS3NETSRV_VERSION: ${{ matrix.release.version }} | |
PS3NETSRV_SRC_REF: ${{ matrix.release.ref }} | |
PS3NETSRV_COMMITS: ${{ join(matrix.release.commits, ',') }} | |
PS3NETSRV_TAGS: ${{ join(matrix.release.tags, ',') }} | |
run: | | |
DOCKERIO_IMAGE=docker.io/${{ github.repository_owner }}/$IMAGE_NAME | |
GHCRIO_IMAGE=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME | |
# Split PS3NETSRV_TAGS by commata | |
IFS=, read -r -a TAGS <<< "$PS3NETSRV_TAGS" | |
echo "Would create tags:" | |
for TAG in "${TAGS[@]}"; do | |
DOCKERIO_TAGS="${DOCKERIO_TAGS},${DOCKERIO_IMAGE}:${TAG}" | |
GHCRIO_TAGS="${GHCRIO_TAGS},${GHCRIO_IMAGE}:${TAG}" | |
done | |
echo "DOCKERIO_TAGS=${DOCKERIO_TAGS:-}" | |
echo "GHCRIO_TAGS=${GHCRIO_TAGS:-}" | |
- name: Test build | |
uses: docker/build-push-action@v6 | |
with: | |
builder: ${{ steps.buildx_test.outputs.name }} | |
context: . | |
platforms: linux/amd64 | |
push: false | |
load: true | |
build-args: | | |
BUILD_FROM_GIT=true | |
PS3NETSRV_VERSION=${{ matrix.release.version }} | |
PS3NETSRV_SRC_REF=${{ matrix.release.ref }} | |
tags: ps3netsrv:test-build-${{ matrix.release.version }} | |
cache-from: type=local,src=/tmp/.test-buildx-cache | |
cache-to: type=local,dest=/tmp/.test-buildx-cache-new | |
# Temp fix | |
# https://github.com/docker/build-push-action/issues/252 | |
# https://github.com/moby/buildkit/issues/1896 | |
- name: Move cache | |
run: | | |
rm -rf /tmp/.test-buildx-cache | |
mv /tmp/.test-buildx-cache-new /tmp/.test-buildx-cache | |
- name: Create ps3netsrv container... | |
run: | | |
echo "Starting ps3netsrv container..." | |
docker run -t -d --name ps3netsrv-test ps3netsrv:test-build-${{ matrix.release.version }} | |
echo "Trying 30 seconds to check for startup..." | |
count=0 | |
exitcode=1 | |
while [ $count -lt 30 ]; do | |
if docker logs ps3netsrv-test | grep "ps3netsrv build ${{ matrix.release.version }}" >/dev/null; then | |
docker logs ps3netsrv-test | |
echo "Success!" | |
exitcode=0 | |
break | |
else | |
echo "Waiting for ps3netsrv to start..." | |
exitcode=1 | |
fi | |
sleep 1 | |
count=$((count + 1)) | |
done | |
docker stop ps3netsrv-test >/dev/null | |
exit "$exitcode" | |
# Build images. | |
build: | |
if: github.event_name != 'pull_request' | |
runs-on: ubuntu-latest | |
outputs: | |
publish: ${{ steps.prep.outputs.publish }} | |
readme_tags: ${{ steps.prep.outputs.readme_tags }} | |
needs: [build-matrix, test-build] | |
strategy: | |
matrix: | |
release: ${{ fromJSON(needs.build-matrix.outputs.releases) }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.ref }} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
with: | |
platforms: all | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to DockerHub | |
uses: docker/login-action@v3 | |
with: | |
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: ${{ secrets.GITHUB_TOKEN }} | |
- name: Prepare variables | |
id: prep | |
env: | |
PS3NETSRV_VERSION: ${{ matrix.release.version }} | |
PS3NETSRV_SRC_REF: ${{ matrix.release.ref }} | |
PS3NETSRV_COMMITS: ${{ join(matrix.release.commits, ',') }} | |
PS3NETSRV_TAGS: ${{ join(matrix.release.tags, ',') }} | |
run: | | |
DOCKERIO_IMAGE=docker.io/${{ github.repository_owner }}/$IMAGE_NAME | |
GHCRIO_IMAGE=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME | |
# Split PS3NETSRV_TAGS by commata | |
IFS=, read -r -a TAGS <<< "$PS3NETSRV_TAGS" | |
for TAG in "${TAGS[@]}"; do | |
DOCKERIO_TAGS="${DOCKERIO_TAGS},${DOCKERIO_IMAGE}:${TAG}" | |
GHCRIO_TAGS="${GHCRIO_TAGS},${GHCRIO_IMAGE}:${TAG}" | |
README_TAGS="${README_TAGS}, \`${TAG}\`" | |
done | |
echo "version=${PS3NETSRV_VERSION}" >> $GITHUB_OUTPUT | |
echo "tags=${DOCKERIO_TAGS},${GHCRIO_TAGS}" >> $GITHUB_OUTPUT | |
echo "readme_tags=${README_TAGS}" >> $GITHUB_OUTPUT | |
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT | |
echo "publish=${{ github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags') }}" >> $GITHUB_OUTPUT | |
- name: Cache Docker layers | |
uses: actions/cache@v4 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ matrix.release.version }} | |
restore-keys: | | |
${{ runner.os }}-buildx-${{ matrix.release.version }} | |
${{ runner.os }}-buildx- | |
- name: Build and push | |
id: docker_build | |
uses: docker/build-push-action@v6 | |
with: | |
builder: ${{ steps.buildx.outputs.name }} | |
context: . | |
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 | |
push: ${{ steps.prep.outputs.publish == 'true' }} | |
tags: ${{ steps.prep.outputs.tags }} | |
build-args: | | |
BUILD_FROM_GIT=true | |
PS3NETSRV_VERSION=${{ matrix.release.version }} | |
PS3NETSRV_SRC_REF=${{ matrix.release.ref }} | |
labels: | | |
org.opencontainers.image.title=${{ env.IMAGE_TITLE }} | |
org.opencontainers.image.description=${{ env.IMAGE_DESCRIPTION }} | |
org.opencontainers.image.vendor=${{ github.repository_owner }} | |
org.opencontainers.image.url=${{ github.event.repository.html_url }} | |
org.opencontainers.image.source=${{ github.event.repository.html_url }} | |
org.opencontainers.image.created=${{ steps.prep.outputs.created }} | |
org.opencontainers.image.revision=${{ matrix.release.ref }} | |
org.opencontainers.image.version=${{ matrix.release.version }} | |
org.opencontainers.image.licenses=${{ env.IMAGE_LICENSE }} | |
annotations: | | |
org.opencontainers.image.title=${{ env.IMAGE_TITLE }} | |
org.opencontainers.image.description=${{ env.IMAGE_DESCRIPTION }} | |
org.opencontainers.image.vendor=${{ github.repository_owner }} | |
org.opencontainers.image.url=${{ github.event.repository.html_url }} | |
org.opencontainers.image.source=${{ github.event.repository.html_url }} | |
org.opencontainers.image.created=${{ steps.prep.outputs.created }} | |
org.opencontainers.image.revision=${{ matrix.release.ref }} | |
org.opencontainers.image.version=${{ matrix.release.version }} | |
org.opencontainers.image.licenses=${{ env.IMAGE_LICENSE }} | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,dest=/tmp/.buildx-cache-new | |
# Temp fix | |
# https://github.com/docker/build-push-action/issues/252 | |
# https://github.com/moby/buildkit/issues/1896 | |
- name: Move cache | |
run: | | |
rm -rf /tmp/.buildx-cache | |
mv /tmp/.buildx-cache-new /tmp/.buildx-cache | |
update-readme: | |
if: github.event_name != 'pull_request' | |
runs-on: ubuntu-latest | |
needs: [build-matrix, build] | |
steps: | |
- name: Checkout main | |
if: needs.build.outputs.publish == 'true' | |
uses: actions/checkout@v4 | |
with: | |
ref: main | |
- name: Set start and end comment | |
if: needs.build.outputs.publish == 'true' | |
id: comment | |
run: | | |
# Replace supported tags in README.md | |
# Define the start and end comments in the README.md file | |
if [ "${{ github.ref }}" != "refs/heads/main" ]; then | |
echo 'start_comment=<!-- edge tags start -->' >> "$GITHUB_OUTPUT" | |
echo 'end_comment=<!-- edge tags end -->' >> "$GITHUB_OUTPUT" | |
else | |
echo 'start_comment=<!-- latest tags start -->' >> "$GITHUB_OUTPUT" | |
echo 'end_comment=<!-- latest tags end -->' >> "$GITHUB_OUTPUT" | |
fi | |
- name: Prepare variables | |
if: needs.build.outputs.publish == 'true' | |
id: prep | |
run: | | |
README_TAGS=$(jq -r '"- " + (.[] | [.tags[] | "`" + . + "`"] | join(", "))' <<< '${{ needs.build-matrix.outputs.releases }}') | |
# Cleanout space between start and end comment | |
sed -i '/${{ steps.comment.outputs.start_comment }}/,/${{ steps.comment.outputs.end_comment }}/ { //!d }' README.md | |
# Replace the tags | |
sed -i "\\~${{ steps.comment.outputs.start_comment }}~,\\~${{ steps.comment.outputs.end_comment }}~ { \\|${{ steps.comment.outputs.start_comment }}|r /dev/stdin | |
}" README.md <<< "$README_TAGS" | |
- name: Check for modified files | |
if: needs.build.outputs.publish == 'true' | |
id: git-check | |
run: echo modified=$([ -z "`git status --porcelain`" ] && echo "false" || echo "true") >> $GITHUB_OUTPUT | |
- name: Commit updated README.md | |
if: steps.git-check.outputs.modified == 'true' | |
run: | | |
git config user.name 'github-actions[bot]' | |
git config user.email 'github-actions[bot]@users.noreply.github.com' | |
git add README.md | |
git commit -m "docs(README): update supported tags" | |
git push |