-
Notifications
You must be signed in to change notification settings - Fork 0
180 lines (160 loc) · 7.67 KB
/
Copy pathdockerbuild-github.yml
File metadata and controls
180 lines (160 loc) · 7.67 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
name: Build Docker image for Github
on:
workflow_call:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: "Cleanup build folder"
run: |
ls -la ./
rm -rf ./* || true
rm -rf ./.??* || true
ls -la ./
- uses: actions/checkout@v4
with:
fetch-tags: true
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: "1.24"
check-latest: true
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set Github Registry
id: gh-registry
run: |
echo "GITHUB_REGISTRY=ghcr.io" >> $GITHUB_ENV
- name: Import Secrets
id: import-secrets
uses: hashicorp/vault-action@v3
with:
url: ${{ secrets.VAULT_ADDR }}
method: approle
roleId: ${{ secrets.VAULT_ROLE_ID }}
secretId: ${{ secrets.VAULT_SECRET_ID }}
secrets: |
secret/deployments/github registry | REGISTRY;
secret/deployments/github token | GH_TOKEN
- name: Import Secrets for Staging
id: import-secrets-staging
uses: hashicorp/vault-action@v3
if: (github.event.inputs.environment != 'production' && startsWith(github.event.ref, 'refs/heads/master')) || github.event.inputs.environment == 'staging'
with:
url: ${{ secrets.VAULT_ADDR }}
method: approle
roleId: ${{ secrets.VAULT_ROLE_ID }}
secretId: ${{ secrets.VAULT_SECRET_ID }}
secrets: |
staging/mgmt/hetzner deployment_bucket_access_key | BUCKET_ACCESS_KEY;
staging/mgmt/hetzner deployment_bucket_endpoint | BUCKET_ENDPOINT;
staging/mgmt/hetzner deployment_bucket_name | BUCKET_NAME;
staging/mgmt/hetzner deployment_bucket_region | BUCKET_REGION;
staging/mgmt/hetzner deployment_bucket_secret_key | BUCKET_SECRET_KEY
- name: Import Secrets for Production
id: import-secrets-production
uses: hashicorp/vault-action@v3
if: startsWith(github.event.ref, 'refs/tags/v') || github.event.inputs.environment == 'production'
with:
url: ${{ secrets.VAULT_ADDR }}
method: approle
roleId: ${{ secrets.VAULT_ROLE_ID }}
secretId: ${{ secrets.VAULT_SECRET_ID }}
secrets: |
secret/mgmt/hetzner deployment_bucket_access_key | BUCKET_ACCESS_KEY;
secret/mgmt/hetzner deployment_bucket_endpoint | BUCKET_ENDPOINT;
secret/mgmt/hetzner deployment_bucket_name | BUCKET_NAME;
secret/mgmt/hetzner deployment_bucket_region | BUCKET_REGION;
secret/mgmt/hetzner deployment_bucket_secret_key | BUCKET_SECRET_KEY
- name: Extract staging metadata for Docker on Github
id: meta_staging
uses: docker/metadata-action@v5
if: (github.event.inputs.environment != 'production' && startsWith(github.event.ref, 'refs/heads/master')) || github.event.inputs.environment == 'staging'
with:
images: ${{ env.GITHUB_REGISTRY }}/${{ github.repository }}
tags: |
type=schedule
type=semver,pattern={{version}}
type=raw,value=latest,enable=${{ inputs.environment == 'staging' || github.event.ref == format('refs/heads/{0}', 'master') }}
- name: Extract production metadata for Docker
id: meta_live
uses: docker/metadata-action@v5
if: startsWith(github.event.ref, 'refs/tags/v') || github.event.inputs.environment == 'production'
with:
images: ${{ env.GITHUB_REGISTRY }}/${{ github.repository }}
tags: |
type=schedule
type=semver,pattern={{version}}
type=raw,value=stable,enable=${{ inputs.environment == 'production' || startsWith(github.event.ref, 'refs/tags/v') }}
- name: Extract branch name
shell: bash
run: |
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
echo "tag=$(git describe --tags || git rev-parse --short HEAD || echo 'none')" >> $GITHUB_OUTPUT
id: extract_branch
- name: Build and push to staging
id: push
uses: docker/build-push-action@v5
if: (github.event.inputs.environment != 'production' && startsWith(github.event.ref, 'refs/heads/master')) || github.event.inputs.environment == 'staging'
with:
file: Dockerfile
context: .
build-args: |
VAULT_ADDR=${{ secrets.VAULT_ADDR }}
VAULT_ROLE_ID=${{ secrets.VAULT_ROLE_ID }}
VAULT_SECRET_ID=${{ secrets.VAULT_SECRET_ID }}
GH_TOKEN=${{ env.GH_TOKEN }}
push: true
tags: |
${{ steps.meta_staging.outputs.tags }}
- name: Build and push to production
uses: docker/build-push-action@v5
if: startsWith(github.event.ref, 'refs/tags/v') || github.event.inputs.environment == 'production'
with:
file: Dockerfile
context: .
build-args: |
VAULT_ADDR=${{ secrets.VAULT_ADDR }}
VAULT_ROLE_ID=${{ secrets.VAULT_ROLE_ID }}
VAULT_SECRET_ID=${{ secrets.VAULT_SECRET_ID }}
GH_TOKEN=${{ env.GH_TOKEN }}
push: true
tags: |
${{ steps.meta_live.outputs.tags }}
- name: Set up S3cmd for staging
uses: mthie/s3cmd@main
if: (github.event.inputs.environment != 'production' && startsWith(github.event.ref, 'refs/heads/master')) || github.event.inputs.environment == 'staging'
with:
provider: hcloud
region: ${{ steps.import-secrets-staging.outputs.BUCKET_REGION }}
access_key: ${{ steps.import-secrets-staging.outputs.BUCKET_ACCESS_KEY }}
secret_key: ${{ steps.import-secrets-staging.outputs.BUCKET_SECRET_KEY }}
- name: "Upload version files for staging"
if: (github.event.inputs.environment != 'production' && startsWith(github.event.ref, 'refs/heads/master')) || github.event.inputs.environment == 'staging'
run: |
echo "${{ steps.extract_branch.outputs.branch }}" > version.txt
cp version.txt ${{ steps.extract_branch.outputs.tag }}.txt
s3cmd rm s3://${{ env.BUCKET_NAME }}/contentflow-builds/${{ github.event.repository.name }}/*.txt || true
s3cmd put ${{ steps.extract_branch.outputs.tag }}.txt s3://${{ env.BUCKET_NAME }}/contentflow-builds/${{ github.event.repository.name }}/
- name: Set up S3cmd for production
uses: mthie/s3cmd@main
if: startsWith(github.event.ref, 'refs/tags/v') || github.event.inputs.environment == 'production'
with:
provider: hcloud
region: ${{ steps.import-secrets-production.outputs.BUCKET_REGION }}
access_key: ${{ steps.import-secrets-production.outputs.BUCKET_ACCESS_KEY }}
secret_key: ${{ steps.import-secrets-production.outputs.BUCKET_SECRET_KEY }}
- name: "Upload version files for production"
if: startsWith(github.event.ref, 'refs/tags/v') || github.event.inputs.environment == 'production'
run: |
echo "${{ steps.extract_branch.outputs.branch }}" > version.txt
cp version.txt ${{ steps.extract_branch.outputs.tag }}.txt
s3cmd rm s3://${{ env.BUCKET_NAME }}/contentflow-builds/${{ github.event.repository.name }}/*.txt || true
s3cmd put ${{ steps.extract_branch.outputs.tag }}.txt s3://${{ env.BUCKET_NAME }}/contentflow-builds/${{ github.event.repository.name }}/