From 6f4938aa75ae77c4dd2825237e7251d0b3b264f2 Mon Sep 17 00:00:00 2001
From: CrazyMax <crazy-max@users.noreply.github.com>
Date: Fri, 21 May 2021 17:05:16 +0200
Subject: [PATCH] Share built image between jobs

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
---
 README.md                         |  2 ++
 docs/advanced/share-image-jobs.md | 58 +++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 docs/advanced/share-image-jobs.md

diff --git a/README.md b/README.md
index 6a1dfa3..96fac42 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,7 @@ ___
     * [GitHub cache](docs/advanced/cache.md#github-cache)
   * [Local registry](docs/advanced/local-registry.md)
   * [Export image to Docker](docs/advanced/export-docker.md)
+  * [Share built image between jobs](docs/advanced/share-image-jobs.md)
   * [Handle tags and labels](docs/advanced/tags-labels.md)
   * [Update DockerHub repo description](docs/advanced/dockerhub-desc.md)
 * [Customizing](#customizing)
@@ -171,6 +172,7 @@ jobs:
   * [GitHub cache](docs/advanced/cache.md#github-cache)
 * [Local registry](docs/advanced/local-registry.md)
 * [Export image to Docker](docs/advanced/export-docker.md)
+* [Share built image between jobs](docs/advanced/share-image-jobs.md)
 * [Handle tags and labels](docs/advanced/tags-labels.md)
 * [Update DockerHub repo description](docs/advanced/dockerhub-desc.md)
 
diff --git a/docs/advanced/share-image-jobs.md b/docs/advanced/share-image-jobs.md
new file mode 100644
index 0000000..3421bd6
--- /dev/null
+++ b/docs/advanced/share-image-jobs.md
@@ -0,0 +1,58 @@
+# Share built image between jobs
+
+As each job is isolated in its own runner you cannot use your built image between jobs (except for [self-hosted runners](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners)).
+However, you can [pass data between jobs in a workflow](https://docs.github.com/en/actions/guides/storing-workflow-data-as-artifacts#passing-data-between-jobs-in-a-workflow)
+using the [actions/upload-artifact](https://github.com/actions/upload-artifact) and [actions/download-artifact](https://github.com/actions/download-artifact)
+actions:
+
+```yaml
+name: ci
+
+on:
+  push:
+    branches:
+      - 'master'
+
+jobs:
+  build:
+    runs-on: ubuntu-latest
+    steps:
+      -
+        name: Checkout
+        uses: actions/checkout@v2
+      -
+        name: Set up Docker Buildx
+        uses: docker/setup-buildx-action@v1
+      -
+        name: Build and export
+        uses: docker/build-push-action@v2
+        with:
+          context: .
+          tags: myimage:latest
+          outputs: type=docker,dest=/tmp/myimage.tar
+      -
+        name: Upload artifact
+        uses: actions/upload-artifact@v2
+        with:
+          name: myimage
+          path: /tmp/myimage.tar
+
+  use:
+    runs-on: ubuntu-latest
+    needs: build
+    steps:
+      -
+        name: Set up Docker Buildx
+        uses: docker/setup-buildx-action@v1
+      -
+        name: Download artifact
+        uses: actions/download-artifact@v2
+        with:
+          name: myimage
+          path: /tmp
+      -
+        name: Load image
+        run: |
+          docker load --input /tmp/myimage.tar
+          docker image ls -a
+```