Debian Codegraph @

.github/workflows/test.yml

raw
1 on: [push, pull_request]
2 name: Test
3 jobs:
4 test:
5 strategy:
6 matrix:
7 go-version: [1.26.x, 1.27.x]
8 os: [ubuntu-latest, macos-latest, windows-latest]
9 runs-on: ${{ matrix.os }}
10 timeout-minutes: 10
11 steps:
12 - uses: actions/checkout@v7
13 - uses: actions/setup-go@v7
14 with:
15 go-version: ${{ matrix.go-version }}
16 cache: false
17
18 - run: go test ./...
19 - run: cd moreinterp && go test ./...
20
21 - run: go test -race ./...
22 if: matrix.os == 'ubuntu-latest'
23 - run: GOARCH=386 go test -count=1 ./...
24 if: matrix.os == 'ubuntu-latest'
25 - name: confirm tests with Bash 5.2
26 run: |
27 go install mvdan.cc/dockexec@latest
28 CGO_ENABLED=0 go test -run TestRunnerRunConfirm -exec 'dockexec bash:5.2' ./interp
29 if: matrix.os == 'ubuntu-latest'
30
31 # Test that we can build for platforms that we can't currently test on.
32 - if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.27.x'
33 run: GOOS=plan9 GOARCH=amd64 go build ./...
34
35 # js/wasm can't run subprocesses, so many tests skip themselves there.
36 - if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.27.x'
37 run: PATH="$PATH:$(go env GOROOT)/lib/wasm" GOOS=js GOARCH=wasm go test ./...
38
39 # Static checks from this point forward. Only run on one Go version and on
40 # Linux, since it's the fastest platform, and the tools behave the same.
41 - if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.27.x'
42 run: diff <(echo -n) <(gofmt -s -d .)
43 - if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.27.x'
44 run: go vet ./...
45
46 test-linux-alpine:
47 runs-on: ubuntu-latest
48 timeout-minutes: 10
49 steps:
50 - uses: actions/checkout@v5
51 - name: Test as root, without cgo, and with busybox
52 run: docker run -v="$PWD:/pwd" -w=/pwd --user=1000 -e=GOCACHE=/tmp -e=CGO_ENABLED=0 golang:1.27.0-alpine go test ./...
53
54 docker:
55 name: Build and test Docker images
56 # Only deploy if previous stages pass.
57 needs: [test, test-linux-alpine]
58 runs-on: ubuntu-latest
59 timeout-minutes: 10
60 services:
61 registry:
62 image: registry:2
63 ports:
64 - 5000:5000
65 # this is needed because we restart the docker daemon for experimental
66 # support
67 options: "--restart always"
68 env:
69 # Export environment variables for all stages.
70 DOCKER_USER: ${{ secrets.DOCKER_USER }}
71 DOCKER_DEPLOY_IMAGES: false
72 DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
73 DOCKER_REPO: shfmt
74 # We use all platforms for which FROM images in our Dockerfile are
75 # available.
76 DOCKER_PLATFORMS: >
77 linux/386
78 linux/amd64
79 linux/arm/v7
80 linux/arm64/v8
81 linux/ppc64le
82
83 # linux/s390x TODO: reenable when we figure out its weird errors when
84 # fetching dependencies, including:
85 #
86 # zip: checksum error
87 # Get "https://proxy.golang.org/...": local error: tls: bad record MAC
88 # Get "https://proxy.golang.org/...": local error: tls: unexpected message
89 # Get "https://proxy.golang.org/...": x509: certificate signed by unknown authority
90 steps:
91 - uses: actions/checkout@v5
92 with:
93 fetch-depth: 0 # also fetch tags for 'git describe'
94 # Enable docker daemon experimental support (for 'pull --platform').
95 - name: Enable experimental support
96 run: |
97 config='/etc/docker/daemon.json'
98 if [[ -e "$config" ]]; then
99 sudo sed -i -e 's/{/{ "experimental": true, /' "$config"
100 else
101 echo '{ "experimental": true }' | sudo tee "$config"
102 fi
103 sudo systemctl restart docker
104 - uses: docker/setup-qemu-action@v3
105 - uses: docker/setup-buildx-action@v3
106 with:
107 driver-opts: network=host
108 - name: Set up env vars
109 run: |
110 set -vx
111 # Export environment variable for later stages.
112 if echo "$GITHUB_REF" | grep -q '^refs/heads/master$'; then
113 # Pushes to the master branch deploy 'latest'.
114 echo "TAG=latest" >> $GITHUB_ENV
115 elif echo "$GITHUB_REF" | grep -q '^refs/heads/docker-push-test$'; then
116 # Pushes to the test branch deploy 'latest-test'.
117 echo "TAG=latest-test" >> $GITHUB_ENV
118 elif echo "$GITHUB_REF" | grep -q '^refs/tags/'; then
119 # Pushes to a git tag use it as the docker tag.
120 echo "TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
121 else
122 # Otherwise, we build and test the image locally, but we don't push it.
123 echo "TAG=${GITHUB_SHA::8}" >> $GITHUB_ENV
124 fi
125 echo "DOCKER_BASE=test/${{ env.DOCKER_REPO }}" >> $GITHUB_ENV
126 echo "DOCKER_BUILD_PLATFORMS=${DOCKER_PLATFORMS// /,}" >> $GITHUB_ENV
127 - name: Build and push to local registry
128 uses: docker/build-push-action@v5
129 with:
130 provenance: false # temporarily work around https://github.com/containers/skopeo/issues/1874
131 context: .
132 file: ./cmd/shfmt/Dockerfile
133 platforms: ${{ env.DOCKER_BUILD_PLATFORMS }}
134 push: true
135 tags: localhost:5000/${{ env.DOCKER_BASE }}:${{ env.TAG }}
136 - name: Build and push to local registry (alpine)
137 uses: docker/build-push-action@v5
138 with:
139 provenance: false # temporarily work around https://github.com/containers/skopeo/issues/1874
140 context: .
141 file: ./cmd/shfmt/Dockerfile
142 platforms: ${{ env.DOCKER_BUILD_PLATFORMS }}
143 push: true
144 tags: localhost:5000/${{ env.DOCKER_BASE }}:${{ env.TAG }}-alpine
145 target: alpine
146 - name: Test multi-arch Docker images locally
147 run: |
148 for platform in $DOCKER_PLATFORMS; do
149 for ext in '' '-alpine'; do
150 image="localhost:5000/${DOCKER_BASE}:${TAG}${ext}"
151 msg="Testing docker image $image on platform $platform"
152 line="${msg//?/=}"
153 printf "\n${line}\n${msg}\n${line}\n"
154 docker pull -q --platform "$platform" "$image"
155 if [[ -n "$ext" ]]; then
156 echo -n "Image architecture: "
157 docker run --rm --entrypoint /bin/sh "$image" -c 'uname -m'
158 fi
159 version=$(docker run --rm "$image" --version)
160 echo "shfmt version: $version"
161 if [[ $TAG != "latest" ]] &&
162 [[ $TAG != "latest-test" ]] &&
163 [[ $TAG != "$version" ]] &&
164 ! echo "$version" | grep -q "$TAG"; then
165 echo "Version mismatch: shfmt $version tagged as $TAG"
166 exit 1
167 fi
168 docker run --rm -v "$PWD:/mnt" -w '/mnt' "$image" -d cmd/shfmt/docker-entrypoint.sh
169 done
170 done
171 - name: Check GitHub settings
172 if: >
173 github.event_name == 'push' &&
174 github.repository == 'mvdan/sh' &&
175 (github.ref == 'refs/heads/master' ||
176 github.ref == 'refs/heads/docker-push-test' ||
177 startsWith(github.ref, 'refs/tags/'))
178 run: |
179 missing=()
180 [[ -n "${{ secrets.DOCKER_USER }}" ]] || missing+=(DOCKER_USER)
181 [[ -n "${{ secrets.DOCKER_TOKEN }}" ]] || missing+=(DOCKER_TOKEN)
182 for i in "${missing[@]}"; do
183 echo "Missing github secret: $i"
184 done
185 (( ${#missing[@]} == 0 )) || exit 1
186 echo "DOCKER_DEPLOY_IMAGES=true" >> $GITHUB_ENV
187 - name: Login to DockerHub
188 if: ${{ env.DOCKER_DEPLOY_IMAGES == 'true' }}
189 uses: docker/login-action@v3
190 with:
191 username: ${{ secrets.DOCKER_USER }}
192 password: ${{ secrets.DOCKER_TOKEN }}
193 - name: Push images to DockerHub
194 if: ${{ env.DOCKER_DEPLOY_IMAGES == 'true' }}
195 run: |
196 for ext in '' '-alpine'; do
197 image_src="${DOCKER_BASE}:${TAG}${ext}"
198
199 image_dsts=("${{ secrets.DOCKER_USER }}/${{ env.DOCKER_REPO }}:${TAG}${ext}")
200 if echo $TAG | grep -q '^v3\.[0-9]\+\.[0-9]\+$'; then
201 image_dsts+=("${{ secrets.DOCKER_USER }}/${{ env.DOCKER_REPO }}:v3${ext}")
202 elif [[ $TAG == latest-test ]]; then
203 image_dsts+=("${{ secrets.DOCKER_USER }}/${{ env.DOCKER_REPO }}:v3-test${ext}")
204 fi
205
206 # Show what we're doing.
207 msg="Copy multi-arch docker images to DockerHub ($image_src with ${#image_dsts[@]} destinations)"
208 line="${msg//?/=}"
209 printf "\n${line}\n${msg}\n${line}\n"
210
211 for image_dst in "${image_dsts[@]}"; do
212 skopeo copy --all --src-tls-verify=0 docker://localhost:5000/$image_src docker://docker.io/$image_dst
213 done
214 done
215 - name: Update DockerHub description
216 if: ${{ env.DOCKER_DEPLOY_IMAGES == 'true' }}
217 uses: peter-evans/dockerhub-description@v4
218 with:
219 username: ${{ secrets.DOCKER_USER }}
220 password: ${{ secrets.DOCKER_TOKEN }}
221 repository: ${{ secrets.DOCKER_USER }}/${{ env.DOCKER_REPO }}
222 readme-filepath: README.md