1+ name : Build container
2+
3+ on :
4+ push :
5+ branches :
6+ - ' main'
7+ - ' [0-9]+.[1-9][0-9]*.x'
8+ pull_request :
9+ branches :
10+ - ' main'
11+ - ' [0-9]+.[1-9][0-9]*.x'
12+ paths-ignore :
13+ - " **.md"
14+
15+ env :
16+ GO_VERSION : " ~1.20"
17+ IMAGE_NAME : " cdevents-controller"
18+ defaults :
19+ run :
20+ shell : bash
21+
22+ jobs :
23+ prepare_ci_run :
24+ name : Prepare CI Run
25+ runs-on : ubuntu-22.04
26+ outputs :
27+ GIT_SHA : ${{ steps.extract_branch.outputs.GIT_SHA }}
28+ BRANCH : ${{ steps.extract_branch.outputs.BRANCH }}
29+ BRANCH_SLUG : ${{ steps.extract_branch.outputs.BRANCH_SLUG }}
30+ DATETIME : ${{ steps.get_datetime.outputs.DATETIME }}
31+ BUILD_TIME : ${{ steps.get_datetime.outputs.BUILD_TIME }}
32+ NON_FORKED_AND_NON_ROBOT_RUN : ${{ steps.get_run_type.outputs.NON_FORKED_AND_NON_ROBOT_RUN }}
33+
34+ steps :
35+ - name : Check out code
36+ uses : actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3
37+
38+ - name : Extract branch name
39+ id : extract_branch
40+ uses : keptn/gh-action-extract-branch-name@main
41+
42+ - name : Get current date and time
43+ id : get_datetime
44+ run : |
45+ DATETIME=$(date +'%Y%m%d%H%M')
46+ BUILD_TIME=$(date -u "+%F_%T")
47+ echo "DATETIME=$DATETIME" >> "$GITHUB_OUTPUT"
48+ echo "BUILD_TIME=$BUILD_TIME" >> "$GITHUB_OUTPUT"
49+
50+ - name : Get workflow run type
51+ id : get_run_type
52+ run : |
53+ NON_FORKED_AND_NON_ROBOT_RUN=${{ ( github.actor != 'renovate[bot]' && github.actor != 'dependabot[bot]' ) && ( github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository ) }}
54+ echo "github.actor != 'renovate[bot]' = ${{ github.actor != 'renovate[bot]' }}"
55+ echo "github.actor != 'dependabot[bot]' = ${{ github.actor != 'dependabot[bot]' }}"
56+ echo "github.event_name == 'push' = ${{ github.event_name == 'push' }}"
57+ echo "github.event.pull_request.head.repo.full_name == github.repository = ${{ github.event.pull_request.head.repo.full_name == github.repository }}"
58+ echo "NON_FORKED_AND_NON_ROBOT_RUN = $NON_FORKED_AND_NON_ROBOT_RUN"
59+ echo "NON_FORKED_AND_NON_ROBOT_RUN=$NON_FORKED_AND_NON_ROBOT_RUN" >> "$GITHUB_OUTPUT"
60+
61+ build_image :
62+ name : Build Container Image
63+ needs : prepare_ci_run
64+ runs-on : ubuntu-22.04
65+ env :
66+ BRANCH : ${{ needs.prepare_ci_run.outputs.BRANCH }}
67+ DATETIME : ${{ needs.prepare_ci_run.outputs.DATETIME }}
68+ BUILD_TIME : ${{ needs.prepare_ci_run.outputs.BUILD_TIME }}
69+ GIT_SHA : ${{ needs.prepare_ci_run.outputs.GIT_SHA }}
70+ RELEASE_REGISTRY : " localhost:5000/k8sgpt"
71+ steps :
72+ - name : Check out code
73+ uses : actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3
74+
75+ - name : Set up Docker Buildx
76+ id : buildx
77+ uses : docker/setup-buildx-action@4b4e9c3e2d4531116a6f8ba8e71fc6e2cb6e6c8c # v2
78+
79+ - name : Build Docker Image
80+ uses : docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 # v4
81+ with :
82+ context : .
83+ platforms : linux/amd64
84+ file : ./container/Dockerfile
85+ target : production
86+ tags : |
87+ ${{ env.RELEASE_REGISTRY }}/${{ env.IMAGE_NAME }}:dev-${{ env.DATETIME }}
88+ build-args : |
89+ GIT_HASH=${{ env.GIT_SHA }}
90+ RELEASE_VERSION=dev-${{ env.DATETIME }}
91+ BUILD_TIME=${{ env.BUILD_TIME }}
92+ builder : ${{ steps.buildx.outputs.name }}
93+ push : false
94+ cache-from : type=gha,scope=${{ github.ref_name }}-${{ env.IMAGE_NAME }}
95+ cache-to : type=gha,scope=${{ github.ref_name }}-${{ env.IMAGE_NAME }}
96+ outputs : type=docker,dest=/tmp/${{ env.IMAGE_NAME }}-image.tar
97+
98+ - name : Upload image as artifact
99+ uses : actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3
100+ with :
101+ name : ${{ env.IMAGE_NAME }}-image.tar
102+ path : /tmp/${{ env.IMAGE_NAME }}-image.tar
103+
104+ upload_images :
105+ name : Upload images to ghcr registry
106+ needs : [ prepare_ci_run, build_image ]
107+ if : github.event_name == 'push' && needs.prepare_ci_run.outputs.NON_FORKED_AND_NON_ROBOT_RUN == 'true' # only run on push to main/maintenance branches
108+ runs-on : ubuntu-22.04
109+ env :
110+ DATETIME : ${{ needs.prepare_ci_run.outputs.DATETIME }}
111+ BUILD_TIME : ${{ needs.prepare_ci_run.outputs.BUILD_TIME }}
112+ GIT_SHA : ${{ needs.prepare_ci_run.outputs.GIT_SHA }}
113+ permissions :
114+ packages : write # Needed for pushing images to the registry
115+ contents : read # Needed for checking out the repository
116+ steps :
117+ - name : Check out code
118+ uses : actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3
119+
120+ - name : Login to GitHub Container Registry
121+ uses : docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a # v2
122+ with :
123+ registry : " ghcr.io"
124+ username : ${{ github.actor }}
125+ password : ${{ secrets.GITHUB_TOKEN }}
126+
127+ - name : Set up Docker Buildx
128+ id : buildx
129+ uses : docker/setup-buildx-action@4b4e9c3e2d4531116a6f8ba8e71fc6e2cb6e6c8c # v2
130+
131+ - name : Build Docker Image
132+ uses : docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 # v4
133+ with :
134+ context : .
135+ file : ./container/Dockerfile
136+ platforms : linux/amd64,linux/arm64
137+ target : production
138+ tags : |
139+ ghcr.io/k8sgpt-ai/${{ env.IMAGE_NAME }}:dev-${{ env.DATETIME }}
140+ build-args : |
141+ GIT_HASH=${{ env.GIT_SHA }}
142+ RELEASE_VERSION=dev-${{ env.DATETIME }}
143+ BUILD_TIME=${{ env.BUILD_TIME }}
144+ builder : ${{ steps.buildx.outputs.name }}
145+ push : true
146+ cache-from : type=gha,scope=${{ github.ref_name }}-${{ env.IMAGE_NAME }}
147+ cache-to : type=gha,scope=${{ github.ref_name }}-${{ env.IMAGE_NAME }}
0 commit comments