Sustainable use of common-pool resources such as fish, water or forests depends on the cooperation of resource users that restrain their individual extraction to socially optimal levels. Empirical evidence has shown that under certain social and biophysical conditions, self-organized cooperation in the commons can evolve. Global change, however, may drastically alter these conditions. We assess the robustness of cooperation to environmental variability in a stylized model of a community that harvests a shared resource. Community members follow a norm of socially optimal resource extraction, which is enforced through social sanctioning. Our results indicate that both resource abundance and a small increase in resource variability can lead to collapse of cooperation observed in the no-variability case, while either scarcity or large variability have the potential to stabilize it. The combined effects of changes in amount and variability can reinforce or counteract each other depending on their size and the initial level of cooperation in the community. If two socially separate groups are ecologically connected through resource leakage, cooperation in one can destabilize the other. These findings provide insights into possible effects of global change and spatial connectivity, indicating that there is no simple answer as to their effects on cooperation and sustainable resource use.