-
Notifications
You must be signed in to change notification settings - Fork 0
200 lines (164 loc) · 7.51 KB
/
dockerise-image.yml
File metadata and controls
200 lines (164 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
name: "dockerise-image"
on:
workflow_call:
inputs:
year:
description: "current year"
type: string
required: true
user_name:
description: "name of the repisotry owner"
type: string
required: true
repo_name:
description: "name of the repository"
type: string
required: true
submodule_status:
description: "status of submodule creation"
type: string
required: true
repo_url:
description: "student's repo url"
type: string
required: true
secrets:
DEPENDENCY_GRAPH_TOKEN:
required: true
outputs:
user_name:
description: "user_name"
value: ${{ jobs.create_docker_images.outputs.user_name }}
repo_name:
description: "repo_name"
value: ${{ jobs.create_docker_images.outputs.repo_name }}
year:
description: "year"
value: ${{ jobs.create_docker_images.outputs.year }}
image_name:
description: "image_name"
value: ${{ jobs.create_docker_images.outputs.image_name }}
jobs:
get_repository_dependencies:
permissions:
contents: "write"
runs-on: ubuntu-latest
defaults:
run:
shell: bash
if: inputs.submodule_status == 'success'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Pull the changes made in the repository.
run: git pull --recurse-submodules
- name: Get submodule's dependencies
run: |
year=$(date '+%Y')
user_name=$(echo "${{ inputs.repo_url }}" | cut -d'/' -f4)
repo_name=$(echo "${{ inputs.repo_url }}" | cut -d'/' -f5)
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.DEPENDENCY_GRAPH_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${user_name}/${repo_name}/dependency-graph/sbom >> extracted_dependencies.json
jq '.sbom.packages' extracted_dependencies.json >> extracted_packages.json
jq ".[] | .name" extracted_packages.json >> package_names_list.txt
rm extracted_dependencies.json extracted_packages.json
- id: dependencies_extraction_result
name: Provide the result of dependencies extraction to the next job
run: |
if grep -q "express" package_names_list.txt ; then
echo "Found express dependency"
DEPENDENCY_RESULT=$(echo "npm:express")
elif grep -q "flask" package_names_list.txt ; then
echo "Found flask dependency"
DEPENDENCY_RESULT=$(echo "pip:flask")
else
echo "value does not exist!"
DEPENDENCY_RESULT="failed"
fi
echo "DEPENDENCY_RESULT=$DEPENDENCY_RESULT" >> "$GITHUB_OUTPUT"
rm package_names_list.txt
outputs:
dependency_result: ${{ steps.dependencies_extraction_result.outputs.DEPENDENCY_RESULT }}
year: ${{ inputs.year }}
user_name: ${{ inputs.user_name }}
repo_name: ${{ inputs.repo_name }}
create_docker_images:
needs: get_repository_dependencies
permissions:
contents: "write"
id-token: "write"
runs-on: ubuntu-latest
defaults:
run:
shell: bash
if: needs.get_repository_dependencies.outputs.dependency_result != 'failed'
steps:
- name: Checkout
uses: actions/checkout@v3
- id: get_file_name
name: Copy Docker image into student's submodule
run: |
git pull --recurse-submodules
cd ./submissions-${{ needs.get_repository_dependencies.outputs.year }}/${{ needs.get_repository_dependencies.outputs.user_name }}-${{ needs.get_repository_dependencies.outputs.repo_name}}
git submodule update --init --recursive
cd ./${{ needs.get_repository_dependencies.outputs.user_name }}-${{ needs.get_repository_dependencies.outputs.repo_name}}
if [ ${{ needs.get_repository_dependencies.outputs.dependency_result }} == "npm:express" ] ; then
echo "Copying Dockerfiles/nodejs/Dockerfile into repository"
cp ../../../Dockerfiles/nodejs/Dockerfile .
FILE_NAME=$(echo "server.js")
if [[ -f ./main.js ]]; then
FILE_NAME=$(echo "main.js")
elif [[ -f ./app.js ]]; then
FILE_NAME=$(echo "app.js")
elif [[ -f ./index.js ]]; then
FILE_NAME=$(echo "index.js")
fi
echo "FILE_NAME=$FILE_NAME" >> "$GITHUB_OUTPUT"
elif [ ${{ needs.get_repository_dependencies.outputs.dependency_result }} == "pip:flask" ] ; then
echo "Copying Dockerfiles/flask/Dockerfile into repository"
cp ../../../Dockerfiles/flask/Dockerfile .
export DOCKER_BUILDKIT=1 # or configure in daemon.json
export COMPOSE_DOCKER_CLI_BUILD=1
else
echo "value does not exist!"
fi
- id: "auth"
name: "Authenticate to Google Cloud"
uses: "google-github-actions/auth@v2"
with:
workload_identity_provider: "projects/1006240973223/locations/global/workloadIdentityPools/docker-image-workflow-pool/providers/github-actions-provider"
service_account: "github-actions-magic@code-idp.iam.gserviceaccount.com"
access_token_lifetime: 300s
create_credentials_file: true
cleanup_credentials: true
access_token_scopes: https://www.googleapis.com/auth/cloud-platform
id_token_include_email: false
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: code-idp
- name: Setup Authentication to Docker repository
run: gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://europe-west10-docker.pkg.dev
- id: build-docker-image
name: build and push docker image for the student's submodule
run: |
cd ./submissions-${{ needs.get_repository_dependencies.outputs.year }}/${{ needs.get_repository_dependencies.outputs.user_name }}-${{ needs.get_repository_dependencies.outputs.repo_name}}
SHA_VALUE="$(git submodule status ./${{ needs.get_repository_dependencies.outputs.user_name }}-${{ needs.get_repository_dependencies.outputs.repo_name}} | awk '{print $1}')"
cd ./${{ needs.get_repository_dependencies.outputs.user_name }}-${{ needs.get_repository_dependencies.outputs.repo_name}}
user_name=${{ needs.get_repository_dependencies.outputs.user_name }}
repo_name=${{ needs.get_repository_dependencies.outputs.repo_name }}
if [ -f ./Dockerfile ]; then
docker build -t europe-west10-docker.pkg.dev/code-idp/idp-artifact-registry/${user_name}-${repo_name}:${SHA_VALUE} --build-arg file_name=${{ steps.get_file_name.outputs.FILE_NAME }} .
docker push europe-west10-docker.pkg.dev/code-idp/idp-artifact-registry/${user_name}-${repo_name}:${SHA_VALUE}
echo "Docker image pushed"
IMAGE_NAME=$(echo "europe-west10-docker.pkg.dev/code-idp/idp-artifact-registry/${user_name}-${repo_name}:${SHA_VALUE}")
echo "IMAGE_NAME=$IMAGE_NAME" >> "$GITHUB_OUTPUT"
fi
outputs:
year: ${{ needs.get_repository_dependencies.outputs.year }}
user_name: ${{ needs.get_repository_dependencies.outputs.user_name }}
repo_name: ${{ needs.get_repository_dependencies.outputs.repo_name }}
image_name: ${{ steps.build-docker-image.outputs.IMAGE_NAME }}