Skip to content

Commit 0d93e58

Browse files
committed
ci: [#448] simplify container publish jobs by branch type
1 parent 8340801 commit 0d93e58

1 file changed

Lines changed: 53 additions & 54 deletions

File tree

.github/workflows/container.yaml

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
# Following patterns from torrust/torrust-tracker container.yaml workflow.
55
#
66
# Triggers:
7-
# - Push to main/develop branches
7+
# - Push to main/develop/releases/** branches
88
# - Pull requests to main/develop
99
# - Manual dispatch
1010
#
1111
# Publishing:
12-
# - Images are pushed to Docker Hub on push to main/develop (not PRs)
13-
# - Requires Docker Hub credentials in repository secrets
12+
# - Images are pushed to Docker Hub on push to main/develop/release branches (not PRs)
13+
# - Release branches (releases/vX.Y.Z) publish versioned Docker tags (X.Y.Z)
14+
# - Release Docker tags use bare semver without the v prefix
15+
# - Requires Docker Hub credentials in the dockerhub-torrust GitHub Environment
1416

1517
name: Container
1618

@@ -19,6 +21,7 @@ on:
1921
branches:
2022
- "develop"
2123
- "main"
24+
- "releases/**/*"
2225
paths:
2326
- "src/**"
2427
- "Cargo.toml"
@@ -100,6 +103,7 @@ jobs:
100103
outputs:
101104
continue: ${{ steps.check.outputs.continue }}
102105
type: ${{ steps.check.outputs.type }}
106+
version: ${{ steps.check.outputs.version }}
103107

104108
steps:
105109
- name: Check Context
@@ -108,10 +112,15 @@ jobs:
108112
if [[ "${{ github.repository }}" == "torrust/torrust-tracker-deployer" ]]; then
109113
if [[ "${{ github.event_name }}" == "push" ]]; then
110114
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
111-
echo "type=production" >> $GITHUB_OUTPUT
115+
echo "type=main" >> $GITHUB_OUTPUT
112116
echo "continue=true" >> $GITHUB_OUTPUT
113117
elif [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then
114-
echo "type=development" >> $GITHUB_OUTPUT
118+
echo "type=develop" >> $GITHUB_OUTPUT
119+
echo "continue=true" >> $GITHUB_OUTPUT
120+
elif [[ $(echo "${{ github.ref }}" | grep -P '^refs/heads/releases/v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$') ]]; then
121+
version=$(echo "${{ github.ref }}" | sed -n -E 's/^refs\/heads\/releases\///p')
122+
echo "version=$version" >> $GITHUB_OUTPUT
123+
echo "type=release" >> $GITHUB_OUTPUT
115124
echo "continue=true" >> $GITHUB_OUTPUT
116125
fi
117126
fi
@@ -122,71 +131,54 @@ jobs:
122131
echo "continue=false" >> $GITHUB_OUTPUT
123132
fi
124133
125-
publish_development:
126-
name: Publish (Development)
134+
publish:
135+
name: Publish (${{ needs.context.outputs.type }})
127136
environment: dockerhub-torrust
128137
needs: context
129-
if: needs.context.outputs.continue == 'true' && needs.context.outputs.type == 'development'
138+
if: needs.context.outputs.continue == 'true'
130139
runs-on: ubuntu-latest
131140
timeout-minutes: 30
132141

133142
steps:
134143
- name: Checkout
135144
uses: actions/checkout@v5
136145

137-
- name: Docker Meta
138-
id: meta
139-
uses: docker/metadata-action@v5
140-
with:
141-
images: |
142-
${{ env.DOCKER_HUB_USERNAME }}/tracker-deployer
143-
tags: |
144-
type=ref,event=branch
145-
type=sha,prefix=dev-
146-
147-
- name: Login to Docker Hub
148-
uses: docker/login-action@v3
149-
with:
150-
username: ${{ env.DOCKER_HUB_USERNAME }}
151-
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
152-
153-
- name: Setup Docker Buildx
154-
uses: docker/setup-buildx-action@v3
155-
156-
- name: Build and Push
157-
uses: docker/build-push-action@v6
158-
with:
159-
context: .
160-
file: ./docker/deployer/Dockerfile
161-
target: release
162-
push: true
163-
tags: ${{ steps.meta.outputs.tags }}
164-
labels: ${{ steps.meta.outputs.labels }}
165-
cache-from: type=gha
166-
cache-to: type=gha,mode=max
167-
168-
publish_production:
169-
name: Publish (Production)
170-
environment: dockerhub-torrust
171-
needs: context
172-
if: needs.context.outputs.continue == 'true' && needs.context.outputs.type == 'production'
173-
runs-on: ubuntu-latest
174-
timeout-minutes: 30
175-
176-
steps:
177-
- name: Checkout
178-
uses: actions/checkout@v5
146+
- name: Configure Docker Tag Strategy
147+
id: tag_config
148+
run: |
149+
if [[ "${{ needs.context.outputs.type }}" == "develop" ]]; then
150+
{
151+
echo "tags<<EOF"
152+
echo "type=ref,event=branch"
153+
echo "type=sha,prefix=dev-"
154+
echo "EOF"
155+
} >> "$GITHUB_OUTPUT"
156+
elif [[ "${{ needs.context.outputs.type }}" == "main" ]]; then
157+
{
158+
echo "tags<<EOF"
159+
echo "type=raw,value=latest"
160+
echo "type=ref,event=branch"
161+
echo "type=sha"
162+
echo "EOF"
163+
} >> "$GITHUB_OUTPUT"
164+
elif [[ "${{ needs.context.outputs.type }}" == "release" ]]; then
165+
{
166+
echo "tags<<EOF"
167+
echo "type=semver,value=${{ needs.context.outputs.version }},pattern={{version}}"
168+
echo "EOF"
169+
} >> "$GITHUB_OUTPUT"
170+
else
171+
echo "Unsupported publish type: ${{ needs.context.outputs.type }}" >&2
172+
exit 1
173+
fi
179174
180175
- name: Docker Meta
181176
id: meta
182177
uses: docker/metadata-action@v5
183178
with:
184179
images: |
185180
${{ env.DOCKER_HUB_USERNAME }}/tracker-deployer
186-
tags: |
187-
type=raw,value=latest
188-
type=ref,event=branch
189-
type=sha
181+
tags: ${{ steps.tag_config.outputs.tags }}
190182

191183
- name: Login to Docker Hub
192184
uses: docker/login-action@v3
@@ -208,3 +200,10 @@ jobs:
208200
labels: ${{ steps.meta.outputs.labels }}
209201
cache-from: type=gha
210202
cache-to: type=gha,mode=max
203+
204+
- name: Inspect Published Image
205+
if: needs.context.outputs.type == 'release'
206+
run: |
207+
version=$(echo "${{ needs.context.outputs.version }}" | sed 's/^v//')
208+
docker pull ${{ env.DOCKER_HUB_USERNAME }}/tracker-deployer:"$version"
209+
docker image inspect ${{ env.DOCKER_HUB_USERNAME }}/tracker-deployer:"$version"

0 commit comments

Comments
 (0)