-
-
Notifications
You must be signed in to change notification settings - Fork 115
178 lines (157 loc) · 6.6 KB
/
docker-publish.yml
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
name: Publish Docker images
on:
push:
branches: [main]
workflow_dispatch:
inputs:
tag:
description: "The tag of release"
required: true
permissions:
id-token: write # Required to auth to AWS
contents: read # Required for actions/checkout
packages: write # Required to upload to ghcr
env:
AWS_EC2_INSTANCE_ID: i-0960f9b8de8285aed
AWS_REGION: us-east-1
jobs:
push_base_image:
name: Push base Docker image to multiple registries
runs-on: ubuntu-latest
steps:
- name: Checkout project
uses: actions/checkout@v3
- name: Parse tag
# This step converts Grain tags into standard semver, i.e. grain-v1.2.3 -> v1.2.3
id: vars
if: ${{ github.event.inputs.tag }}
run: GITHUB_TAG=${{ github.event.inputs.tag }}; echo ::set-output name=tag::${GITHUB_TAG#grain-}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1.6.1
with:
role-to-assume: arn:aws:iam::881972110175:role/github-actions-openid
role-session-name: cisession
aws-region: ${{ env.AWS_REGION }}
- name: Start AWS EC2 arm64 instance
id: instance
run: |
aws ec2 start-instances --instance-ids ${{ env.AWS_EC2_INSTANCE_ID }}
aws ec2 wait instance-status-ok --instance-ids ${{ env.AWS_EC2_INSTANCE_ID }}
echo ::set-output name=ip::$(aws ec2 describe-instances --filters "Name=instance-id,Values=${{ env.AWS_EC2_INSTANCE_ID }}" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text)
- name: Bring in SSH keys
uses: grain-lang/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.AWS_SSH_PRIVATE_KEY }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2.0.0
- name: Set up remote builder
run: |
ssh-keyscan -H ${{ steps.instance.outputs.ip }} >> ~/.ssh/known_hosts
docker context create node-amd64 --docker host=unix:///var/run/docker.sock
docker context create node-arm64 --docker host=ssh://ubuntu@${{ steps.instance.outputs.ip }}
docker buildx create --use --name multiarch-builder --platform linux/amd64 node-amd64
docker buildx create --append --name multiarch-builder --platform linux/arm64 node-arm64
docker buildx inspect --bootstrap
- name: Docker meta
id: meta
uses: docker/metadata-action@v3.6.0
with:
images: |
grainlang/grain
ghcr.io/grain-lang/grain
tags: |
type=ref,event=branch
type=semver,pattern={{version}},value=${{ steps.vars.outputs.tag }}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.vars.outputs.tag }}
- name: Login to DockerHub
uses: docker/login-action@v2.0.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to Github Container Registry
uses: docker/login-action@v2.0.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
uses: docker/build-push-action@v2.7.0
with:
context: .
file: Dockerfile
push: true
builder: multiarch-builder
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push_slim_image:
name: Push slim Docker image to multiple registries
runs-on: ubuntu-latest
steps:
- name: Checkout project
uses: actions/checkout@v3
- name: Parse tag
# This step converts Grain tags into standard semver, i.e. grain-v1.2.3 -> v1.2.3
id: vars
if: ${{ github.event.inputs.tag }}
run: GITHUB_TAG=${{ github.event.inputs.tag }}; echo ::set-output name=tag::${GITHUB_TAG#grain-}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1.6.1
with:
role-to-assume: arn:aws:iam::881972110175:role/github-actions-openid
role-session-name: cisession
aws-region: ${{ env.AWS_REGION }}
- name: Start AWS EC2 arm64 instance
id: instance
run: |
aws ec2 start-instances --instance-ids ${{ env.AWS_EC2_INSTANCE_ID }}
aws ec2 wait instance-status-ok --instance-ids ${{ env.AWS_EC2_INSTANCE_ID }}
echo ::set-output name=ip::$(aws ec2 describe-instances --filters "Name=instance-id,Values=${{ env.AWS_EC2_INSTANCE_ID }}" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text)
- name: Bring in SSH keys
uses: grain-lang/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.AWS_SSH_PRIVATE_KEY }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2.0.0
- name: Set up remote builder
run: |
ssh-keyscan -H ${{ steps.instance.outputs.ip }} >> ~/.ssh/known_hosts
docker context create node-amd64 --docker host=unix:///var/run/docker.sock
docker context create node-arm64 --docker host=ssh://ubuntu@${{ steps.instance.outputs.ip }}
docker buildx create --use --name multiarch-builder --platform linux/amd64 node-amd64
docker buildx create --append --name multiarch-builder --platform linux/arm64 node-arm64
docker buildx inspect --bootstrap
- name: Docker meta
id: meta
uses: docker/metadata-action@v3.6.0
with:
flavor: |
suffix=-slim,onlatest=true
images: |
grainlang/grain
ghcr.io/grain-lang/grain
tags: |
type=ref,event=branch
type=semver,pattern={{version}},value=${{ steps.vars.outputs.tag }}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.vars.outputs.tag }}
- name: Login to DockerHub
uses: docker/login-action@v2.0.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to Github Container Registry
uses: docker/login-action@v2.0.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push slim image
uses: docker/build-push-action@v2.7.0
with:
context: .
file: Dockerfile-slim
push: true
builder: multiarch-builder
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}