Build Container #225
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 Container | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '36 4 * * 1' | |
push: | |
branches: [ master ] | |
pull_request: | |
branches: [ master ] | |
env: | |
# Use docker.io for Docker Hub if empty | |
REGISTRY1: ghcr.io | |
REGISTRY2: docker.io | |
# ${{ github.repository }} as <account>/<repo> | |
IMAGE_NAME1: greyltc/archlinux | |
IMAGE_NAME2: greyltc/archlinux | |
TEST_TAG: greyltc/archlinux:test | |
jobs: | |
job0: | |
name: Build Docker Context | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v2 | |
- name: Install deps | |
run: | | |
sudo apt-get install -y fakeroot fakechroot zstd | |
- name: Build root tarball(s) | |
run: | | |
set -o pipefail | |
mkdir -p out | |
bash build-root.sh x86_64 out |& tee out/archlinux-x86_64.build.log.txt | |
#ARCH=aarch64 bash -c './build-root.sh ${ARCH} out |& tee out/archlinux-${ARCH}.build.log.txt && mv -t out/${ARCH} *.tar.xz' | |
- name: Cache root tarball(s) | |
uses: actions/cache@v2 | |
with: | |
path: out | |
key: ${{ runner.os }}-${{ github.run_number }}-${{ hashFiles('*.build.log.txt') }} | |
job1: | |
name: Build Docker Container | |
needs: job0 | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: write | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v2 | |
# with: | |
# token: ${{ secrets.PAT }} | |
# - name: Set up QEMU | |
# id: qemu | |
# uses: docker/setup-qemu-action@v1 | |
# with: | |
# image: tonistiigi/binfmt:latest | |
# platforms: linux/amd64 | |
# platforms: all | |
# - name: Available platforms | |
# run: echo ${{ steps.qemu.outputs.platforms }} | |
- name: Load root tarball(s) | |
uses: actions/cache@v2 | |
with: | |
path: out | |
key: ${{ runner.os }}-${{ github.run_number }}-${{ hashFiles('*.build.log.txt') }} | |
- name: Get current date | |
id: date | |
run: echo "TODAY_IS=$(date +'%Y%m%d')" >> $GITHUB_ENV | |
- name: Generate version string | |
id: versionstring | |
run: echo "THIS_VERSTRING=${{ env.TODAY_IS }}.0.${{ github.run_number }}" >> $GITHUB_ENV | |
# Login against a Docker registry except on PR | |
# https://github.com/docker/login-action | |
- name: Log into registry1 | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v1 | |
with: | |
registry: ${{ env.REGISTRY1 }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# Login against a Docker registry except on PR | |
# https://github.com/docker/login-action | |
- name: Log into registry2 | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v1 | |
with: | |
registry: ${{ env.REGISTRY2 }} | |
username: greyltc | |
password: ${{ secrets.DHT }} | |
# Extract metadata (tags, labels) for Docker | |
# https://github.com/docker/metadata-action | |
- name: Extract Docker metadata1 | |
id: meta1 | |
uses: docker/metadata-action@v3 | |
with: | |
tags: | | |
type=raw,value=${{ env.THIS_VERSTRING }} | |
flavor: | | |
latest=true | |
images: ${{ env.REGISTRY1 }}/${{ env.IMAGE_NAME1 }} | |
# Extract metadata (tags, labels) for Docker | |
# https://github.com/docker/metadata-action | |
- name: Extract Docker metadata2 | |
id: meta2 | |
uses: docker/metadata-action@v3 | |
with: | |
tags: | | |
type=raw,value=${{ env.THIS_VERSTRING }} | |
flavor: | | |
latest=true | |
images: ${{ env.REGISTRY2 }}/${{ env.IMAGE_NAME2 }} | |
- name: Docker build | |
uses: docker/build-push-action@v2 | |
with: | |
context: out/x86_64/ | |
load: true | |
tags: ${{ env.TEST_TAG }} | |
- name: Test the container | |
run: | | |
docker run --rm ${{ env.TEST_TAG }} cat /usr/lib/os-release | |
# Push Docker image with Buildx (don't push on PR) | |
# https://github.com/docker/build-push-action | |
- name: Push to registry1 | |
uses: docker/build-push-action@v2 | |
with: | |
context: out/x86_64/ | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta1.outputs.tags }} | |
labels: ${{ steps.meta1.outputs.labels }} | |
# Push Docker image with Buildx (don't push on PR) | |
# https://github.com/docker/build-push-action | |
- name: Push to registry2 | |
uses: docker/build-push-action@v2 | |
with: | |
context: out/x86_64/ | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta2.outputs.tags }} | |
labels: ${{ steps.meta2.outputs.labels }} | |
- name: Commit with tag | |
if: github.event_name != 'pull_request' | |
run: | | |
git config --global user.name 'GitHub Actions Robot' | |
git config --global user.email '[email protected]' | |
mv out/*.build.log.txt . | |
git add *.build.log.txt | |
sed "s|docker build --tag arch-localbuild https.*|docker build --tag arch-localbuild ${{ github.server_url }}/${{ github.repository }}/releases/download/v${{ env.THIS_VERSTRING }}/docker-archlinux-x86_64.tar.xz|g" --in-place README.md | |
git add README.md | |
git commit -m "version ${{ env.THIS_VERSTRING }} root tarball(s) rebuilt" | |
git tag -a "v${{ env.THIS_VERSTRING }}" -m "rebuilt root tarball(s)" | |
git push -u origin master --tags | |
# https://docs.github.com/en/rest/reference/releases#create-a-release | |
- name: Release | |
if: github.event_name != 'pull_request' | |
run: | | |
ASSET_FILE="${{ github.workspace }}/docker-archlinux-x86_64.tar.xz" | |
XZ_OPT="-9e --threads=0" tar --create --file "${ASSET_FILE}" --xz --directory "${{ github.workspace }}"/out/x86_64 . | |
curl --silent \ | |
--url https://api.github.com/repos/${{ github.repository }}/releases \ | |
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \ | |
--header "Accept: application/vnd.github.v3+json" \ | |
--data '{"draft":false,"tag_name":"v${{ env.THIS_VERSTRING }}","name":"Version ${{ env.THIS_VERSTRING }}"}' \ | |
--output rel_resp.json | |
echo "Release Done." | |
ASSET_UL_URL=$(cat rel_resp.json | jq --raw-output '.upload_url' | sed "s|{?.*||g") | |
# upload asset | |
echo "Uploading asset..." | |
ASSET_UL_URL="${ASSET_UL_URL}?name=asset" | |
curl --silent \ | |
--url "${ASSET_UL_URL}" \ | |
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \ | |
--header "Accept: application/vnd.github.v3+json" \ | |
--header "Content-Type: $(file --brief --mime-type ${ASSET_FILE})" \ | |
--data-binary @${ASSET_FILE} \ | |
--output asset_resp.json | |
ASSET_URL=$(cat asset_resp.json | jq --raw-output '.url') | |
echo "Asset upload done." | |
# update asset | |
ASSET_NAME="$(basename ${ASSET_FILE})" | |
ASSET_LABEL="x86_64 Docker Build Context" | |
echo "Updating asset..." | |
jq -n --arg arg_name "${ASSET_NAME}" --arg arg_label "${ASSET_LABEL}" '{"name":$arg_name,"label":$arg_label}' | curl --silent \ | |
--request PATCH \ | |
--url "${ASSET_URL}" \ | |
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \ | |
--header "Accept: application/vnd.github.v3+json" \ | |
--data @- \ | |
--output asset_update.json | |
echo "Asset update done." | |
if test "$(cat asset_update.json | jq --raw-output '.name')"x != "${ASSET_NAME}"x | |
then | |
echo "Could not verify asset update" | |
exit -1 | |
fi | |