Skip to content

Docker

Docker #684

Workflow file for this run

name: Docker
on:
push:
branches:
- docker**
# https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onschedule
schedule:
- cron: "0 3 * * 1-5" # every work night (delayed 1 hr)
repository_dispatch:
types:
- all
# Trigger with `shinycoreci::trigger_docker()`
- docker
jobs:
config:
uses: ./.github/workflows/apps-config.yml
docker:
needs: config
name: ${{ matrix.docker.type }}-${{ matrix.r_version }}${{ matrix.flavor.extra_base_tag }}
runs-on: ubuntu-latest
strategy:
max-parallel: 5
fail-fast: false
matrix:
flavor:
# - shinycoreci_sha: main
- shinycoreci_sha: main
extra_base_tag: ""
# - shinycoreci_sha: shiny-1.4.0.1
# extra_base_tag: "-rc_v1.4.0.1"
r_version:
- ${{ needs.config.outputs.release }}
- ${{ needs.config.outputs.oldrel1 }}
- ${{ needs.config.outputs.oldrel2 }}
- ${{ needs.config.outputs.oldrel3 }}
- ${{ needs.config.outputs.oldrel4 }}
docker:
# - workdir: inst/Docker/centos
# type: centos7
- workdir: inst/Docker/ubuntu
type: jammy
- workdir: inst/Docker/ubuntu
type: focal
steps:
- name: Short R version and SHA
id: short
run: |
# keep only the major.minor values
RVERSION=`echo '${{ matrix.r_version }}' | sed 's/\([0-9]\.[0-9]\).*/\1/'`
echo "r-version: $RVERSION"
echo "r-version=$RVERSION" >> $GITHUB_OUTPUT
SHA="${{github.event.pull_request.head.sha}}${{ github.sha }}"
if [[ -z "$SHA" ]]; then
SHA=${{ github.sha }}
fi
SHORT_SHA=${SHA:0:7}
echo "sha: $SHORT_SHA"
echo "sha=$SHORT_SHA" >> $GITHUB_OUTPUT
- uses: actions/checkout@v4
if: steps.can-build.conclusion != 'skipped'
- name: Log in to the Container registry
if: steps.can-build.conclusion != 'skipped'
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build Base
if: steps.can-build.conclusion != 'skipped'
uses: docker/build-push-action@v3
with:
context: ${{ matrix.docker.workdir }}
push: true
tags: |
ghcr.io/${{ github.repository }}:base-${{ steps.short.outputs.r-version }}-${{ matrix.docker.type }}${{ matrix.flavor.extra_base_tag }}
build-args: |
R_VERSION=${{ steps.short.outputs.r-version }}
RELEASE=${{ matrix.docker.type }}
SHINYCORECI_SHA=${{ matrix.flavor.shinycoreci_sha }}
GITHUB_PAT=${{ secrets.GITHUB_TOKEN }}
- name: Build SSO
if: steps.can-build.conclusion != 'skipped'
uses: docker/build-push-action@v2
with:
context: "${{ matrix.docker.workdir }}_sso"
push: true
tags: |
ghcr.io/${{ github.repository }}:sso-${{ steps.short.outputs.r-version }}-${{ matrix.docker.type }}${{ matrix.flavor.extra_base_tag }}
build-args: |
R_VERSION=${{ steps.short.outputs.r-version }}
RELEASE=${{ matrix.docker.type }}
EXTRA_BASE_TAG=${{ matrix.flavor.extra_base_tag }}
- name: Build SSP
if: steps.can-build.conclusion != 'skipped'
uses: docker/build-push-action@v2
with:
context: "${{ matrix.docker.workdir }}_ssp"
push: true
tags: |
ghcr.io/${{ github.repository }}:ssp-${{ steps.short.outputs.r-version }}-${{ matrix.docker.type }}${{ matrix.flavor.extra_base_tag }}
build-args: |
R_VERSION=${{ steps.short.outputs.r-version }}
RELEASE=${{ matrix.docker.type }}
EXTRA_BASE_TAG=${{ matrix.flavor.extra_base_tag }}
docker-cleanup:
if: success() || failure()
needs: [docker]
runs-on: ubuntu-latest
name: Cleanup images
steps:
# Inspiration: https://github.com/jcansdale-test/ghcr-delete-untagged-images/blob/36e56b47bfb6ee9208b4061959f6d0c615e3c5cc/.github/workflows/delete.yml
- name: Delete untagged images
uses: actions/github-script@v6
with:
github-token: ${{ secrets.DOCKER_DELETE_IMAGES }}
script: |
// const response_list = await github.request("GET /orgs/rstudio/packages?package_type=container&visibility=public");
// console.log(JSON.stringify(response_list, null, " "))
let found = true
// Don't try forever if something goes wrong
let retryCount = 0
let n = 100
n = Math.min(100, n) // API has max of 100
while (found && retryCount < 10) {
found = false
retryCount += 1
// GET /orgs/{org}/packages/{package_type}/{package_name}/versions
const response = await github.request("GET /orgs/rstudio/packages/container/shinycoreci/versions",
{ per_page: n }
);
// console.log(JSON.stringify(response, null, " "))
console.log("\n\nRequest count: ", retryCount, "\nVersion count: ", response.data.length, "\n")
for(version of response.data) {
if (version.metadata.container.tags.length == 0) {
console.log("delete " + version.id)
// DELETE /orgs/{org}/packages/{package_type}/{package_name}/versions/{package_version_id}
const deleteResponse = await github.request("DELETE /orgs/rstudio/packages/container/shinycoreci/versions/" + version.id, { });
console.log("status " + deleteResponse.status)
if (deleteResponse.status == 204) {
// We have less than `n` tags
// If `n` versions are returned, then try again
found = response.data.length == n
}
}
}
}