1+ name : Build Container Image & Test
2+
3+ # This workflow uses actions that are not certified by GitHub.
4+ # They are provided by a third-party and are governed by
5+ # separate terms of service, privacy policy, and support
6+ # documentation.
7+
8+ on :
9+ push :
10+ # Publish semver tags as releases.
11+ tags : ["v*.*.*"]
12+ paths-ignore :
13+ - " README.md"
14+ branches : [ "**" ]
15+ pull_request :
16+
17+ env :
18+ # Use docker.io for Docker Hub if empty
19+ REGISTRY : ghcr.io
20+ # github.repository as <account>/<repo>
21+ IMAGE_NAME : ${{ github.repository }}
22+ concurrency :
23+ group : ${{ github.workflow }}-${{ github.ref }}
24+ cancel-in-progress : true
25+ jobs :
26+ Build_Container :
27+ name : Build Docker Container
28+ runs-on : ubuntu-latest
29+ permissions :
30+ contents : read
31+ packages : write
32+ # This is used to complete the identity challenge
33+ # with sigstore/fulcio when running outside of PRs.
34+ id-token : write
35+
36+ steps :
37+ - name : Checkout repository
38+ uses : actions/checkout@v4
39+
40+ # Set up BuildKit Docker container builder to be able to build
41+ # multi-platform images and export cache
42+ # https://github.com/docker/setup-buildx-action
43+ - name : Set up Docker Buildx
44+ uses : docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
45+
46+ # Login against a Docker registry except on PR
47+ # https://github.com/docker/login-action
48+ - name : Log into registry ${{ env.REGISTRY }}s
49+ if : github.event_name != 'pull_request'
50+ uses : docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
51+ with :
52+ registry : ${{ env.REGISTRY }}
53+ username : ${{ github.actor }}
54+ password : ${{ secrets.GITHUB_TOKEN }}
55+
56+ # Extract metadata (tags, labels) for Docker
57+ # https://github.com/docker/metadata-action
58+ - name : Extract Docker metadata
59+ id : meta
60+ uses : docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
61+ with :
62+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
63+
64+ # Build and push Docker image with Buildx (don't push on PR)
65+ # https://github.com/docker/build-push-action
66+ - name : Build Docker image
67+ id : buildandpush
68+ uses : docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
69+ with :
70+ context : .
71+ push : false
72+ load : true
73+ tags : ${{ steps.meta.outputs.tags }}
74+ labels : ${{ steps.meta.outputs.labels }}
75+ cache-from : type=gha
76+ cache-to : type=gha,mode=max
77+
78+ - name : Add Environment Variables
79+ id : env
80+ run : |
81+ echo "GH_PULLREQ_NUM=${{ github.event.number }}" >> $GITHUB_ENV
82+ echo "GH_EEVENT_NAME=${{ github.event_name }}" >> $GITHUB_ENV
83+ echo "GH_RREPOSITORY=${{ github.repository }}" >> $GITHUB_ENV
84+ - name : Create Tar Image For Upload
85+ id : tar
86+ run : |
87+ docker images
88+ tag=$(echo "${{ steps.meta.outputs.tags }}" | tr '\n' ' ')
89+ echo $tag
90+ docker save -o image.tar $tag
91+
92+ - name : Upload Artifact
93+ id : upload
94+ uses : actions/upload-artifact@v4
95+ with :
96+ name : image.tar
97+ path : image.tar
98+ retention-days : 1 # One Day (The Minimum)
99+ outputs :
100+ prnum : ${{ github.event.number }}
101+ url : ${{ steps.upload.outputs.artifact-url }}
102+ tag : ${{ env.IMAGE_NAME }}
103+ artifact_id : ${{ steps.upload.outputs.artifact-id }}
104+
105+ Test_Action :
106+ name : Test Docker Container/GitHub Action
107+ needs : Build_Container
108+ uses : ./.github/workflows/test.yml
109+ strategy :
110+ fail-fast : true
111+ matrix :
112+ write_job_summary : [true, false]
113+ repository : ["ZestCommunity/ZestCode"]
114+ ref : ["main"]
115+ caller_token : ["${{ github.token }}"]
116+ with :
117+ write_job_summary : ${{ matrix.write_job_summary }}
118+ repository : ${{ matrix.repository }}
119+ ref : ${{ matrix.ref }}
120+ caller_token : ${{ matrix.caller_token }}
121+
122+ Upload_Image :
123+ name : Upload Docker Image to ghcr.io Registry
124+ permissions :
125+ contents : read
126+ packages : write
127+ needs :
128+ [
129+ Build_Container,
130+ Test_Action
131+ ]
132+ runs-on : ubuntu-latest
133+ if : ${{ github.event_name != 'pull_request' }}
134+ steps :
135+ - name : Checkout repository
136+ uses : actions/checkout@v4
137+ - name : Set up Docker Buildx
138+ uses : docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
139+ - name : Log into registry ${{ env.REGISTRY }}
140+ uses : docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
141+ with :
142+ registry : ${{ env.REGISTRY }}
143+ username : ${{ github.actor }}
144+ password : ${{ secrets.GITHUB_TOKEN }}
145+ - name : Download Image
146+ uses : actions/download-artifact@v4
147+ with :
148+ name : image.tar
149+ github-token : ${{ secrets.GITHUB_TOKEN }}
150+ - name : Load Image
151+ id : load
152+ run : |
153+ echo "tag=$(docker load -i ./image.tar | grep -oP 'Loaded image: \K.*' | tr '\n' ' ')" > $GITHUB_OUTPUT
154+ - name : Push the image
155+ if : ${{ github.event_name != 'pull_request' }}
156+ run : |
157+ for tag in $(echo "${{ steps.load.outputs.tag }}" | tr ' ' '\n'); do
158+ echo "$tag"
159+ docker push "$tag"
160+ done
0 commit comments