1+ # PlatformIO Docker Build Workflow
2+ name : PlatformIO Docker Image
3+
4+ on :
5+ push :
6+ branches : [master, dev]
7+ paths :
8+ - ' .github/workflows/platformio.yml'
9+ - ' images/platformio/**'
10+ pull_request :
11+ branches : [master, dev]
12+ paths :
13+ - ' .github/workflows/platformio.yml'
14+ - ' images/platformio/**'
15+ # Manual trigger
16+ workflow_dispatch :
17+ inputs :
18+ force_rebuild :
19+ description : ' Force rebuild without cache'
20+ required : false
21+ default : false
22+ type : boolean
23+ custom_tag :
24+ description : ' Additional custom tag (optional)'
25+ required : false
26+ default : ' '
27+ type : string
28+ # Scheduled monthly rebuild (1st of each month at 2 AM UTC)
29+ schedule :
30+ - cron : ' 0 2 1 * *'
31+
32+ env :
33+ REGISTRY : ghcr.io
34+ IMAGE_NAME : jethome-dev-platformio
35+
36+ jobs :
37+ build-and-push :
38+ runs-on : ubuntu-latest
39+ permissions :
40+ contents : read
41+ packages : write
42+
43+ steps :
44+ - name : Checkout repository
45+ uses : actions/checkout@v4
46+
47+ - name : Set up Docker Buildx
48+ uses : docker/setup-buildx-action@v3
49+
50+ - name : Log in to GitHub Container Registry
51+ if : github.event_name != 'pull_request'
52+ uses : docker/login-action@v3
53+ with :
54+ registry : ${{ env.REGISTRY }}
55+ username : ${{ github.actor }}
56+ password : ${{ secrets.GITHUB_TOKEN }}
57+
58+ - name : Extract metadata
59+ id : meta
60+ uses : docker/metadata-action@v5
61+ with :
62+ images : ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
63+
64+ - name : Generate tags based on branch
65+ id : tags
66+ run : |
67+ TAGS=""
68+ SHA_SHORT=$(echo "${{ github.sha }}" | cut -c1-7)
69+ VERSION=$(date +%Y.%m.%d)
70+
71+ # Determine branch (for manual/scheduled triggers)
72+ if [[ "${{ github.event_name }}" == "schedule" ]] || [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
73+ BRANCH="${{ github.ref_name }}"
74+ else
75+ BRANCH="${{ github.ref }}"
76+ fi
77+
78+ if [[ "$BRANCH" == "refs/heads/master" ]] || [[ "$BRANCH" == "master" ]]; then
79+ TAGS="${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest"
80+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:stable"
81+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:$VERSION"
82+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:sha-$SHA_SHORT"
83+ elif [[ "$BRANCH" == "refs/heads/dev" ]] || [[ "$BRANCH" == "dev" ]]; then
84+ TAGS="${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:dev"
85+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:dev-$VERSION"
86+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:sha-$SHA_SHORT"
87+ fi
88+
89+ # Add custom tag if provided
90+ if [[ "${{ github.event.inputs.custom_tag }}" != "" ]]; then
91+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ github.event.inputs.custom_tag }}"
92+ fi
93+
94+ # Add rebuild tag for scheduled/manual rebuilds
95+ if [[ "${{ github.event_name }}" == "schedule" ]]; then
96+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:monthly-$(date +%Y%m%d)"
97+ elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
98+ TAGS="$TAGS,${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:manual-$(date +%Y%m%d-%H%M%S)"
99+ fi
100+
101+ echo "TAGS=$TAGS" >> $GITHUB_OUTPUT
102+
103+ - name : Determine cache strategy
104+ id : cache
105+ run : |
106+ if [[ "${{ github.event.inputs.force_rebuild }}" == "true" ]]; then
107+ echo "CACHE_FROM=" >> $GITHUB_OUTPUT
108+ echo "CACHE_TO=" >> $GITHUB_OUTPUT
109+ else
110+ echo "CACHE_FROM=type=gha" >> $GITHUB_OUTPUT
111+ echo "CACHE_TO=type=gha,mode=max" >> $GITHUB_OUTPUT
112+ fi
113+
114+ - name : Build and push Docker image
115+ uses : docker/build-push-action@v5
116+ with :
117+ context : images/platformio
118+ push : ${{ github.event_name != 'pull_request' }}
119+ tags : ${{ steps.tags.outputs.TAGS }}
120+ labels : ${{ steps.meta.outputs.labels }}
121+ cache-from : ${{ steps.cache.outputs.CACHE_FROM }}
122+ cache-to : ${{ steps.cache.outputs.CACHE_TO }}
123+ platforms : linux/amd64,linux/arm64
0 commit comments