Skip to content

Commit 795103c

Browse files
authored
Test build on pull requests (#102)
* Build image on pull-requests and publish on main and tags * Only on pull-requests to main * Use artifact to save/load container from build to publish job * Add missing variable * Add missing needed step * Cleanup useless step ids
1 parent e706926 commit 795103c

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

.github/workflows/build-image.yaml renamed to .github/workflows/build-publish.yaml

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
name: Build Docker image
1+
name: Build and Publish Docker image
22

33
on:
4+
pull_request:
5+
branches:
6+
- main
7+
48
push:
59
branches:
610
- main
@@ -27,17 +31,14 @@ jobs:
2731
fi
2832
env:
2933
version_pattern: "tags\\/v[0-9]+\\.[0-9]+\\.[0-9]+"
30-
build-image:
34+
build:
3135
runs-on: ubuntu-latest
3236
needs: prepare
3337
environment:
3438
name: cloudops
35-
outputs:
36-
BUILD_TAG: ${{ steps.build-push.outputs.build-tag }}
3739

3840
steps:
39-
- name: Echo tag
40-
id: echotag
41+
- name: Docker image tag
4142
env:
4243
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
4344
run: |
@@ -54,14 +55,11 @@ jobs:
5455
password: ${{ secrets.DOCKERHUB_TOKEN }}
5556

5657
- name: Extract metadata (tags, labels) for Docker
57-
id: meta
5858
uses: docker/metadata-action@v3
5959
with:
6060
images: $GITHUB_REPOSITORY
6161

62-
63-
- name: Build and push to Docker Hub
64-
id: build-push
62+
- name: Build Docker image
6563
env:
6664
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
6765
run: |
@@ -72,8 +70,46 @@ jobs:
7270
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
7371
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > ./version.json
7472
docker build --file Dockerfile -t $GITHUB_REPOSITORY:$IMAGE_TAG .
73+
74+
- name: Save image as artifact
75+
env:
76+
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
77+
run: |
78+
docker save -o /tmp/container.tar.gz $GITHUB_REPOSITORY:$IMAGE_TAG
79+
80+
- uses: actions/upload-artifact@v3
81+
with:
82+
name: container-image
83+
path: /tmp/container.tar.gz
84+
retention-days: 1 # Instead of default 90 days
85+
86+
publish:
87+
runs-on: ubuntu-latest
88+
needs:
89+
- prepare
90+
- build
91+
if: ${{ !github.event.pull_request }} # Exclude pull-requests (see `on:` above)
92+
93+
steps:
94+
- uses: actions/download-artifact@v3
95+
with:
96+
name: container-image
97+
path: /tmp/container.tar.gz
98+
99+
- name: Load container from artifact
100+
run: |
101+
docker load -i /tmp/container.tar.gz
102+
103+
- name: Login to Docker Hub
104+
uses: docker/login-action@v1
105+
with:
106+
username: ${{ secrets.DOCKERHUB_USERNAME }}
107+
password: ${{ secrets.DOCKERHUB_TOKEN }}
108+
109+
- name: Build Docker image
110+
env:
111+
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
112+
run: |
75113
docker image tag $GITHUB_REPOSITORY:$IMAGE_TAG $GITHUB_REPOSITORY:latest
76114
docker push $GITHUB_REPOSITORY:$IMAGE_TAG
77115
docker push $GITHUB_REPOSITORY:latest
78-
echo "::set-output name=build-tag::$IMAGE_TAG"
79-
echo "::debug::Set the build-tag output as $IMAGE_TAG"

0 commit comments

Comments
 (0)