Skip to content

Commit 33ea10f

Browse files
authored
Merge pull request #9 from philibea/handle-dns-prefix
feat(dns): handle prefix
2 parents 31534f4 + f5be0d5 commit 33ea10f

File tree

7 files changed

+121
-55
lines changed

7 files changed

+121
-55
lines changed

.github/workflows/main.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ jobs:
5151
type: deploy
5252
scw_access_key: ${{ secrets.ACCESS_KEY }}
5353
scw_secret_key: ${{ secrets.SECRET_KEY }}
54-
scw_project_id: 9d1d9183-bc98-4a92-ab54-be5ef9ada957
5554
scw_containers_namespace_id: ae28eaf1-3b94-4660-bce0-9b0e0a5d1062
5655
scw_registry: rg.fr-par.scw.cloud/testong/shire-testing:latest
5756
scw_container_port: "80"
5857
scw_dns: containers.philibeaux.fr
58+
scw_dns_prefix: testing-github
5959

6060
- name: check output
6161
shell: bash
@@ -101,10 +101,10 @@ jobs:
101101
type: teardown
102102
scw_access_key: ${{ secrets.ACCESS_KEY }}
103103
scw_secret_key: ${{ secrets.SECRET_KEY }}
104-
scw_project_id: 9d1d9183-bc98-4a92-ab54-be5ef9ada957
105104
scw_containers_namespace_id: ae28eaf1-3b94-4660-bce0-9b0e0a5d1062
106105
scw_registry: rg.fr-par.scw.cloud/testong/shire-testing:latest
107106
scw_dns: containers.philibeaux.fr
107+
scw_dns_prefix: testing-github
108108

109109
- name: check output
110110
shell: bash

.github/workflows/publish.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
push:
77
branches:
88
- main
9+
tags:
10+
- 'v*.*.*'
911

1012
jobs:
1113
publish:
@@ -29,6 +31,7 @@ jobs:
2931
type=schedule
3032
type=ref,event=branch
3133
type=ref,event=pr
34+
type=ref,event=tag
3235
type=semver,pattern={{version}}
3336
type=semver,pattern={{major}}.{{minor}}
3437
type=semver,pattern={{major}}

README.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
1-
# scaleway-containers-deploy
2-
scaleway deploy container with registry and custom endpoint dns
1+
# Scaleway Containers Deploy
2+
3+
This github action makes it easy to deploy on the containers product from the path of a registry scaleway.
4+
It adds the functionality of using its own domain which must be on scaleway.
5+
6+
You can check the `action.yaml` file to see how it works.
7+
8+
# Exemple of use:
9+
10+
scw_access_key, scw_secret_key & scw_containers_namespace_id will always be necessary
11+
12+
## simple deploy
13+
14+
| input name | value |
15+
| --------------------------- | -------------------------------------- |
16+
| type | deploy |
17+
| scw_registry | rg.fr-par.scw.cloud/test/images:latest |
18+
19+
## simple teardown
20+
21+
| input name | value |
22+
| --------------------------- | -------------------------------------- |
23+
| type | teardown |
24+
| scw_registry | rg.fr-par.scw.cloud/test/images:latest |
25+
26+
## dns deploy
27+
28+
| input name | value |
29+
| --------------------------- | -------------------------------------- |
30+
| type | deploy |
31+
| scw_registry | rg.fr-par.scw.cloud/test/images:latest |
32+
| scw_dns | containers.test.fr |
33+
| scw_dns_prefix (optional) | testing |
34+
35+
36+
if not define scw_dns_prefix, the action will use the default value: "name of you created container"
37+
38+
This created containers will be based on the tag name of the registry.
39+
40+
## dns teardown
41+
42+
| input name | value |
43+
| --------------------------- | -------------------------------------- |
44+
| type | teardown |
45+
| scw_registry | rg.fr-par.scw.cloud/test/images:latest |
46+
| scw_dns | containers.test.fr |
47+
| scw_dns_prefix (optional) | testing |

action.yaml

+10-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ inputs:
1111
scw_secret_key:
1212
description: "secret key ( https://console.scaleway.com/project/credentials )"
1313
required: true
14-
scw_project_id:
15-
description: "Project ID"
16-
required: true
14+
# scw_project_id:
15+
# description: "Project ID"
16+
# required: true
1717
scw_containers_namespace_id:
1818
description: "default namespace id where your container will be deploy, if this not required, this action will create a namespace and we need projectID to be set."
19-
required: false
19+
required: true
2020
scw_registry:
2121
description: "From your scaleway registry ( format: rg.$REGION.scw.cloud/$NAMESPACE/$IMAGE:$TAG )"
2222
required: true
@@ -27,6 +27,9 @@ inputs:
2727
scw_dns:
2828
description: "DNS name where your container will be available"
2929
required: false
30+
scw_dns_prefix:
31+
description: "DNS name where your container will be available"
32+
required: false
3033

3134
# scw_debug:
3235
# description: "debug log api call"
@@ -39,7 +42,7 @@ outputs:
3942
scw_container_id:
4043
description: "ID of your container inside your namespace"
4144
container_url:
42-
description: 'Url of your container deployed'
45+
description: 'Url of your deployed container'
4346
url:
4447
description: "Url with dns set"
4548

@@ -51,10 +54,11 @@ runs:
5154
- ${{ inputs.type }}
5255
- ${{ inputs.scw_access_key }}
5356
- ${{ inputs.scw_secret_key }}
54-
- ${{ inputs.scw_project_id }}
57+
# - ${{ inputs.scw_project_id }}
5558
- ${{ inputs.scw_containers_namespace_id }}
5659
- ${{ inputs.scw_registry }}
5760
- ${{ inputs.scw_container_port }}
5861
- ${{ inputs.scw_dns }}
62+
- ${{ inputs.scw_dns_prefix }}
5963
# - ${{ inputs.scw_debug }}
6064

container.go

+14-20
Original file line numberDiff line numberDiff line change
@@ -96,43 +96,33 @@ func DeleteContainer(
9696

9797
}
9898

99-
func GetOrCreateContainersNamespace(
99+
func GetContainersNamespace(
100100
client *scw.Client,
101101
Region scw.Region,
102102
) (*container.Namespace, error) {
103103

104104
// OPTIONAL ENV VARIABLES
105105
ContainersNamespaceId := os.Getenv(EnvContainerNamespaceID)
106106

107-
api := container.NewAPI(client)
108-
109-
if ContainersNamespaceId != "" {
110-
111-
namespace, err := api.GetNamespace(&container.GetNamespaceRequest{
112-
Region: Region,
113-
NamespaceID: ContainersNamespaceId,
114-
})
107+
if ContainersNamespaceId == "" {
115108

116-
if err != nil {
117-
fmt.Println("unable to get namespace: ", err)
118-
} else {
119-
return namespace, nil
120-
}
109+
return nil, fmt.Errorf("containers namespace id not found")
121110
}
122111

123-
Description := "Namespace created by a github action ( philibea/scaleway-action-container )"
112+
api := container.NewAPI(client)
124113

125-
createdNamespace, err := api.CreateNamespace(&container.CreateNamespaceRequest{
126-
Description: &Description,
114+
namespace, err := api.GetNamespace(&container.GetNamespaceRequest{
127115
Region: Region,
116+
NamespaceID: ContainersNamespaceId,
128117
})
129118

130119
if err != nil {
131-
fmt.Println("unable to create namespace: ", err)
132-
return nil, err
120+
fmt.Println("unable to get namespace: ", err)
121+
} else {
122+
return namespace, nil
133123
}
134124

135-
return createdNamespace, nil
125+
return namespace, nil
136126
}
137127

138128
func isContainerAlreadyCreated(
@@ -240,6 +230,10 @@ func SetCustomDomainContainer(
240230
Hostname string,
241231
) (*container.Domain, error) {
242232

233+
if Hostname == "" {
234+
return nil, fmt.Errorf("hostname is required")
235+
}
236+
243237
api := container.NewAPI(client)
244238

245239
ResListDomains, _ := api.ListDomains(&container.ListDomainsRequest{

dns.go

+34-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"time"
67

78
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
@@ -15,10 +16,13 @@ const (
1516
)
1617

1718
const (
18-
TTL = uint32(360)
1919
TYPE = "CNAME"
2020
)
2121

22+
var (
23+
TTL = uint32(360)
24+
)
25+
2226
func WaitForDNS(
2327
client *scw.Client,
2428
DNSZone string,
@@ -64,20 +68,28 @@ func WaitForDNS(
6468

6569
func DeleteDNSRecord(
6670
client *scw.Client,
67-
DNSZone string,
6871
Container *container.Container,
72+
DNSZone string,
6973
) (*domain.UpdateDNSZoneRecordsResponse, error) {
70-
fmt.Println("Update Zone DNS")
74+
fmt.Println("Update Zone DNS - Delete")
7175

7276
api := domain.NewAPI(client)
7377

7478
Data := Container.DomainName + "."
7579

80+
Prefix := os.Getenv(EnvDNSPrefix)
81+
82+
var Name string = Container.Name
83+
84+
if Prefix != "" {
85+
Name = Prefix
86+
}
87+
7688
IDFields := &domain.RecordIdentifier{
77-
Name: Container.Name,
89+
Name: Name,
7890
Data: &Data,
7991
Type: TYPE,
80-
//TTL: TTL,
92+
TTL: &TTL,
8193
}
8294

8395
Changes := []*domain.RecordChange{
@@ -104,23 +116,28 @@ func DeleteDNSRecord(
104116

105117
func AddDNSRecord(
106118
client *scw.Client,
107-
DNSZone string,
108119
Container *container.Container,
120+
DNSZone string,
121+
) (string, error) {
109122

110-
) (*domain.UpdateDNSZoneRecordsResponse, error) {
111-
112-
fmt.Println("Update Zone DNS")
123+
fmt.Println("Update Zone DNS - Add")
113124

114125
api := domain.NewAPI(client)
115126

116-
Data := Container.DomainName + "."
127+
Prefix := os.Getenv(EnvDNSPrefix)
128+
129+
var Name string = Container.Name
130+
131+
if Prefix != "" {
132+
Name = Prefix
133+
}
117134

118135
Records := []*domain.Record{
119136
{
120-
Name: Container.Name,
137+
Name: Name,
121138
Type: TYPE,
122139
TTL: TTL,
123-
Data: Data,
140+
Data: Container.DomainName + ".",
124141
},
125142
}
126143

@@ -132,16 +149,17 @@ func AddDNSRecord(
132149
},
133150
}
134151

135-
records, err := api.UpdateDNSZoneRecords(
152+
_, err := api.UpdateDNSZoneRecords(
136153
&domain.UpdateDNSZoneRecordsRequest{
137154
DNSZone: DNSZone,
138155
Changes: Changes,
139156
})
140157

141158
if err != nil {
142-
143-
return nil, err
159+
return "", err
144160
}
145161

146-
return records, nil
162+
Hostname := Name + "." + DNSZone
163+
164+
return Hostname, nil
147165
}

0 commit comments

Comments
 (0)